make serving files as optional feature.

This commit is contained in:
Miroslav Šedivý 2020-11-01 17:40:22 +01:00
parent 7fb977faa4
commit d78f98e78e
2 changed files with 11 additions and 9 deletions

View File

@ -30,7 +30,7 @@ func (Server) Init(cmd *cobra.Command) error {
return err return err
} }
cmd.PersistentFlags().String("static", "./www", "path to neko client files to serve") cmd.PersistentFlags().String("static", "", "path to neko client files to serve")
if err := viper.BindPFlag("static", cmd.PersistentFlags().Lookup("static")); err != nil { if err := viper.BindPFlag("static", cmd.PersistentFlags().Lookup("static")); err != nil {
return err return err
} }

View File

@ -40,14 +40,16 @@ func New(WebSocketManager types.WebSocketManager, ApiManager types.ApiManager, c
} }
}) })
fs := http.FileServer(http.Dir(conf.Static)) if conf.Static != "" {
router.Get("/*", func(w http.ResponseWriter, r *http.Request) { fs := http.FileServer(http.Dir(conf.Static))
if _, err := os.Stat(conf.Static + r.RequestURI); os.IsNotExist(err) { router.Get("/*", func(w http.ResponseWriter, r *http.Request) {
http.StripPrefix(r.RequestURI, fs).ServeHTTP(w, r) if _, err := os.Stat(conf.Static + r.RequestURI); os.IsNotExist(err) {
} else { http.StripPrefix(r.RequestURI, fs).ServeHTTP(w, r)
fs.ServeHTTP(w, r) } else {
} fs.ServeHTTP(w, r)
}) }
})
}
router.NotFound(endpoint.Handle(func(w http.ResponseWriter, r *http.Request) error { router.NotFound(endpoint.Handle(func(w http.ResponseWriter, r *http.Request) error {
return &endpoint.HandlerError{ return &endpoint.HandlerError{