diff options
author | Kamil Trzcinski <ayufan@ayufan.eu> | 2016-02-11 20:25:41 +0300 |
---|---|---|
committer | Kamil Trzcinski <ayufan@ayufan.eu> | 2016-02-11 20:25:41 +0300 |
commit | f97ebb63782cb6ac681268651a2234e942b7e90a (patch) | |
tree | 1d62c7ab28c41d9fc18db55bfda1a8fe52503828 | |
parent | a5548d71b72b050df4d708953120af5f16dde39b (diff) |
Reduce code complexity
-rw-r--r-- | domain.go | 56 | ||||
-rw-r--r-- | domain_config_test.go | 4 | ||||
-rw-r--r-- | domains.go | 57 | ||||
-rw-r--r-- | domains_test.go | 10 | ||||
-rw-r--r-- | main.go | 2 |
5 files changed, 74 insertions, 55 deletions
@@ -3,6 +3,7 @@ package main import ( "crypto/tls" "errors" + "fmt" "net/http" "os" "path/filepath" @@ -21,22 +22,41 @@ func (d *domain) notFound(w http.ResponseWriter, r *http.Request) { http.NotFound(w, r) } -func (d *domain) tryFile(w http.ResponseWriter, r *http.Request, projectName, subPath string) bool { - publicPath := filepath.Join(*pagesRoot, d.Group, projectName, "public") - fullPath := filepath.Join(publicPath, subPath) - fullPath, err := filepath.EvalSymlinks(fullPath) +func (d *domain) serveFile(w http.ResponseWriter, r *http.Request, fullPath string) bool { + // Open and serve content of file + file, err := os.Open(fullPath) if err != nil { return false } + defer file.Close() - if !strings.HasPrefix(fullPath, publicPath+"/") && fullPath != publicPath { - println("The", fullPath, "should be in", publicPath) + fi, err := file.Stat() + if err != nil { return false } + println("Serving", fullPath, "for", r.URL.Path) + http.ServeContent(w, r, filepath.Base(file.Name()), fi.ModTime(), file) + return true +} + +func (d *domain) fullPath(w http.ResponseWriter, r *http.Request, projectName, subPath string) (fullPath string, err error) { + publicPath := filepath.Join(*pagesRoot, d.Group, projectName, "public") + + fullPath = filepath.Join(publicPath, subPath) + fullPath, err = filepath.EvalSymlinks(fullPath) + if err != nil { + return + } + + if !strings.HasPrefix(fullPath, publicPath+"/") && fullPath != publicPath { + err = fmt.Errorf("%q should be in %q", fullPath, publicPath) + return + } + fi, err := os.Lstat(fullPath) if err != nil { - return false + return } // If this file is directory, open the index.html @@ -44,30 +64,24 @@ func (d *domain) tryFile(w http.ResponseWriter, r *http.Request, projectName, su fullPath = filepath.Join(fullPath, "index.html") fi, err = os.Lstat(fullPath) if err != nil { - return false + return } } // We don't allow to open non-regular files if !fi.Mode().IsRegular() { - return false - } - - // Open and serve content of file - file, err := os.Open(fullPath) - if err != nil { - return false + err = fmt.Errorf("%s: is not a regular file", fullPath) + return } - defer file.Close() + return +} - fi, err = file.Stat() +func (d *domain) tryFile(w http.ResponseWriter, r *http.Request, projectName, subPath string) bool { + fullPath, err := d.fullPath(w, r, projectName, subPath) if err != nil { return false } - - println("Serving", fullPath, "for", r.URL.Path) - http.ServeContent(w, r, filepath.Base(file.Name()), fi.ModTime(), file) - return true + return d.serveFile(w, r, fullPath) } func (d *domain) serveFromGroup(w http.ResponseWriter, r *http.Request) { diff --git a/domain_config_test.go b/domain_config_test.go index 5417fa86..d38accf8 100644 --- a/domain_config_test.go +++ b/domain_config_test.go @@ -1,9 +1,9 @@ package main import ( - "testing" - "os" "io/ioutil" + "os" + "testing" "github.com/stretchr/testify/assert" "path/filepath" @@ -2,6 +2,7 @@ package main import ( "bytes" + "errors" "io/ioutil" "log" "os" @@ -30,6 +31,34 @@ func (d domains) addDomain(group, project string, config *domainConfig) error { return nil } +func (d domains) readProject(group, project string) error { + if strings.HasPrefix(project, ".") { + return errors.New("hidden project") + } + + // Ignore projects that have .deleted in name + if strings.HasSuffix(project, ".deleted") { + return errors.New("deleted project") + } + + _, err := os.Lstat(filepath.Join(*pagesRoot, group, project, "public")) + if err != nil { + return errors.New("missing public/ in project") + } + + var config domainsConfig + err = config.Read(group, project) + log.Println(err) + if err == nil { + for _, domainConfig := range config.Domains { + if domainConfig.Valid() { + d.addDomain(group, project, &domainConfig) + } + } + } + return nil +} + func (d domains) readProjects(group string) (count int) { projects, err := os.Open(filepath.Join(*pagesRoot, group)) if err != nil { @@ -48,33 +77,9 @@ func (d domains) readProjects(group string) (count int) { continue } - // Ignore hidden projects - if strings.HasPrefix(project.Name(), ".") { - continue - } - - // Ignore projects that have .deleted in name - if strings.HasSuffix(project.Name(), ".deleted") { - continue - } - - // Ignore projects without public - _, err := os.Lstat(filepath.Join(*pagesRoot, group, project.Name(), "public")) - if err != nil { - continue - } - - count++ - - var config domainsConfig - err = config.Read(group, project.Name()) - log.Println(err) + err := d.readProject(group, project.Name()) if err == nil { - for _, domainConfig := range config.Domains { - if domainConfig.Valid() { - d.addDomain(group, project.Name(), &domainConfig) - } - } + count++ } } return diff --git a/domains_test.go b/domains_test.go index ae849f46..0406617f 100644 --- a/domains_test.go +++ b/domains_test.go @@ -1,13 +1,13 @@ package main import ( - "testing" - "github.com/stretchr/testify/require" + "crypto/rand" "github.com/stretchr/testify/assert" - "time" + "github.com/stretchr/testify/require" "io/ioutil" - "crypto/rand" "os" + "testing" + "time" ) const updateFile = "shared/pages/.update" @@ -20,7 +20,7 @@ func TestReadProjects(t *testing.T) { require.NoError(t, err) var domains []string - for domain, _ := range d { + for domain := range d { domains = append(domains, domain) } @@ -6,10 +6,10 @@ import ( "fmt" "log" "net/http" + "path/filepath" "strings" "sync" "time" - "path/filepath" ) // VERSION stores the information about the semantic version of application |