This repository has been archived by the owner on Mar 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openapi_server.gen.go
86 lines (64 loc) · 2.24 KB
/
openapi_server.gen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Package ports provides primitives to interact with the openapi HTTP API.
//
// Code generated by github.com/deepmap/oapi-codegen version v1.9.1 DO NOT EDIT.
package ports
import (
"fmt"
"net/http"
"github.com/deepmap/oapi-codegen/pkg/runtime"
"github.com/gin-gonic/gin"
)
// ServerInterface represents all server handlers.
type ServerInterface interface {
// (POST /api/v1/urls)
CreateShortUrl(c *gin.Context)
// (GET /{urlId})
RedirectToOriginUrl(c *gin.Context, urlId string)
}
// ServerInterfaceWrapper converts contexts to parameters.
type ServerInterfaceWrapper struct {
Handler ServerInterface
HandlerMiddlewares []MiddlewareFunc
}
type MiddlewareFunc func(c *gin.Context)
// CreateShortUrl operation middleware
func (siw *ServerInterfaceWrapper) CreateShortUrl(c *gin.Context) {
for _, middleware := range siw.HandlerMiddlewares {
middleware(c)
}
siw.Handler.CreateShortUrl(c)
}
// RedirectToOriginUrl operation middleware
func (siw *ServerInterfaceWrapper) RedirectToOriginUrl(c *gin.Context) {
var err error
// ------------- Path parameter "urlId" -------------
var urlId string
err = runtime.BindStyledParameter("simple", false, "urlId", c.Param("urlId"), &urlId)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"msg": fmt.Sprintf("Invalid format for parameter urlId: %s", err)})
return
}
for _, middleware := range siw.HandlerMiddlewares {
middleware(c)
}
siw.Handler.RedirectToOriginUrl(c, urlId)
}
// GinServerOptions provides options for the Gin server.
type GinServerOptions struct {
BaseURL string
Middlewares []MiddlewareFunc
}
// RegisterHandlers creates http.Handler with routing matching OpenAPI spec.
func RegisterHandlers(router *gin.Engine, si ServerInterface) *gin.Engine {
return RegisterHandlersWithOptions(router, si, GinServerOptions{})
}
// 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.POST(options.BaseURL+"/api/v1/urls", wrapper.CreateShortUrl)
router.GET(options.BaseURL+"/:urlId", wrapper.RedirectToOriginUrl)
return router
}