This question is not precise, since I think there will be multiple ways to achieve this. Let first explain the goal and then what I have tried.
I'm making a REST server in Go and I'm using Chi as the router. I can mount a route in Chi by simply calling
func HandleFoo() *chi.Mux {
r := chi.NewRouter()
r.Get("/bar/{id}", HandleBar()) // HandleBar() is hidden
return r
}
router := chi.NewRouter()
router.Mount("/foo", HandleFoo())
And it'll mount me a router in a tree structure, meaning HandleFoo()
returns a sub-router that's mounted under /foo
.
I have A LOT of routes to mount. I want some automated way for my server to mount all of the routes without me manually calling router.mount()
for each route I create.
My basic train of thought is that if I can define an type API interface{}
with a function route() *chi.Mux
, then any type that implements this route()
function will conform to this interface, and I might be able to call all of the API
's route()
function through reflection
. I may need to put all of the API
s under the same package but that's fine.
But so far I haven't found a way to do that through reflection. I found this post that shows to to discover all package types, and this post that seems to be relevant but not exactly what I'm looking for.
Is this possible?
Aucun commentaire:
Enregistrer un commentaire