Move static file ServeMux to App struct

This commit is contained in:
Matt Baer 2019-06-13 18:22:18 -04:00
parent f8de8f7f21
commit ed4aacd1ac
3 changed files with 15 additions and 8 deletions

6
app.go
View File

@ -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)

View File

@ -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
}

View File

@ -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 {