From 989a004183269bbc53eab14e481f8346e8eb6d94 Mon Sep 17 00:00:00 2001 From: Scott Owens Date: Tue, 9 Aug 2016 12:16:13 +1000 Subject: [PATCH 1/3] made serving a static file fail over to default directories if not found. added support for multiple custom defined static directories, both in the existing string format (so as not to be breaking to existing uses) and through a new string array that caches the list --- server.go | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/server.go b/server.go index ae6b8a9f..fd30afef 100644 --- a/server.go +++ b/server.go @@ -22,6 +22,7 @@ import ( // ServerConfig is configuration for server objects. type ServerConfig struct { StaticDir string + StaticDirs []string Addr string Port int CookieSecret string @@ -48,6 +49,10 @@ func NewServer() *Server { } } +// initialises the server in the following steps +// if no configuration is provided, initialise an empty (default) configuration +// if no logger is provided create a default one +// if the config contains StaticDir data, tokenize it and add it to the array form func (s *Server) initServer() { if s.Config == nil { s.Config = &ServerConfig{} @@ -56,6 +61,12 @@ func (s *Server) initServer() { if s.Logger == nil { s.Logger = log.New(os.Stdout, "", log.Ldate|log.Ltime) } + if s.Config.StaticDir != "" { + dirs := strings.Split(s.Config.StaticDir, ",") + for _, dir := range dirs { + s.Config.StaticDirs = append(s.Config.StaticDirs, dir) + } + } } type route struct { @@ -244,19 +255,13 @@ func requiresContext(handlerType reflect.Type) bool { // tryServingFile attempts to serve a static file, and returns // whether or not the operation is successful. // It checks the following directories for the file, in order: -// 1) Config.StaticDir +// 1) Directors specified in Config.StaticDirs // 2) The 'static' directory in the parent directory of the executable. // 3) The 'static' directory in the current working directory func (s *Server) tryServingFile(name string, req *http.Request, w http.ResponseWriter) bool { //try to serve a static file - if s.Config.StaticDir != "" { - staticFile := path.Join(s.Config.StaticDir, name) - if fileExists(staticFile) { - http.ServeFile(w, req, staticFile) - return true - } - } else { - for _, staticDir := range defaultStaticDirs { + if len(c.Config.StaticDirs) > 0 { + for _, staticDir := range c.Config.StaticDirs { staticFile := path.Join(staticDir, name) if fileExists(staticFile) { http.ServeFile(w, req, staticFile) @@ -264,6 +269,14 @@ func (s *Server) tryServingFile(name string, req *http.Request, w http.ResponseW } } } + for _, staticDir := range defaultStaticDirs { + staticFile := path.Join(staticDir, name) + if fileExists(staticFile) { + http.ServeFile(w, req, staticFile) + return true + } + } + return false } From 2bb6537ea5642230be5dcb57b1fd2ff87c592d76 Mon Sep 17 00:00:00 2001 From: Scott Owens Date: Tue, 9 Aug 2016 12:35:44 +1000 Subject: [PATCH 2/3] correcting typo left from testing in other location. --- server.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server.go b/server.go index fd30afef..067dd9b4 100644 --- a/server.go +++ b/server.go @@ -260,8 +260,8 @@ func requiresContext(handlerType reflect.Type) bool { // 3) The 'static' directory in the current working directory func (s *Server) tryServingFile(name string, req *http.Request, w http.ResponseWriter) bool { //try to serve a static file - if len(c.Config.StaticDirs) > 0 { - for _, staticDir := range c.Config.StaticDirs { + if len(s.Config.StaticDirs) > 0 { + for _, staticDir := range s.Config.StaticDirs { staticFile := path.Join(staticDir, name) if fileExists(staticFile) { http.ServeFile(w, req, staticFile) From 4f9524485c75339b3f73de26848cd7e06e16145e Mon Sep 17 00:00:00 2001 From: Scott Owens Date: Tue, 9 Aug 2016 13:08:23 +1000 Subject: [PATCH 3/3] forgot to call gofmt. --- server.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/server.go b/server.go index 067dd9b4..cc8bcd1e 100644 --- a/server.go +++ b/server.go @@ -62,10 +62,10 @@ func (s *Server) initServer() { s.Logger = log.New(os.Stdout, "", log.Ldate|log.Ltime) } if s.Config.StaticDir != "" { - dirs := strings.Split(s.Config.StaticDir, ",") - for _, dir := range dirs { - s.Config.StaticDirs = append(s.Config.StaticDirs, dir) - } + dirs := strings.Split(s.Config.StaticDir, ",") + for _, dir := range dirs { + s.Config.StaticDirs = append(s.Config.StaticDirs, dir) + } } }