Using uber fx to provide an interface
Answer #1 0You're missing *http.Config
object, create a function that return that object, e.g. NewConfig()
package http
import (
"time"
"github.com/caarlos0/env/v6"
"github.com/gofiber/fiber/v2"
)
type BaseConfig interface {
GetPort() string
GetTimeout() int
}
type Config struct {
Port string `env:"LISTEN_ADDR" envDefault:":3000"`
Timeout uint64 `env:"TIMEOUT" envDefault:"10"`
}
func NewConfig() (*Config, error) {
cfg := Config{}
if err := env.Parse(&cfg); err != nil {
return nil, err
}
return &cfg, nil
}
func (c *Config) GetPort() string {
return c.Port
}
func (c *Config) GetTimeout() int {
return int(c.Timeout)
}
func CreateServer(config *Config) *fiber.App {
fiberConfig := fiber.Config{
ReadTimeout: time.Second * time.Duration(config.GetTimeout()),
WriteTimeout: time.Second * time.Duration(config.GetTimeout()),
}
app := fiber.New(fiberConfig)
// do setup and other stuff
return app
}
then change your provideOptions()
, maybe like this:
func provideOptions() []fx.Option {
return []fx.Option{
fx.Invoke(utils.ConfigureLogger),
fx.Provide(config.Parse, http.NewConfig),
fx.Invoke(controllers.SomeController),
fx.Provide(http.CreateServer),
}
}