Move static file ServeMux to App struct
This commit is contained in:
parent
f8de8f7f21
commit
ed4aacd1ac
6
app.go
6
app.go
@ -65,6 +65,7 @@ var (
|
|||||||
// App holds data and configuration for an individual WriteFreely instance.
|
// App holds data and configuration for an individual WriteFreely instance.
|
||||||
type App struct {
|
type App struct {
|
||||||
router *mux.Router
|
router *mux.Router
|
||||||
|
shttp *http.ServeMux
|
||||||
db *datastore
|
db *datastore
|
||||||
cfg *config.Config
|
cfg *config.Config
|
||||||
cfgFile string
|
cfgFile string
|
||||||
@ -195,7 +196,6 @@ func pageForReq(app *App, r *http.Request) page.StaticPage {
|
|||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
var shttp = http.NewServeMux()
|
|
||||||
var fileRegex = regexp.MustCompile("/([^/]*\\.[^/]*)$")
|
var fileRegex = regexp.MustCompile("/([^/]*\\.[^/]*)$")
|
||||||
|
|
||||||
func Serve(app *App, debug bool) {
|
func Serve(app *App, debug bool) {
|
||||||
@ -272,10 +272,6 @@ func Serve(app *App, debug bool) {
|
|||||||
initLocalTimeline(app)
|
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
|
// Handle shutdown
|
||||||
c := make(chan os.Signal, 2)
|
c := make(chan os.Signal, 2)
|
||||||
|
4
posts.go
4
posts.go
@ -275,7 +275,7 @@ func handleViewPost(app *App, w http.ResponseWriter, r *http.Request) error {
|
|||||||
return handleTemplatedPage(app, w, r, t)
|
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" {
|
} else if (strings.Contains(r.URL.Path, ".") && !isRaw && !isMarkdown) || r.URL.Path == "/robots.txt" || r.URL.Path == "/manifest.json" {
|
||||||
// Serve static file
|
// Serve static file
|
||||||
shttp.ServeHTTP(w, r)
|
app.shttp.ServeHTTP(w, r)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1196,7 +1196,7 @@ func viewCollectionPost(app *App, w http.ResponseWriter, r *http.Request) error
|
|||||||
|
|
||||||
if strings.Contains(r.URL.Path, ".") && !isRaw {
|
if strings.Contains(r.URL.Path, ".") && !isRaw {
|
||||||
// Serve static file
|
// Serve static file
|
||||||
shttp.ServeHTTP(w, r)
|
app.shttp.ServeHTTP(w, r)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
13
routes.go
13
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.
|
* This file is part of WriteFreely.
|
||||||
*
|
*
|
||||||
@ -17,9 +17,20 @@ import (
|
|||||||
"github.com/writeas/writefreely/config"
|
"github.com/writeas/writefreely/config"
|
||||||
"github.com/writefreely/go-nodeinfo"
|
"github.com/writefreely/go-nodeinfo"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"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) {
|
func initRoutes(handler *Handler, r *mux.Router, cfg *config.Config, db *datastore) {
|
||||||
hostSubroute := cfg.App.Host[strings.Index(cfg.App.Host, "://")+3:]
|
hostSubroute := cfg.App.Host[strings.Index(cfg.App.Host, "://")+3:]
|
||||||
if cfg.App.SingleUser {
|
if cfg.App.SingleUser {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user