Skip to content

Commit 5633e70

Browse files
Adds ordered routes to go-server router
1 parent 4089438 commit 5633e70

2 files changed

Lines changed: 28 additions & 7 deletions

File tree

modules/openapi-generator/src/main/resources/go-server/controller-api.mustache

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,33 @@ func (c *{{classname}}Controller) Routes() Routes {
6666
{{#operations}}
6767
{{#operation}}
6868
"{{operationId}}": Route{
69+
"{{operationId}}",
6970
strings.ToUpper("{{httpMethod}}"),
7071
"{{{basePathWithoutHost}}}{{{path}}}",
7172
c.{{operationId}},
7273
},
7374
{{/operation}}
7475
{{/operations}}
7576
}
76-
}{{#operations}}{{#operation}}
77+
}
78+
79+
// OrderedRoutes returns all the api routes in a deterministic order for the {{classname}}Controller
80+
func (c *{{classname}}Controller) OrderedRoutes() []Route {
81+
return []Route{
82+
{{#operations}}
83+
{{#operation}}
84+
Route{
85+
"{{operationId}}",
86+
strings.ToUpper("{{httpMethod}}"),
87+
"{{{basePathWithoutHost}}}{{{path}}}",
88+
c.{{operationId}},
89+
},
90+
{{/operation}}
91+
{{/operations}}
92+
}
93+
}
94+
95+
{{#operations}}{{#operation}}
7796

7897
// {{nickname}} - {{{summary}}}
7998
{{#isDeprecated}}

modules/openapi-generator/src/main/resources/go-server/routers.mustache

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ import (
2121

2222
// A Route defines the parameters for an api endpoint
2323
type Route struct {
24-
Method string
25-
Pattern string
24+
Name string
25+
Method string
26+
Pattern string
2627
HandlerFunc http.HandlerFunc
2728
}
2829

@@ -32,6 +33,7 @@ type Routes map[string]Route
3233
// Router defines the required methods for retrieving api routes
3334
type Router interface {
3435
Routes() Routes
36+
OrderedRoutes() []Route
3537
}
3638

3739
// NewRouter creates a new router for any number of api routers
@@ -49,19 +51,19 @@ func NewRouter(routers ...Router) {{#routers}}{{#mux}}*mux.Router{{/mux}}{{#chi}
4951
{{/chi}}
5052
{{/routers}}
5153
for _, api := range routers {
52-
for {{#routers}}{{#mux}}name{{/mux}}{{#chi}}_{{/chi}}{{/routers}}, route := range api.Routes() {
54+
for _, route := range api.OrderedRoutes() {
5355
var handler http.Handler = route.HandlerFunc
5456
{{#routers}}
5557
{{#mux}}
56-
handler = Logger(handler, name)
58+
handler = Logger(handler, route.Name)
5759
{{#featureCORS}}
5860
handler = handlers.CORS()(handler)
5961
{{/featureCORS}}
6062

6163
router.
6264
Methods(route.Method).
6365
Path(route.Pattern).
64-
Name(name).
66+
Name(route.Name).
6567
Handler(handler)
6668
{{/mux}}
6769
{{#chi}}
@@ -72,4 +74,4 @@ func NewRouter(routers ...Router) {{#routers}}{{#mux}}*mux.Router{{/mux}}{{#chi}
7274
}
7375

7476
return router
75-
}
77+
}

0 commit comments

Comments
 (0)