-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor, mutualize and document Route object
- Loading branch information
Showing
4 changed files
with
120 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package fuego | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/getkin/kin-openapi/openapi3" | ||
) | ||
|
||
func NewRoute[T, B any](method, path string, handler any, openapi *OpenAPI, options ...func(*BaseRoute)) Route[T, B] { | ||
return Route[T, B]{ | ||
BaseRoute: NewBaseRoute(method, path, handler, openapi, options...), | ||
} | ||
} | ||
|
||
// Route is the main struct for a route in Fuego. | ||
// It contains the OpenAPI operation and other metadata. | ||
// It is a wrapper around BaseRoute, with the addition of the response and request body types. | ||
type Route[ResponseBody any, RequestBody any] struct { | ||
BaseRoute | ||
} | ||
|
||
func NewBaseRoute(method, path string, handler any, openapi *OpenAPI, options ...func(*BaseRoute)) BaseRoute { | ||
baseRoute := BaseRoute{ | ||
Method: method, | ||
Path: path, | ||
Params: make(map[string]OpenAPIParam), | ||
FullName: FuncName(handler), | ||
Operation: openapi3.NewOperation(), | ||
OpenAPI: openapi, | ||
} | ||
|
||
for _, o := range options { | ||
o(&baseRoute) | ||
} | ||
|
||
return baseRoute | ||
} | ||
|
||
// BaseRoute is the base struct for all routes in Fuego. | ||
// It contains the OpenAPI operation and other metadata. | ||
type BaseRoute struct { | ||
Operation *openapi3.Operation // GENERATED OpenAPI operation, do not set manually in Register function. You can change it after the route is registered. | ||
Method string // HTTP method (GET, POST, PUT, PATCH, DELETE) | ||
Path string // URL path. Will be prefixed by the base path of the server and the group path if any | ||
Handler http.Handler // handler executed for this route | ||
FullName string // namespace and name of the function to execute | ||
Params map[string]OpenAPIParam | ||
Middlewares []func(http.Handler) http.Handler | ||
AcceptedContentTypes []string // Content types accepted for the request body. If nil, all content types (*/*) are accepted. | ||
Hidden bool // If true, the route will not be documented in the OpenAPI spec | ||
DefaultStatusCode int // Default status code for the response | ||
OpenAPI *OpenAPI // Ref to the whole OpenAPI spec | ||
|
||
overrideDescription bool // Override the default description | ||
} | ||
|
||
func (r *BaseRoute) GenerateDefaultDescription() { | ||
if r.overrideDescription { | ||
return | ||
} | ||
r.Operation.Description = DefaultDescription(r.FullName, r.Middlewares) + r.Operation.Description | ||
} | ||
|
||
func (r *BaseRoute) GenerateDefaultOperationID() { | ||
r.Operation.OperationID = r.Method + "_" + strings.ReplaceAll(strings.ReplaceAll(r.Path, "{", ":"), "}", "") | ||
} |
Oops, something went wrong.