Annotate Multiple methods that produce the same type #1031
Unanswered
UdamDewaraja
asked this question in
Q&A
Replies: 1 comment
-
@UdamDewaraja sorry for the delayed response. This looks like it has to do with the way which annotation is applied to an fx-injected parameter. You could work around this problem by "skipping" the first parameter's annotation with an empty string tag: func NewV1RoutesMux() *chi.Mux {
...
}
func NewServerMux(v1ServerMux *chi.Mux) *chi.Mux {
...
}
// NewHTTPServer builds an HTTP server that will begin serving requests
// when the Fx application starts.
func NewHTTPServer(lc fx.Lifecycle, serverMux *chi.Mux) *http.Server {
srv := &http.Server{Addr: ":8080", Handler: servermux}
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
ln, err := net.Listen("tcp", srv.Addr)
if err != nil {
log.Fatal("Error serving router")
return err
}
log.Println("Starting HTTP server at", srv.Addr)
go srv.Serve(ln)
return nil
},
OnStop: func(ctx context.Context) error {
return srv.Shutdown(ctx)
},
})
return srv
}
// main.go
func main() {
fx.New(
fx.Provide(
fx.Annotate(router.NewHTTPServer, fx.ParamTags(`name:"serverMux"`)),
fx.Annotate(router.NewServerMux, fx.ParamTags(``, `name:"v1ServerMux"`), fx.ResultTags(`name:"serverMux"`)),
fx.Annotate(router.NewV1RoutesMux, fx.ResultTags(`name:"v1ServerMux"`)),
),
fx.Invoke(func(*http.Server) {}),
).Run()
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello, would like some help on how to get started with fx. Relatively new to go as well.
I have an API server that I'm bootstrapping using go-chi. I have a method that returns a chi.Mux that feeds into another method that produces a higher-level chi.Mux which gets passed into the method that sets up the server. I've looked through many of the docs and I'm struggling to understand why this wouldn't work.
All this works if I don't use DI or partially use DI.
Methods are as follows:
The error I get is:
Any help would be much appreciated.
Beta Was this translation helpful? Give feedback.
All reactions