From 4f0c5b2e5eb1277e6b5a6ba347ce08fd34cf1326 Mon Sep 17 00:00:00 2001 From: xengineering Date: Thu, 9 Feb 2023 20:52:36 +0100 Subject: Migrate to multiplexer concept This introduces a layered approach to handling HTTP requests: - server layer - path layer - request layer The multiplexer file cares about the path layer. It delegates the request handling to handlers from the request layer. --- router.go | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) (limited to 'router.go') diff --git a/router.go b/router.go index 508d23d..c1f5d78 100644 --- a/router.go +++ b/router.go @@ -6,31 +6,15 @@ import ( "net/http" ) -func indexMux(db *Database, templateRoot string) func(http.ResponseWriter, *http.Request) { - return func(w http.ResponseWriter, r *http.Request) { - switch r.Method { - case "GET": - indexGet(w, r, db, templateRoot) - default: - http.Error(w, "Bad Request", 400) - } - } -} - func RunServer(config HttpConfig, db *Database) { http.HandleFunc("/", indexMux(db, config.Templates)) - - http.HandleFunc("/recipe", recipe(db, config.Templates)) - - http.HandleFunc("/recipe/edit", recipe_edit(db, config.Templates)) - - http.HandleFunc("/recipe/image", image(config.Storage)) - - http.HandleFunc("/add_recipes", add_recipes(db, config.Storage, config.Static)) - - http.HandleFunc("/static/style.css", static("style.css", config.Static)) - http.HandleFunc("/favicon.ico", static("favicon.ico", config.Static)) + http.HandleFunc("/recipe", recipeMux(db, config.Templates)) + http.HandleFunc("/recipe/edit", recipeEditMux(db, config.Templates)) + http.HandleFunc("/recipe/image", recipeImageMux(config.Storage)) + http.HandleFunc("/add_recipes", addRecipesMux(db, config.Storage, config.Static)) + http.HandleFunc("/static/style.css", staticStyleMux("style.css", config.Static)) + http.HandleFunc("/favicon.ico", faviconMux("favicon.ico", config.Static)) address := config.Host + ":" + config.Port log.Println("Binding to 'http://" + address) -- cgit v1.2.3-70-g09d2