Commit 5532c088 authored by vicotor's avatar vicotor

add function for forward

parent cc502407
...@@ -43,12 +43,12 @@ func (c *CaddyAPI) buildPayloadForCreateWebsite(domain string, root string) (str ...@@ -43,12 +43,12 @@ func (c *CaddyAPI) buildPayloadForCreateWebsite(domain string, root string) (str
return buffer.String(), nil return buffer.String(), nil
} }
func (c *CaddyAPI) newCaddyFile(domain string, root string) error { func (c *CaddyAPI) newWebsiteCaddyFile(domain string, root string) error {
buffer := bytes.NewBufferString("") buffer := bytes.NewBufferString("")
caddypath := filepath.Join(c.RootDir, fmt.Sprintf("%s.caddy", domain)) caddypath := filepath.Join(c.RootDir, fmt.Sprintf("%s.caddy", domain))
// build CaddyFileTemplate. // build CaddyFileTemplate.
cfg := NewWebsiteCaddyFile{ cfg := NewWebsiteCaddyFile{
DomainName: domain, DomainName: fmt.Sprintf("https://%s", domain),
RootDir: root, RootDir: root,
} }
tmpl, err := template.New("test").Parse(staticWebsiteCaddyFileTempl) tmpl, err := template.New("test").Parse(staticWebsiteCaddyFileTempl)
...@@ -69,6 +69,52 @@ func (c *CaddyAPI) newCaddyFile(domain string, root string) error { ...@@ -69,6 +69,52 @@ func (c *CaddyAPI) newCaddyFile(domain string, root string) error {
return nil return nil
} }
func (c *CaddyAPI) newForwardCaddyFile(from string, to string) error {
buffer := bytes.NewBufferString("")
caddypath := filepath.Join(c.RootDir, fmt.Sprintf("%s.caddy", from))
// build CaddyFileTemplate.
cfg := ForwardWebsiteCaddyFile{
DomainName: fmt.Sprintf("https://%s", from),
Target: to,
}
tmpl, err := template.New("test").Parse(forwardWebsiteCaddyFileTempl)
if err != nil {
log.WithError(err).Error("failed to parse forward caddyfile template")
return err
}
err = tmpl.Execute(buffer, cfg)
if err != nil {
log.WithError(err).Error("failed to build forward caddyfile")
return err
}
if err = utils.CreateFile(caddypath, buffer.String()); err != nil {
log.WithError(err).Error("failed to create forward caddyfile")
return err
}
return nil
}
func (c *CaddyAPI) buildPayloadForForwardWebsite(domain string, target string) (string, error) {
// build the payload for creating a new website.
buffer := bytes.NewBufferString("")
cfg := ForwardWebsitePayload{
DomainName: domain,
Target: target,
}
tmpl, err := template.New("test").Parse(forwardWebsitePayloadTempl)
if err != nil {
log.WithError(err).Error("failed to parse forward payload template")
return "", err
}
err = tmpl.Execute(buffer, cfg)
if err != nil {
log.WithError(err).Error("failed to build forward payload")
return "", err
}
return buffer.String(), nil
}
func (c *CaddyAPI) CreateWebsite(domain string, resource string) error { func (c *CaddyAPI) CreateWebsite(domain string, resource string) error {
// unzip resource to the website root directory. // unzip resource to the website root directory.
websiteRoot := filepath.Join(c.RootDir, "websites", domain) websiteRoot := filepath.Join(c.RootDir, "websites", domain)
...@@ -76,7 +122,7 @@ func (c *CaddyAPI) CreateWebsite(domain string, resource string) error { ...@@ -76,7 +122,7 @@ func (c *CaddyAPI) CreateWebsite(domain string, resource string) error {
return err return err
} }
// create caddyfile. // create caddyfile.
if err := c.newCaddyFile(domain, websiteRoot); err != nil { if err := c.newWebsiteCaddyFile(domain, websiteRoot); err != nil {
return err return err
} }
...@@ -97,7 +143,24 @@ func (c *CaddyAPI) CreateWebsite(domain string, resource string) error { ...@@ -97,7 +143,24 @@ func (c *CaddyAPI) CreateWebsite(domain string, resource string) error {
} }
func (c *CaddyAPI) ForwardWebsite(param types.ForwardWebsite) error { func (c *CaddyAPI) ForwardWebsite(param types.ForwardWebsite) error {
// todo: create a new site in Caddy and set the target to the domain. // create caddyfile.
if err := c.newForwardCaddyFile(param.Domain, param.Target); err != nil {
return err
}
// build api payload.
payload, err := c.buildPayloadForForwardWebsite(param.Domain, param.Target)
if err != nil {
return err
}
// send put request to the caddy api.
path := fmt.Sprintf("%s/config/apps/http/servers/srv0/routes/%d", c.Url, 0)
log.WithField("payload", payload).Debug("sending payload to caddy")
if err := c.put(path, []byte(payload)); err != nil {
return err
}
return nil return nil
} }
......
...@@ -9,6 +9,63 @@ var staticWebsiteCaddyFileTempl = ` ...@@ -9,6 +9,63 @@ var staticWebsiteCaddyFileTempl = `
} }
` `
var forwardWebsiteCaddyFileTempl = `
{{ .DomainName }} {
reverse_proxy https://{{ .Target }} {
header_up Host {upstream_hostport}
transport http {
tls_insecure_skip_verify
}
}
}
`
var forwardWebsitePayloadTempl = `
{
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"handler": "reverse_proxy",
"headers": {
"request": {
"set": {
"Host": [
"{http.reverse_proxy.upstream.hostport}"
]
}
}
},
"transport": {
"protocol": "http",
"tls": {
"insecure_skip_verify": true
}
},
"upstreams": [
{
"dial": "{{ .Target }}:443"
}
]
}
]
}
]
}
],
"match": [
{
"host": [
"{{ .DomainName }}"
]
}
],
"terminal": true
}`
var newWebsitePayloadTempl = ` var newWebsitePayloadTempl = `
{ {
"handle": [ "handle": [
......
...@@ -10,3 +10,13 @@ type StaticWebsiteCreatePayload struct { ...@@ -10,3 +10,13 @@ type StaticWebsiteCreatePayload struct {
WebsiteCaddyFile string WebsiteCaddyFile string
RootDir string RootDir string
} }
type ForwardWebsiteCaddyFile struct {
DomainName string
Target string
}
type ForwardWebsitePayload struct {
DomainName string
Target string
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment