Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
// ServerConfig is configuration for server objects.
type ServerConfig struct {
StaticDir string
StaticDirs []string
Addr string
Port int
CookieSecret string
Expand All @@ -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{}
Expand All @@ -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 {
Expand Down Expand Up @@ -244,26 +255,28 @@ 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(s.Config.StaticDirs) > 0 {
for _, staticDir := range s.Config.StaticDirs {
staticFile := path.Join(staticDir, name)
if fileExists(staticFile) {
http.ServeFile(w, req, staticFile)
return true
}
}
}
for _, staticDir := range defaultStaticDirs {
staticFile := path.Join(staticDir, name)
if fileExists(staticFile) {
http.ServeFile(w, req, staticFile)
return true
}
}

return false
}

Expand Down