Skip to content

Commit ab8b9dd

Browse files
author
Jamie Tanna
committed
sq
1 parent 54055c8 commit ab8b9dd

File tree

1 file changed

+70
-3
lines changed

1 file changed

+70
-3
lines changed

README.md

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,14 @@ func SetupHandler() {
353353

354354
<td>
355355

356-
<details><summary><code>net/http</code></summary>
356+
<details><summary><code>net/http</code>-compatible servers</summary>
357357

358-
[Chi](https://github.com/go-chi/chi) is 100% compatible with `net/http` allowing the following with code generated using `-generate chi-server`.
358+
The following servers implement handlers that are directly compatible with the standard library:
359+
360+
- gorilla/mux
361+
- Chi
362+
363+
Therefore, these can be used as-is, for instance:
359364

360365
```go
361366
type PetStoreImpl struct {}
@@ -370,9 +375,71 @@ func SetupHandler() {
370375
}
371376
```
372377

373-
Alternatively, [Gorilla](https://github.com/gorilla/mux) is also 100% compatible with `net/http` and can be generated with `-generate gorilla`.
378+
</details>
379+
380+
<details><summary>Go 1.22+ <code>net/http</code></summary>
381+
382+
As of Go 1.22, enhancements have been made to the routing of the `net/http` package in the standard library.
383+
You can use `-generate std-http` to generate functions to help you associate your handlers with the auto-generated code.
384+
For the pet store, it looks like this:
385+
386+
```go
387+
// HandlerWithOptions creates http.Handler with additional options
388+
func HandlerWithOptions(si ServerInterface, options StdHTTPServerOptions) http.Handler {
389+
m := options.BaseRouter
390+
391+
if m == nil {
392+
m = http.NewServeMux()
393+
}
394+
if options.ErrorHandlerFunc == nil {
395+
options.ErrorHandlerFunc = func(w http.ResponseWriter, r *http.Request, err error) {
396+
http.Error(w, err.Error(), http.StatusBadRequest)
397+
}
398+
}
399+
400+
wrapper := ServerInterfaceWrapper{
401+
Handler: si,
402+
HandlerMiddlewares: options.Middlewares,
403+
ErrorHandlerFunc: options.ErrorHandlerFunc,
404+
}
405+
406+
m.HandleFunc("GET "+options.BaseURL+"/pets", wrapper.FindPets)
407+
m.HandleFunc("POST "+options.BaseURL+"/pets", wrapper.AddPet)
408+
m.HandleFunc("DELETE "+options.BaseURL+"/pets/{id}", wrapper.DeletePet)
409+
m.HandleFunc("GET "+options.BaseURL+"/pets/{id}", wrapper.FindPetByID)
410+
411+
return m
412+
}
413+
```
414+
415+
The wrapper functions referenced above contain generated code which pulls parameters off the request and unmarshals them into Go objects.
416+
417+
You would register the generated handlers as follows:
418+
419+
```go
420+
type PetStoreImpl struct {}
421+
func (*PetStoreImpl) GetPets(w http.ResponseWriter, r *http.Request) {
422+
// Implement me
423+
}
424+
425+
func SetupHandler() {
426+
var myApi PetStoreImpl
427+
428+
options := petstore.StdHTTPServerOptions{
429+
BaseRouter: http.DefaultServeMux, // Or use a new ServeMux
430+
}
431+
petstore.HandlerWithOptions(&myApi, options)
432+
}
433+
```
434+
435+
**Note** that if you feel like you've done everything right, but are still receiving `404 page not found` errors, make sure that you've got the `go` directive in your `go.mod` updated to:
436+
437+
```go.mod
438+
go 1.22
439+
```
374440

375441
</details>
442+
376443
</td>
377444
</tr>
378445

0 commit comments

Comments
 (0)