package main import ( "context" "fmt" "github.com/go-kit/log/level" sr "tildegit.org/tjp/sliderule" "tildegit.org/tjp/sliderule/contrib/cgi" "tildegit.org/tjp/sliderule/contrib/fs" "tildegit.org/tjp/sliderule/logging" "tildegit.org/tjp/sliderule/nex" ) func buildNexServer(server Server, config *Configuration) (sr.Server, error) { addr := fmt.Sprintf("%s:%d", server.IP.String(), server.Port) baselog := BaseLogger(config) info := level.Info(baselog) errlog := level.Error(baselog) if server.TLS != nil { return nex.NewTLSServer( context.Background(), server.Hostnames[0], "tcp", addr, logging.LogRequests(info)(routes(server)), errlog, server.TLS, ) } else { return nex.NewServer( context.Background(), server.Hostnames[0], "tcp", addr, logging.LogRequests(info)(routes(server)), errlog, ) } } func addNexRoute(router *sr.Router, route RouteDirective) { switch route.Type { case "static": addNexStaticRoute(router, route) case "cgi": buildAndAddRoute(router, route, func(route RouteDirective) sr.Handler { return cgi.NexCGIDirectory(route.FsPath, route.URLPath, route.Modifiers.ExecCmd) }) } } func addNexStaticRoute(router *sr.Router, route RouteDirective) { buildAndAddRoute(router, route, func(route RouteDirective) sr.Handler { handlers := []sr.Handler{} if route.Modifiers.Exec { handlers = append(handlers, cgi.NexCGIDirectory(route.FsPath, route.URLPath, route.Modifiers.ExecCmd)) } handlers = append(handlers, fs.NexFileHandler(route.FsPath, route.URLPath)) if route.Modifiers.DirDefault != "" { handlers = append( handlers, fs.NexDirectoryDefault(route.FsPath, route.URLPath, route.Modifiers.DirDefault), ) } if route.Modifiers.DirList { handlers = append(handlers, fs.NexDirectoryListing(route.FsPath, route.URLPath, nil)) } return sr.FallthroughHandler(handlers...) }) }