This file contains hidden or 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
package loggerfx | |
// imports | |
// ProvideLogger to fx | |
func ProvideLogger() *zap.SugaredLogger { | |
logger, _ := zap.NewProduction() | |
slogger := logger.Sugar() | |
return slogger |
This file contains hidden or 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
... | |
// ProvideLogger to fx | |
func ProvideLogger() *zap.SugaredLogger { | |
logger, _ := zap.NewProduction() | |
slogger := logger.Sugar() | |
return slogger | |
} | |
func main() { |
This file contains hidden or 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
... | |
func main() { | |
fx.New( | |
configfx.Module, | |
... | |
).Run() | |
} |
This file contains hidden or 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
// example continued | |
// Module provided to fx | |
var Module = fx.Options( | |
fx.Provide(ProvideConfig), | |
) |
This file contains hidden or 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
package configfx | |
// imports | |
// ApplicationConfig ... | |
type ApplicationConfig struct { | |
Address string `yaml:"address"` | |
} | |
// Config ... |
This file contains hidden or 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
... | |
// ProvideConfig provides the standard configuration to fx | |
func ProvideConfig() *Config { | |
conf := Config{} | |
data, err := ioutil.ReadFile("config/base.yaml") | |
// handle error | |
err = yaml.Unmarshal([]byte(data), &conf) | |
// handle error |
This file contains hidden or 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
... | |
func main() { | |
fx.New( | |
... | |
fx.Invoke(registerHooks), | |
).Run() | |
} | |
func registerHooks( | |
lifecycle fx.Lifecycle, |
This file contains hidden or 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
... | |
// New http handler | |
func New(s *http.ServeMux, logger *zap.SugaredLogger) *Handler { | |
h := Handler{s, logger} | |
h.registerRoutes() | |
return &h | |
} | |
... |
This file contains hidden or 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
... | |
func main() { | |
fx.New( | |
fx.Provide(ProvideConfig), | |
fx.Provide(ProvideLogger), | |
fx.Provide(http.NewServeMux), | |
fx.Invoke(httphandler.New), | |
).Run() | |
} |
This file contains hidden or 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
... | |
func main() { | |
fx.New( | |
fx.Provide(ProvideConfig), | |
fx.Provide(ProvideLogger), | |
fx.Provide(http.NewServeMux), | |
).Run() | |
... | |
} |