Commit 992ea229 authored by vicotor's avatar vicotor

add tls

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