Pass config to webserver by DI

master
Nick Krichevsky 2019-03-08 21:39:46 -05:00
parent 17a7bccc1d
commit 187b84f76f
4 changed files with 12 additions and 12 deletions

View File

@ -19,7 +19,7 @@ func main() {
logger.Info("Starting webserver...")
fmt.Printf("%+v\n", appConfig)
webserver := web.NewWebserver(appConfig.Server)
webserver := web.NewWebserver()
err = webserver.Start()
if err != nil {
logger.Error(err)

View File

@ -4,7 +4,6 @@ import (
"net/http"
"github.com/go-chi/chi"
"github.com/ollien/updown/config"
)
// Webserver is the main websever
@ -14,8 +13,8 @@ type Webserver struct {
}
// NewWebserver creates a new webserver and sets up all routes
func NewWebserver(serverConfig config.ServerConfig) *Webserver {
server := initWebserver(serverConfig)
func NewWebserver() *Webserver {
server := initWebserver()
server.setupRoutes()
return server

View File

@ -14,18 +14,19 @@ import (
// Injectors from wire_stubs.go:
func initWebserver(serverConfig config.ServerConfig) *Webserver {
func initWebserver() *Webserver {
configConfig := config.Get()
router := provideRouter()
server := provideHTTPServer(serverConfig, router)
server := provideHTTPServer(configConfig, router)
webserver := provideWebserver(server, router)
return webserver
}
// wire_stubs.go:
func provideHTTPServer(serverConfig config.ServerConfig, router chi.Router) *http.Server {
func provideHTTPServer(appConfig config.Config, router chi.Router) *http.Server {
return &http.Server{
Addr: serverConfig.ListenAddr + ":" + strconv.Itoa(serverConfig.Port),
Addr: appConfig.Server.ListenAddr + ":" + strconv.Itoa(appConfig.Server.Port),
Handler: router,
}
}

View File

@ -12,14 +12,14 @@ import (
)
// initWebserver is generated by wire. Please see wire_gen.go for the generated implementation
func initWebserver(serverConfig config.ServerConfig) *Webserver {
wire.Build(provideWebserver, provideHTTPServer, provideRouter)
func initWebserver() *Webserver {
wire.Build(provideWebserver, provideHTTPServer, provideRouter, config.Get)
return &Webserver{}
}
func provideHTTPServer(serverConfig config.ServerConfig, router chi.Router) *http.Server {
func provideHTTPServer(appConfig config.Config, router chi.Router) *http.Server {
return &http.Server{
Addr: serverConfig.ListenAddr + ":" + strconv.Itoa(serverConfig.Port),
Addr: appConfig.Server.ListenAddr + ":" + strconv.Itoa(appConfig.Server.Port),
Handler: router,
}
}