added multiple method support for routes
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user