74 lines
1.8 KiB
Go
74 lines
1.8 KiB
Go
//+build wireinject
|
|
|
|
package web
|
|
|
|
import (
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/go-chi/chi"
|
|
"github.com/gobuffalo/packr/v2"
|
|
"github.com/google/wire"
|
|
"github.com/ollien/updown/config"
|
|
logHandler "github.com/ollien/updown/handler/log"
|
|
"github.com/ollien/updown/handler/paste"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// initWebserver is generated by wire. Please see wire_gen.go for the generated implementation
|
|
func initWebserver() *Webserver {
|
|
wire.Build(
|
|
provideWebserver,
|
|
provideHTTPServer,
|
|
provideRouter,
|
|
config.Get,
|
|
paste.NewService,
|
|
logHandler.Get,
|
|
provideLogWriter,
|
|
provideStaticFileHandler,
|
|
provideTemplateBox,
|
|
)
|
|
return &Webserver{}
|
|
}
|
|
|
|
func provideHTTPServer(appConfig config.Config, router chi.Router, logWriter *io.PipeWriter) *http.Server {
|
|
return &http.Server{
|
|
Addr: appConfig.Server.ListenAddr + ":" + strconv.Itoa(appConfig.Server.Port),
|
|
Handler: router,
|
|
ErrorLog: log.New(logWriter, "", 0),
|
|
}
|
|
}
|
|
|
|
func provideWebserver(httpServer *http.Server, router chi.Router, pasteService *paste.Service, logger *logrus.Logger, logWriter *io.PipeWriter, staticFileHandler http.FileSystem, templateBox *packr.Box) *Webserver {
|
|
webserver := Webserver{
|
|
httpServer: httpServer,
|
|
router: router,
|
|
logger: logger,
|
|
logWriter: logWriter,
|
|
pasteService: pasteService,
|
|
staticFiles: staticFileHandler,
|
|
templates: templateBox,
|
|
}
|
|
|
|
return &webserver
|
|
}
|
|
|
|
// provideRouter allows us to use the interface type for Chi's mux, rather than the struct
|
|
func provideRouter() chi.Router {
|
|
return chi.NewRouter()
|
|
}
|
|
|
|
func provideLogWriter(logger *logrus.Logger) *io.PipeWriter {
|
|
return logger.Writer()
|
|
}
|
|
|
|
func provideStaticFileHandler() http.FileSystem {
|
|
return packr.New("./static/", "static")
|
|
}
|
|
|
|
func provideTemplateBox() *packr.Box {
|
|
return packr.New("./templates/", "templates")
|
|
}
|