diff options
author | Kamil Trzcinski <ayufan@ayufan.eu> | 2016-02-11 21:36:15 +0300 |
---|---|---|
committer | Kamil Trzcinski <ayufan@ayufan.eu> | 2016-02-11 21:36:15 +0300 |
commit | 8166163d39f1f972b2bea2817bdc78cc0777faa3 (patch) | |
tree | fa41836f32a4368eaf1b1e8c15c6e029a76ee496 | |
parent | 3732867a8f79171e2d5aefb8ceaa4b887fbbaf72 (diff) |
Move most of configuration to appConfig
-rw-r--r-- | app.go | 2 | ||||
-rw-r--r-- | domain_config.go | 4 | ||||
-rw-r--r-- | domain_config_test.go | 16 | ||||
-rw-r--r-- | domains.go | 28 | ||||
-rw-r--r-- | domains_test.go | 6 | ||||
-rw-r--r-- | main.go | 10 |
6 files changed, 31 insertions, 35 deletions
@@ -120,7 +120,7 @@ func (a *theApp) Run() { }() } - go watchDomains(a.UpdateDomains, time.Second) + go watchDomains(a.Domain, a.UpdateDomains, time.Second) wg.Wait() } diff --git a/domain_config.go b/domain_config.go index d384ef67..e0f00cc4 100644 --- a/domain_config.go +++ b/domain_config.go @@ -17,14 +17,14 @@ type domainsConfig struct { Domains []domainConfig } -func (c *domainConfig) Valid() bool { +func (c *domainConfig) Valid(rootDomain string) bool { if c.Domain == "" { return false } // TODO: better sanitize domain domain := strings.ToLower(c.Domain) - rootDomain := "." + strings.ToLower(*pagesDomain) + rootDomain = "." + rootDomain return !strings.HasSuffix(domain, rootDomain) } diff --git a/domain_config_test.go b/domain_config_test.go index 507eb3a9..51b9468b 100644 --- a/domain_config_test.go +++ b/domain_config_test.go @@ -15,27 +15,25 @@ const validConfig = `{"Domains":[{"Domain":"test"}]}` func TestDomainConfigValidness(t *testing.T) { d := domainConfig{} - assert.False(t, d.Valid()) + assert.False(t, d.Valid("gitlab.io")) d = domainConfig{Domain: "test"} - assert.True(t, d.Valid()) - - *pagesDomain = "gitlab.io" + assert.True(t, d.Valid("gitlab.io")) d = domainConfig{Domain: "test"} - assert.True(t, d.Valid()) + assert.True(t, d.Valid("gitlab.io")) d = domainConfig{Domain: "test.gitlab.io"} - assert.False(t, d.Valid()) + assert.False(t, d.Valid("gitlab.io")) d = domainConfig{Domain: "test.test.gitlab.io"} - assert.False(t, d.Valid()) + assert.False(t, d.Valid("gitlab.io")) d = domainConfig{Domain: "test.testgitlab.io"} - assert.True(t, d.Valid()) + assert.True(t, d.Valid("gitlab.io")) d = domainConfig{Domain: "test.GitLab.Io"} - assert.False(t, d.Valid()) + assert.False(t, d.Valid("gitlab.io")) } func TestDomainConfigRead(t *testing.T) { @@ -15,7 +15,7 @@ type domains map[string]*domain type domainsUpdater func(domains domains) -func (d domains) addDomain(group, project string, config *domainConfig) error { +func (d domains) addDomain(rootDomain, group, project string, config *domainConfig) error { newDomain := &domain{ Group: group, Project: project, @@ -26,14 +26,14 @@ func (d domains) addDomain(group, project string, config *domainConfig) error { if config != nil { domainName = config.Domain } else { - domainName = group + "." + *pagesDomain + domainName = group + "." + rootDomain } domainName = strings.ToLower(domainName) d[domainName] = newDomain return nil } -func (d domains) readProjectConfig(group, project string) (err error) { +func (d domains) readProjectConfig(rootDomain, group, project string) (err error) { var config domainsConfig err = config.Read(group, project) if err != nil { @@ -41,14 +41,14 @@ func (d domains) readProjectConfig(group, project string) (err error) { } for _, domainConfig := range config.Domains { - if domainConfig.Valid() { - d.addDomain(group, project, &domainConfig) + if domainConfig.Valid(rootDomain) { + d.addDomain(rootDomain, group, project, &domainConfig) } } return } -func (d domains) readProject(group, project string) error { +func (d domains) readProject(rootDomain, group, project string) error { if strings.HasPrefix(project, ".") { return errors.New("hidden project") } @@ -63,11 +63,11 @@ func (d domains) readProject(group, project string) error { return errors.New("missing public/ in project") } - d.readProjectConfig(group, project) + d.readProjectConfig(rootDomain, group, project) return nil } -func (d domains) readProjects(group string) (count int) { +func (d domains) readProjects(rootDomain, group string) (count int) { projects, err := os.Open(group) if err != nil { return @@ -85,7 +85,7 @@ func (d domains) readProjects(group string) (count int) { continue } - err := d.readProject(group, project.Name()) + err := d.readProject(rootDomain, group, project.Name()) if err == nil { count++ } @@ -93,7 +93,7 @@ func (d domains) readProjects(group string) (count int) { return } -func (d domains) ReadGroups() error { +func (d domains) ReadGroups(rootDomain string) error { groups, err := os.Open(".") if err != nil { return err @@ -113,15 +113,15 @@ func (d domains) ReadGroups() error { continue } - count := d.readProjects(group.Name()) + count := d.readProjects(rootDomain, group.Name()) if count > 0 { - d.addDomain(group.Name(), "", nil) + d.addDomain(rootDomain, group.Name(), "", nil) } } return nil } -func watchDomains(updater domainsUpdater, interval time.Duration) { +func watchDomains(rootDomain string, updater domainsUpdater, interval time.Duration) { lastUpdate := []byte("no-update") for { @@ -137,7 +137,7 @@ func watchDomains(updater domainsUpdater, interval time.Duration) { started := time.Now() domains := make(domains) - domains.ReadGroups() + domains.ReadGroups(rootDomain) duration := time.Since(started) log.Println("Updated", len(domains), "domains in", duration) diff --git a/domains_test.go b/domains_test.go index cf439c7e..cfe93d53 100644 --- a/domains_test.go +++ b/domains_test.go @@ -15,10 +15,8 @@ const updateFile = ".update" func TestReadProjects(t *testing.T) { setUpTests() - *pagesDomain = "test.io" - d := make(domains) - err := d.ReadGroups() + err := d.ReadGroups("test.io") require.NoError(t, err) var domains []string @@ -52,7 +50,7 @@ func TestWatchDomains(t *testing.T) { setUpTests() update := make(chan domains) - go watchDomains(func(domains domains) { + go watchDomains("gitlab.io", func(domains domains) { update <- domains }, time.Microsecond * 50) @@ -3,11 +3,12 @@ package main import ( "flag" "fmt" - "io/ioutil" "log" + "path/filepath" + "io/ioutil" "net" "os" - "path/filepath" + "strings" ) // VERSION stores the information about the semantic version of application @@ -16,8 +17,6 @@ var VERSION = "dev" // REVISION stores the information about the git revision of application var REVISION = "HEAD" -var pagesDomain = flag.String("pages-domain", "gitlab-example.com", "The domain to serve static pages") - func evalSymlinks(directory string) (result string) { result, err := filepath.EvalSymlinks(directory) if err != nil { @@ -58,6 +57,7 @@ func main() { var redirectHTTP = flag.Bool("redirect-http", true, "Serve the pages under HTTP") var useHTTP2 = flag.Bool("use-http2", true, "Enable HTTP2 support") var pagesRoot = flag.String("pages-root", "shared/pages", "The directory where pages are stored") + var pagesDomain = flag.String("pages-domain", "gitlab-example.com", "The domain to serve static pages") fmt.Printf("GitLab Pages Daemon %s (%s)", VERSION, REVISION) fmt.Printf("URL: https://gitlab.com/gitlab-org/gitlab-pages") @@ -65,7 +65,7 @@ func main() { var app theApp - app.Domain = *pagesDomain + app.Domain = strings.ToLower(*pagesDomain) app.RootDir = evalSymlinks(*pagesRoot) app.RedirectHTTP = *redirectHTTP app.HTTP2 = *useHTTP2 |