diff options
author | xengineering <me@xengineering.eu> | 2023-02-09 20:52:36 +0100 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-02-09 21:51:48 +0100 |
commit | 4f0c5b2e5eb1277e6b5a6ba347ce08fd34cf1326 (patch) | |
tree | 8035c01035c8737aa883097f4f66a3b468691ff9 /mux.go | |
parent | 5f5e7e9bce0e463d761a26994766853979ca7ca2 (diff) | |
download | ceres-4f0c5b2e5eb1277e6b5a6ba347ce08fd34cf1326.tar ceres-4f0c5b2e5eb1277e6b5a6ba347ce08fd34cf1326.tar.zst ceres-4f0c5b2e5eb1277e6b5a6ba347ce08fd34cf1326.zip |
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.
Diffstat (limited to 'mux.go')
-rw-r--r-- | mux.go | 89 |
1 files changed, 89 insertions, 0 deletions
@@ -0,0 +1,89 @@ + +package main + +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 recipeMux(db *Database, templateRoot string) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case "GET": + recipeGet(w, r, db, templateRoot) + case "POST": + recipePost(w, r, db) + default: + http.Error(w, "Bad Request", 400) + } + } +} + +func recipeEditMux(db *Database, templateRoot string) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case "GET": + recipeEditGet(w, r, db, templateRoot) + case "POST": + recipeEditPost(w, r, db) + default: + http.Error(w, "Bad Request", 400) + } + } +} + +func recipeImageMux(storage string) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case "GET": + recipeImageGet(w, r, storage) + default: + http.Error(w, "Bad Request", 400) + } + } +} + +func addRecipesMux(db *Database, storage string, static string) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case "GET": + addRecipesGet(w, r, static) + case "POST": + addRecipesPost(w, r, db) + default: + http.Error(w, "Bad Request", 400) + } + } +} + +func staticStyleMux(file string, static string) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case "GET": + staticGet(w, r, file, static) + default: + http.Error(w, "Bad Request", 400) + } + } +} + +func faviconMux(file string, static string) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case "GET": + staticGet(w, r, file, static) + default: + http.Error(w, "Bad Request", 400) + } + } +} |