added multiple method support for routes

This commit is contained in:
2026-01-24 14:44:38 +11:00
parent 1c49b19197
commit 2f49063432
4 changed files with 161 additions and 10 deletions

View File

@@ -51,6 +51,12 @@ func main() {
Method: hws.MethodGET,
Handler: http.HandlerFunc(getUserHandler),
},
{
// Single route handling multiple HTTP methods
Path: "/api/resource",
Methods: []hws.Method{hws.MethodGET, hws.MethodPOST, hws.MethodPUT},
Handler: http.HandlerFunc(resourceHandler),
},
}
// Add routes and middleware
@@ -73,6 +79,18 @@ func getUserHandler(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
w.Write([]byte("User ID: " + id))
}
func resourceHandler(w http.ResponseWriter, r *http.Request) {
// Handle GET, POST, and PUT for the same path
switch r.Method {
case "GET":
w.Write([]byte("Getting resource"))
case "POST":
w.Write([]byte("Creating resource"))
case "PUT":
w.Write([]byte("Updating resource"))
}
}
```
## Documentation