Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ help:
@echo "Targets:"
@echo " generate: regenerate all generated files"
@echo " test: run all tests"
@echo " gin_example generate gin example server code"
@echo " tidy tidy go mod"

generate:
Expand Down
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,51 @@ func SetupHandler() {
```
</summary></details>

<details><summary><code>Gin</code></summary>

Code generated using `-generate gin`.

The usage of `gin` is out of scope of this doc, but once you have an
gin instance, we generate a utility function to help you associate your handlers
with this autogenerated code. For the pet store, it looks like this:
```go
// RegisterHandlersWithOptions creates http.Handler with additional options
func RegisterHandlersWithOptions(router *gin.Engine, si ServerInterface, options GinServerOptions) *gin.Engine {
wrapper := ServerInterfaceWrapper{
Handler: si,
HandlerMiddlewares: options.Middlewares,
}

router.GET(options.BaseURL+"/pets", wrapper.FindPets)
router.POST(options.BaseURL+"/pets", wrapper.AddPet)
router.DELETE(options.BaseURL+"/pets/:id", wrapper.DeletePet)
router.GET(options.BaseURL+"/pets/:id", wrapper.FindPetByID)
return router
}
```

```go
import (
"github.com/gin-gonic/gin"
"github.com/deepmap/oapi-codegen/examples/petstore-expanded/gin/api"
middleware "github.com/deepmap/oapi-codegen/pkg/gin-middleware"
)

type PetStoreImpl struct {}
func (*PetStoreImpl) GetPets(w http.ResponseWriter, r *http.Request) {
// Implement me
}

func SetupHandler() {
var myApi PetStoreImpl

r := gin.Default()
r.Use(middleware.OapiRequestValidator(swagger))
r = api.RegisterHandlers(r, petStore)
}
```
</summary></details>

<details><summary><code>net/http</code></summary>

[Chi](https://github.com/go-chi/chi) is 100% compatible with `net/http` allowing the following with code generated using `-generate chi-server`.
Expand Down
8 changes: 5 additions & 3 deletions cmd/oapi-codegen/oapi-codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var (
flagExcludeSchemas string
flagConfigFile string
flagAliasTypes bool
flagPrintVersion bool
flagPrintVersion bool
)

type configuration struct {
Expand All @@ -63,7 +63,7 @@ func main() {

flag.StringVar(&flagPackageName, "package", "", "The package name for generated code")
flag.StringVar(&flagGenerate, "generate", "types,client,server,spec",
`Comma-separated list of code to generate; valid options: "types", "client", "chi-server", "server", "spec", "skip-fmt", "skip-prune"`)
`Comma-separated list of code to generate; valid options: "types", "client", "chi-server", "server", "gin", "spec", "skip-fmt", "skip-prune"`)
flag.StringVar(&flagOutputFile, "o", "", "Where to output generated code, stdout is default")
flag.StringVar(&flagIncludeTags, "include-tags", "", "Only include operations with the given tags. Comma-separated list of tags.")
flag.StringVar(&flagExcludeTags, "exclude-tags", "", "Exclude operations that are tagged with the given tags. Comma-separated list of tags.")
Expand Down Expand Up @@ -114,6 +114,8 @@ func main() {
opts.GenerateChiServer = true
case "server":
opts.GenerateEchoServer = true
case "gin":
opts.GenerateGinServer = true
case "types":
opts.GenerateTypes = true
case "spec":
Expand All @@ -139,7 +141,7 @@ func main() {

swagger, err := util.LoadSwagger(flag.Arg(0))
if err != nil {
errExit("error loading swagger spec\n: %s", err)
errExit("error loading swagger spec in %s\n: %s", flag.Arg(0), err)
}

templates, err := loadTemplateOverrides(cfg.TemplatesDir)
Expand Down
264 changes: 264 additions & 0 deletions examples/petstore-expanded/gin/api/petstore-server.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading