Commit 992ea229 authored by vicotor's avatar vicotor

add tls

parent 6ca129da
...@@ -17,11 +17,12 @@ import ( ...@@ -17,11 +17,12 @@ import (
type CaddyAPI struct { type CaddyAPI struct {
Url string // URL of the Caddy API Url string // URL of the Caddy API
RootDir string // Root directory of the Caddy to store website files and .caddy files. RootDir string // Root directory of the Caddy to store website files and .caddy files.
MasterDomain string
client *http.Client client *http.Client
} }
func NewCaddyAPI(url string, root string) *CaddyAPI { func NewCaddyAPI(url string, root string, master string) *CaddyAPI {
return &CaddyAPI{Url: url, RootDir: root, client: &http.Client{}} return &CaddyAPI{Url: url, RootDir: root, client: &http.Client{}, MasterDomain: master}
} }
func (c *CaddyAPI) buildPayloadForCreateWebsite(domain string, root string) (string, error) { func (c *CaddyAPI) buildPayloadForCreateWebsite(domain string, root string) (string, error) {
...@@ -45,13 +46,14 @@ func (c *CaddyAPI) buildPayloadForCreateWebsite(domain string, root string) (str ...@@ -45,13 +46,14 @@ func (c *CaddyAPI) buildPayloadForCreateWebsite(domain string, root string) (str
return buffer.String(), nil return buffer.String(), nil
} }
func (c *CaddyAPI) newWebsiteCaddyFile(domain string, root string) error { func (c *CaddyAPI) newWebsiteCaddyFile(domain string, root string, master 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: fmt.Sprintf("https://%s", domain), DomainName: fmt.Sprintf("https://%s", domain),
RootDir: root, RootDir: root,
MasterDomain: master,
} }
tmpl, err := template.New("test").Parse(staticWebsiteCaddyFileTempl) tmpl, err := template.New("test").Parse(staticWebsiteCaddyFileTempl)
if err != nil { if err != nil {
...@@ -71,13 +73,14 @@ func (c *CaddyAPI) newWebsiteCaddyFile(domain string, root string) error { ...@@ -71,13 +73,14 @@ func (c *CaddyAPI) newWebsiteCaddyFile(domain string, root string) error {
return nil return nil
} }
func (c *CaddyAPI) newForwardCaddyFile(from string, to string) error { func (c *CaddyAPI) newForwardCaddyFile(from string, to string, master string) error {
buffer := bytes.NewBufferString("") buffer := bytes.NewBufferString("")
caddypath := filepath.Join(c.RootDir, fmt.Sprintf("%s.caddy", from)) caddypath := filepath.Join(c.RootDir, fmt.Sprintf("%s.caddy", from))
// build CaddyFileTemplate. // build CaddyFileTemplate.
cfg := ForwardWebsiteCaddyFile{ cfg := ForwardWebsiteCaddyFile{
DomainName: fmt.Sprintf("https://%s", from), DomainName: fmt.Sprintf("https://%s", from),
Target: to, Target: to,
MasterDomain: master,
} }
tmpl, err := template.New("test").Parse(forwardWebsiteCaddyFileTempl) tmpl, err := template.New("test").Parse(forwardWebsiteCaddyFileTempl)
if err != nil { if err != nil {
...@@ -159,7 +162,7 @@ func (c *CaddyAPI) CreateWebsite(domain string, resource string) error { ...@@ -159,7 +162,7 @@ func (c *CaddyAPI) CreateWebsite(domain string, resource string) error {
return err return err
} }
// create caddyfile. // create caddyfile.
if err := c.newWebsiteCaddyFile(domain, websiteRoot); err != nil { if err := c.newWebsiteCaddyFile(domain, websiteRoot, c.MasterDomain); err != nil {
return err return err
} }
...@@ -181,7 +184,7 @@ func (c *CaddyAPI) CreateWebsite(domain string, resource string) error { ...@@ -181,7 +184,7 @@ 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 {
// create caddyfile. // create caddyfile.
if err := c.newForwardCaddyFile(param.Domain, param.Target); err != nil { if err := c.newForwardCaddyFile(param.Domain, param.Target, c.MasterDomain); err != nil {
return err return err
} }
......
...@@ -2,6 +2,7 @@ package caddy ...@@ -2,6 +2,7 @@ package caddy
var staticWebsiteCaddyFileTempl = ` var staticWebsiteCaddyFileTempl = `
{{ .DomainName }} { {{ .DomainName }} {
tls /certs/{{ .MasterDomain }}/{{ .MasterDomain }}.crt /certs/{{ .MasterDomain }}/{{ .MasterDomain }}.key
root * {{ .RootDir }} root * {{ .RootDir }}
file_server file_server
...@@ -11,6 +12,7 @@ var staticWebsiteCaddyFileTempl = ` ...@@ -11,6 +12,7 @@ var staticWebsiteCaddyFileTempl = `
var forwardWebsiteCaddyFileTempl = ` var forwardWebsiteCaddyFileTempl = `
{{ .DomainName }} { {{ .DomainName }} {
tls /certs/{{ .MasterDomain }}/{{ .MasterDomain }}.crt /certs/{{ .MasterDomain }}/{{ .MasterDomain }}.key
reverse_proxy https://{{ .Target }} { reverse_proxy https://{{ .Target }} {
header_up Host {upstream_hostport} header_up Host {upstream_hostport}
transport http { transport http {
......
...@@ -3,6 +3,7 @@ package caddy ...@@ -3,6 +3,7 @@ package caddy
type NewWebsiteCaddyFile struct { type NewWebsiteCaddyFile struct {
DomainName string DomainName string
RootDir string RootDir string
MasterDomain string
} }
type StaticWebsiteCreatePayload struct { type StaticWebsiteCreatePayload struct {
...@@ -14,6 +15,7 @@ type StaticWebsiteCreatePayload struct { ...@@ -14,6 +15,7 @@ type StaticWebsiteCreatePayload struct {
type ForwardWebsiteCaddyFile struct { type ForwardWebsiteCaddyFile struct {
DomainName string DomainName string
Target string Target string
MasterDomain string
} }
type ForwardWebsitePayload struct { type ForwardWebsitePayload struct {
......
...@@ -6,6 +6,7 @@ const ( ...@@ -6,6 +6,7 @@ const (
caddyUrlFlag = "caddy-url" caddyUrlFlag = "caddy-url"
caddyRootFlag = "caddy-root" caddyRootFlag = "caddy-root"
downloadDirFlag = "download-dir" downloadDirFlag = "download-dir"
masterDomainFlag = "master-domain"
logFlag = "log" logFlag = "log"
) )
...@@ -16,6 +17,7 @@ type serviceParam struct { ...@@ -16,6 +17,7 @@ type serviceParam struct {
caddyRoot string caddyRoot string
logPath string logPath string
downloadDir string downloadDir string
masterDomain string
} }
var ( var (
...@@ -26,5 +28,6 @@ var ( ...@@ -26,5 +28,6 @@ var (
caddyRoot: "", caddyRoot: "",
logPath: "/root/data/service.log", logPath: "/root/data/service.log",
downloadDir: "/root/data/download", downloadDir: "/root/data/download",
masterDomain: "",
} }
) )
...@@ -92,7 +92,7 @@ func runCommand(cmd *cobra.Command, _ []string) { ...@@ -92,7 +92,7 @@ func runCommand(cmd *cobra.Command, _ []string) {
} }
}() }()
capi := caddy.NewCaddyAPI(params.caddyUrl, params.caddyRoot) capi := caddy.NewCaddyAPI(params.caddyUrl, params.caddyRoot, params.masterDomain)
api := openapi.NewOpenAPI(&openapi.Config{ api := openapi.NewOpenAPI(&openapi.Config{
Host: params.host, Host: params.host,
......
...@@ -4,4 +4,5 @@ type Config struct { ...@@ -4,4 +4,5 @@ type Config struct {
Host string Host string
Port int Port int
TempDir string // temp dir store download files. TempDir string // temp dir store download files.
MasterDomain 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