diff options
author | xengineering <me@xengineering.eu> | 2024-10-22 21:17:47 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2024-10-22 21:53:30 +0200 |
commit | dbc874e2b9f300bea7a18deda32e784afb0ab89a (patch) | |
tree | 88c84964765eaf71944fcdfb42a364176984fbd9 /server.go | |
parent | 38115772ff49bffe6d428c857ca345ee1087a5e7 (diff) | |
download | ceres-dbc874e2b9f300bea7a18deda32e784afb0ab89a.tar ceres-dbc874e2b9f300bea7a18deda32e784afb0ab89a.tar.zst ceres-dbc874e2b9f300bea7a18deda32e784afb0ab89a.zip |
Remove github.com/gorilla/mux dependency
The reason for the introduction of this dependency was that it was easy
to setup routes with HTTP method restrictions.
Since Go 1.22 this feature is part of the standard library. Method
restrictions are part of the patterns used to register routes [1].
[1]: https://pkg.go.dev/net/http#hdr-Patterns-ServeMux
Diffstat (limited to 'server.go')
-rw-r--r-- | server.go | 30 |
1 files changed, 12 insertions, 18 deletions
@@ -9,8 +9,6 @@ import ( "xengineering.eu/ceres/controller" "xengineering.eu/ceres/model" "xengineering.eu/ceres/view" - - "github.com/gorilla/mux" ) type Server http.Server @@ -19,31 +17,27 @@ type Server http.Server var static embed.FS func NewServer(addr string, db *model.DB) *Server { - var r *mux.Router = mux.NewRouter() - - r.PathPrefix("/static/"). - Handler(http.StripPrefix("/static/", http.FileServer(http.FS(static)))) + mux := http.NewServeMux() - r.HandleFunc("/version", view.VersionRead(version)).Methods(`GET`) + mux.Handle("GET /static/", http.StripPrefix("/static/", http.FileServer(http.FS(static)))) - r.HandleFunc("/recipes", view.RecipesRead(db)).Methods(`GET`) - r.HandleFunc("/recipe/create", view.RecipeCreate).Methods(`GET`) + mux.HandleFunc("GET /version", view.VersionRead(version)) - r.HandleFunc("/recipe", controller.RecipeCreate(db)).Methods(`POST`) - r.HandleFunc("/recipe/{id:[0-9]+}", view.RecipeRead(db)).Methods(`GET`) - r.HandleFunc("/recipe/{id:[0-9]+}", controller.RecipeUpdate(db)).Methods(`POST`) - r.HandleFunc("/recipe/{id:[0-9]+}", controller.RecipeDelete(db)).Methods(`DELETE`) + mux.HandleFunc("GET /recipes", view.RecipesRead(db)) - r.HandleFunc("/favicon.ico", view.FaviconRead).Methods(`GET`) + mux.HandleFunc("GET /recipe/create", view.RecipeCreate) + mux.HandleFunc("POST /recipe", controller.RecipeCreate(db)) + mux.HandleFunc("GET /recipe/{id}", view.RecipeRead(db)) + mux.HandleFunc("POST /recipe/{id}", controller.RecipeUpdate(db)) + mux.HandleFunc("DELETE /recipe/{id}", controller.RecipeDelete(db)) - r.HandleFunc("/", view.IndexRead).Methods(`GET`) + mux.HandleFunc("GET /favicon.ico", view.FaviconRead) - muxer := http.NewServeMux() - muxer.Handle("/", r) + mux.HandleFunc("GET /", view.IndexRead) var srv http.Server srv.Addr = addr - srv.Handler = muxer + srv.Handler = mux log.Printf("Configured server to listen on http://%s\n", srv.Addr) |