From 1c09bb0a7143ac54834f64a4cb3c084dce731207 Mon Sep 17 00:00:00 2001 From: Daan Sprenkels Date: Mon, 27 Jul 2020 14:22:43 +0200 Subject: [PATCH] Fix incorrect router setup --- handlers_test.go | 4 +++- router.go | 38 ++++++++++++++++++-------------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/handlers_test.go b/handlers_test.go index c0e2381..1a35fd2 100644 --- a/handlers_test.go +++ b/handlers_test.go @@ -57,7 +57,9 @@ func createTemporaryRouter(t *testing.T) (*mux.Router, *rushlink) { fs: fileStore, rootURL: rootURL, } - return CreateMainRouter(&rl), &rl + r := mux.NewRouter() + InitMainRouter(r, &rl) + return r, &rl } // checkStatusCode checks whether the status code from a recorded response is equal diff --git a/router.go b/router.go index e4e6098..0cad611 100644 --- a/router.go +++ b/router.go @@ -86,30 +86,28 @@ func (w *statusResponseWriter) WriteHeader(statusCode int) { w.Inner.WriteHeader(statusCode) } -// CreateMainRouter creates the main Gorilla router for the application. +// InitMainRouter creates the main Gorilla router for the application. // // 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() - 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") - router.HandleFunc("/", rl.indexGetHandler).Methods("GET", "HEAD") - router.HandleFunc("/", rl.newPasteHandler).Methods("POST") - router.HandleFunc("/"+urlKeyExpr, rl.viewPasteHandler).Methods("GET", "HEAD") - router.HandleFunc("/"+urlKeyWithExtExpr, rl.viewPasteHandler).Methods("GET", "HEAD") - router.HandleFunc("/"+urlKeyExpr+"/nr", rl.viewPasteHandlerNoRedirect).Methods("GET", "HEAD") - router.HandleFunc("/"+urlKeyWithExtExpr+"/nr", rl.viewPasteHandlerNoRedirect).Methods("GET", "HEAD") - router.HandleFunc("/"+urlKeyExpr+"/meta", rl.viewPasteHandlerMeta).Methods("GET", "HEAD") - router.HandleFunc("/"+urlKeyWithExtExpr+"/meta", rl.viewPasteHandlerMeta).Methods("GET", "HEAD") - router.HandleFunc("/"+urlKeyExpr, rl.deletePasteHandler).Methods("DELETE") - router.HandleFunc("/"+urlKeyWithExtExpr, rl.deletePasteHandler).Methods("DELETE") - router.HandleFunc("/"+urlKeyExpr+"/delete", rl.deletePasteHandler).Methods("POST") - router.HandleFunc("/"+urlKeyWithExtExpr+"/delete", rl.deletePasteHandler).Methods("POST") - return router +func InitMainRouter(r *mux.Router, rl *rushlink) { + r.HandleFunc("/{path:img/"+staticFilenameExpr+"}", rl.staticGetHandler).Methods("GET", "HEAD") + r.HandleFunc("/{path:css/"+staticFilenameExpr+"}", rl.staticGetHandler).Methods("GET", "HEAD") + r.HandleFunc("/{path:js/"+staticFilenameExpr+"}", rl.staticGetHandler).Methods("GET", "HEAD") + r.HandleFunc("/", rl.indexGetHandler).Methods("GET", "HEAD") + r.HandleFunc("/", rl.newPasteHandler).Methods("POST") + r.HandleFunc("/"+urlKeyExpr, rl.viewPasteHandler).Methods("GET", "HEAD") + r.HandleFunc("/"+urlKeyWithExtExpr, rl.viewPasteHandler).Methods("GET", "HEAD") + r.HandleFunc("/"+urlKeyExpr+"/nr", rl.viewPasteHandlerNoRedirect).Methods("GET", "HEAD") + r.HandleFunc("/"+urlKeyWithExtExpr+"/nr", rl.viewPasteHandlerNoRedirect).Methods("GET", "HEAD") + r.HandleFunc("/"+urlKeyExpr+"/meta", rl.viewPasteHandlerMeta).Methods("GET", "HEAD") + r.HandleFunc("/"+urlKeyWithExtExpr+"/meta", rl.viewPasteHandlerMeta).Methods("GET", "HEAD") + r.HandleFunc("/"+urlKeyExpr, rl.deletePasteHandler).Methods("DELETE") + r.HandleFunc("/"+urlKeyWithExtExpr, rl.deletePasteHandler).Methods("DELETE") + r.HandleFunc("/"+urlKeyExpr+"/delete", rl.deletePasteHandler).Methods("POST") + r.HandleFunc("/"+urlKeyWithExtExpr+"/delete", rl.deletePasteHandler).Methods("POST") } // StartMainServer starts the main http server listening on addr. @@ -131,7 +129,7 @@ func StartMainServer(addr string, db *db.Database, fs *db.FileStore, rawRootURL router := mux.NewRouter() router.Use(rl.metricsMiddleware) router.Use(rl.recoveryMiddleware) - router.Handle("/", CreateMainRouter(&rl)) + InitMainRouter(router, &rl) srv := &http.Server{ Handler: router,