diff --git a/app.go b/app.go index cbff814..abc34a3 100644 --- a/app.go +++ b/app.go @@ -65,6 +65,7 @@ var ( // App holds data and configuration for an individual WriteFreely instance. type App struct { router *mux.Router + shttp *http.ServeMux db *datastore cfg *config.Config cfgFile string @@ -195,7 +196,6 @@ func pageForReq(app *App, r *http.Request) page.StaticPage { return p } -var shttp = http.NewServeMux() var fileRegex = regexp.MustCompile("/([^/]*\\.[^/]*)$") func Serve(app *App, debug bool) { @@ -272,10 +272,6 @@ func Serve(app *App, debug bool) { initLocalTimeline(app) } - // Handle static files - fs := http.FileServer(http.Dir(filepath.Join(app.cfg.Server.StaticParentDir, staticDir))) - shttp.Handle("/", fs) - r.PathPrefix("/").Handler(fs) // Handle shutdown c := make(chan os.Signal, 2) diff --git a/posts.go b/posts.go index a7b7476..8156748 100644 --- a/posts.go +++ b/posts.go @@ -275,7 +275,7 @@ func handleViewPost(app *App, w http.ResponseWriter, r *http.Request) error { return handleTemplatedPage(app, w, r, t) } else if (strings.Contains(r.URL.Path, ".") && !isRaw && !isMarkdown) || r.URL.Path == "/robots.txt" || r.URL.Path == "/manifest.json" { // Serve static file - shttp.ServeHTTP(w, r) + app.shttp.ServeHTTP(w, r) return nil } @@ -1196,7 +1196,7 @@ func viewCollectionPost(app *App, w http.ResponseWriter, r *http.Request) error if strings.Contains(r.URL.Path, ".") && !isRaw { // Serve static file - shttp.ServeHTTP(w, r) + app.shttp.ServeHTTP(w, r) return nil } diff --git a/routes.go b/routes.go index bd0136f..f7d2451 100644 --- a/routes.go +++ b/routes.go @@ -1,5 +1,5 @@ /* - * Copyright © 2018 A Bunch Tell LLC. + * Copyright © 2018-2019 A Bunch Tell LLC. * * This file is part of WriteFreely. * @@ -17,9 +17,20 @@ import ( "github.com/writeas/writefreely/config" "github.com/writefreely/go-nodeinfo" "net/http" + "path/filepath" "strings" ) +// InitStaticRoutes adds routes for serving static files. +// TODO: this should just be a func, not method +func (app *App) InitStaticRoutes(r *mux.Router) { + // Handle static files + fs := http.FileServer(http.Dir(filepath.Join(app.cfg.Server.StaticParentDir, staticDir))) + app.shttp = http.NewServeMux() + app.shttp.Handle("/", fs) + r.PathPrefix("/").Handler(fs) +} + func initRoutes(handler *Handler, r *mux.Router, cfg *config.Config, db *datastore) { hostSubroute := cfg.App.Host[strings.Index(cfg.App.Host, "://")+3:] if cfg.App.SingleUser {