Fix incorrect router setup

This commit is contained in:
Daan Sprenkels 2020-07-27 14:22:43 +02:00
parent 03a04389ae
commit 1c09bb0a71
2 changed files with 21 additions and 21 deletions

View File

@ -57,7 +57,9 @@ func createTemporaryRouter(t *testing.T) (*mux.Router, *rushlink) {
fs: fileStore, fs: fileStore,
rootURL: rootURL, 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 // checkStatusCode checks whether the status code from a recorded response is equal

View File

@ -86,30 +86,28 @@ func (w *statusResponseWriter) WriteHeader(statusCode int) {
w.Inner.WriteHeader(statusCode) 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 // This function will not populate the router with an error-recovery and
// metrics-reporting middleware. If these middleware are required, then the // metrics-reporting middleware. If these middleware are required, then the
// caller should encapsulate this router inside of another router and register // caller should encapsulate this router inside of another router and register
// the middlewares on the encapsulating router. // the middlewares on the encapsulating router.
func CreateMainRouter(rl *rushlink) *mux.Router { func InitMainRouter(r *mux.Router, rl *rushlink) {
router := mux.NewRouter() r.HandleFunc("/{path:img/"+staticFilenameExpr+"}", rl.staticGetHandler).Methods("GET", "HEAD")
router.HandleFunc("/{path:img/"+staticFilenameExpr+"}", rl.staticGetHandler).Methods("GET", "HEAD") r.HandleFunc("/{path:css/"+staticFilenameExpr+"}", rl.staticGetHandler).Methods("GET", "HEAD")
router.HandleFunc("/{path:css/"+staticFilenameExpr+"}", rl.staticGetHandler).Methods("GET", "HEAD") r.HandleFunc("/{path:js/"+staticFilenameExpr+"}", rl.staticGetHandler).Methods("GET", "HEAD")
router.HandleFunc("/{path:js/"+staticFilenameExpr+"}", rl.staticGetHandler).Methods("GET", "HEAD") r.HandleFunc("/", rl.indexGetHandler).Methods("GET", "HEAD")
router.HandleFunc("/", rl.indexGetHandler).Methods("GET", "HEAD") r.HandleFunc("/", rl.newPasteHandler).Methods("POST")
router.HandleFunc("/", rl.newPasteHandler).Methods("POST") r.HandleFunc("/"+urlKeyExpr, rl.viewPasteHandler).Methods("GET", "HEAD")
router.HandleFunc("/"+urlKeyExpr, rl.viewPasteHandler).Methods("GET", "HEAD") r.HandleFunc("/"+urlKeyWithExtExpr, rl.viewPasteHandler).Methods("GET", "HEAD")
router.HandleFunc("/"+urlKeyWithExtExpr, rl.viewPasteHandler).Methods("GET", "HEAD") r.HandleFunc("/"+urlKeyExpr+"/nr", rl.viewPasteHandlerNoRedirect).Methods("GET", "HEAD")
router.HandleFunc("/"+urlKeyExpr+"/nr", rl.viewPasteHandlerNoRedirect).Methods("GET", "HEAD") r.HandleFunc("/"+urlKeyWithExtExpr+"/nr", rl.viewPasteHandlerNoRedirect).Methods("GET", "HEAD")
router.HandleFunc("/"+urlKeyWithExtExpr+"/nr", rl.viewPasteHandlerNoRedirect).Methods("GET", "HEAD") r.HandleFunc("/"+urlKeyExpr+"/meta", rl.viewPasteHandlerMeta).Methods("GET", "HEAD")
router.HandleFunc("/"+urlKeyExpr+"/meta", rl.viewPasteHandlerMeta).Methods("GET", "HEAD") r.HandleFunc("/"+urlKeyWithExtExpr+"/meta", rl.viewPasteHandlerMeta).Methods("GET", "HEAD")
router.HandleFunc("/"+urlKeyWithExtExpr+"/meta", rl.viewPasteHandlerMeta).Methods("GET", "HEAD") r.HandleFunc("/"+urlKeyExpr, rl.deletePasteHandler).Methods("DELETE")
router.HandleFunc("/"+urlKeyExpr, rl.deletePasteHandler).Methods("DELETE") r.HandleFunc("/"+urlKeyWithExtExpr, rl.deletePasteHandler).Methods("DELETE")
router.HandleFunc("/"+urlKeyWithExtExpr, rl.deletePasteHandler).Methods("DELETE") r.HandleFunc("/"+urlKeyExpr+"/delete", rl.deletePasteHandler).Methods("POST")
router.HandleFunc("/"+urlKeyExpr+"/delete", rl.deletePasteHandler).Methods("POST") r.HandleFunc("/"+urlKeyWithExtExpr+"/delete", rl.deletePasteHandler).Methods("POST")
router.HandleFunc("/"+urlKeyWithExtExpr+"/delete", rl.deletePasteHandler).Methods("POST")
return router
} }
// StartMainServer starts the main http server listening on addr. // 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 := mux.NewRouter()
router.Use(rl.metricsMiddleware) router.Use(rl.metricsMiddleware)
router.Use(rl.recoveryMiddleware) router.Use(rl.recoveryMiddleware)
router.Handle("/", CreateMainRouter(&rl)) InitMainRouter(router, &rl)
srv := &http.Server{ srv := &http.Server{
Handler: router, Handler: router,