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( | |
fx.Provide(ProvideConfig), | |
).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
... | |
// 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().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() { | |
conf := &Config{} | |
data, err := ioutil.ReadFile("config/base.yaml") | |
// handle error | |
err = yaml.Unmarshal([]byte(data), &conf) | |
// handle error | |
... | |
http.ListenAndServe(conf.ApplicationConfig.Address, mux) |
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
... | |
// ApplicationConfig ... | |
type ApplicationConfig struct { | |
Address string `yaml:"address"` | |
} | |
// Config ... | |
type Config struct { | |
ApplicationConfig `yaml:"application"` | |
} |
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
application: | |
address: :8080 |
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() { | |
logger, _ := zap.NewProduction() | |
defer logger.Sync() | |
slogger := logger.Sugar() | |
mux := http.NewServeMux() | |
httphandler.New(mux, slogger) | |
http.ListenAndServe(":8080", mux) | |
} |
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 main | |
import ( | |
"net/http" | |
"github.com/preslavmihaylov/fxappexample/httphandler" | |
) | |
func main() { | |
mux := http.NewServeMux() |
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 httphandler | |
import "net/http" | |
// Handler for http requests | |
type Handler struct { | |
mux *http.ServeMux | |
} | |
// New http handler |