Fix error handling in renderStatic

Fixes #60
This commit is contained in:
Daan Sprenkels
2020-05-30 17:53:46 +02:00
parent 0b2297a2e8
commit d37222f82a
3 changed files with 21 additions and 11 deletions

View File

@@ -82,12 +82,13 @@ func (w *statusResponseWriter) WriteHeader(statusCode int) {
}
// CreateMainRouter creates the main Gorilla router for the application.
func CreateMainRouter(rl *rushlink, debug bool) *mux.Router {
//
// This function will not populate the router with an error-recovery and
// metrics-reporting middleware. If these middleware are required, then the
// caller should encapsulate this router inside of another router and register
// the middlewares on the encapsulating router.
func CreateMainRouter(rl *rushlink) *mux.Router {
router := mux.NewRouter()
if !debug {
router.Use(rl.recoveryMiddleware)
router.Use(rl.metricsMiddleware)
}
router.HandleFunc("/{path:img/"+staticFilenameExpr+"}", rl.staticGetHandler).Methods("GET", "HEAD")
router.HandleFunc("/{path:css/"+staticFilenameExpr+"}", rl.staticGetHandler).Methods("GET", "HEAD")
router.HandleFunc("/{path:js/"+staticFilenameExpr+"}", rl.staticGetHandler).Methods("GET", "HEAD")
@@ -122,8 +123,13 @@ func StartMainServer(addr string, db *db.Database, fs *db.FileStore, rawRootURL
rootURL: rootURL,
}
router := mux.NewRouter()
router.Use(rl.metricsMiddleware)
router.Use(rl.recoveryMiddleware)
router.Handle("/", CreateMainRouter(&rl))
srv := &http.Server{
Handler: CreateMainRouter(&rl, false),
Handler: router,
Addr: addr,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,