Pass config to webserver by DI
parent
17a7bccc1d
commit
187b84f76f
2
main.go
2
main.go
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue