Commit 2e93fa3a authored by vicotor's avatar vicotor

support more master domain

parent c335710a
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"text/template" "text/template"
"time" "time"
) )
...@@ -17,11 +18,17 @@ import ( ...@@ -17,11 +18,17 @@ 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 MasterDomain map[string]bool
client *http.Client client *http.Client
} }
func NewCaddyAPI(url string, root string, master string) *CaddyAPI { func NewCaddyAPI(url string, root string, masterList string) *CaddyAPI {
master := make(map[string]bool)
list := strings.Split(masterList, ",")
for _, domain := range list {
master[strings.ToLower(domain)] = true
}
return &CaddyAPI{Url: url, RootDir: root, client: &http.Client{}, MasterDomain: master} return &CaddyAPI{Url: url, RootDir: root, client: &http.Client{}, MasterDomain: master}
} }
...@@ -128,7 +135,22 @@ func pathExist(path string) bool { ...@@ -128,7 +135,22 @@ func pathExist(path string) bool {
return true return true
} }
func extractMasterDomain(domain string) (string, error) {
parts := strings.Split(domain, ".")
if len(parts) < 2 {
return domain, nil
}
return strings.ToLower(strings.Join(parts[len(parts)-2:], ".")), nil
}
func (c *CaddyAPI) CreateWebsite(domain string, resource string) error { func (c *CaddyAPI) CreateWebsite(domain string, resource string) error {
// check domain is in masterDomain list.
masterDomain, err := extractMasterDomain(domain)
if err != nil || !c.MasterDomain[masterDomain] {
return fmt.Errorf("domain %s is not support on this service", domain)
}
// 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)
rollback := false rollback := false
...@@ -162,7 +184,7 @@ func (c *CaddyAPI) CreateWebsite(domain string, resource string) error { ...@@ -162,7 +184,7 @@ func (c *CaddyAPI) CreateWebsite(domain string, resource string) error {
return err return err
} }
// create caddyfile. // create caddyfile.
if err := c.newWebsiteCaddyFile(domain, websiteRoot, c.MasterDomain); err != nil { if err := c.newWebsiteCaddyFile(domain, websiteRoot, masterDomain); err != nil {
return err return err
} }
...@@ -183,8 +205,13 @@ func (c *CaddyAPI) CreateWebsite(domain string, resource string) error { ...@@ -183,8 +205,13 @@ 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 {
// check domain is in masterDomain list.
masterDomain, err := extractMasterDomain(param.Domain)
if err != nil || !c.MasterDomain[masterDomain] {
return fmt.Errorf("domain %s is not support on this service", param.Domain)
}
// create caddyfile. // create caddyfile.
if err := c.newForwardCaddyFile(param.Domain, param.Target, c.MasterDomain); err != nil { if err := c.newForwardCaddyFile(param.Domain, param.Target, masterDomain); err != nil {
return err return err
} }
......
...@@ -86,7 +86,7 @@ func setFlags(cmd *cobra.Command) { ...@@ -86,7 +86,7 @@ func setFlags(cmd *cobra.Command) {
&params.masterDomain, &params.masterDomain,
masterDomainFlag, masterDomainFlag,
"", "",
"master domain", "master domain, if you need more than one, use ',' to split",
) )
} }
......
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