Add ability to serve static files

master
Nick Krichevsky 2019-03-16 15:01:01 -04:00
parent 22720ab00b
commit 4263faf7ab
3 changed files with 19 additions and 4 deletions

View File

@ -16,6 +16,7 @@ type Webserver struct {
logger *logrus.Logger
logWriter *io.PipeWriter // used for httpServer.ErrorLog. Must be stored separately so we can close it.
pasteService *paste.Service
staticFiles http.FileSystem
}
// NewWebserver creates a new webserver and sets up all routes
@ -38,6 +39,7 @@ func (server *Webserver) setupRoutes() {
w.Write([]byte("Coming soon..."))
})
router.Post("/paste", server.makePaste)
router.Get("/static/*", http.StripPrefix("/static", http.FileServer(server.staticFiles)).ServeHTTP)
}
func (server *Webserver) log(level logrus.Level, req *http.Request, message interface{}) {

View File

@ -7,6 +7,7 @@ package web
import (
"github.com/go-chi/chi"
"github.com/gobuffalo/packr/v2"
"github.com/ollien/updown/config"
"github.com/ollien/updown/handler/log"
"github.com/ollien/updown/handler/paste"
@ -26,7 +27,8 @@ func initWebserver() *Webserver {
pipeWriter := provideLogWriter(logger)
server := provideHTTPServer(configConfig, router, pipeWriter)
service := paste.NewService()
webserver := provideWebserver(server, router, service, logger, pipeWriter)
fileSystem := provideStaticFileHandler()
webserver := provideWebserver(server, router, service, logger, pipeWriter, fileSystem)
return webserver
}
@ -40,13 +42,14 @@ func provideHTTPServer(appConfig config.Config, router chi.Router, logWriter *io
}
}
func provideWebserver(httpServer *http.Server, router chi.Router, pasteService *paste.Service, logger *logrus.Logger, logWriter *io.PipeWriter) *Webserver {
func provideWebserver(httpServer *http.Server, router chi.Router, pasteService *paste.Service, logger *logrus.Logger, logWriter *io.PipeWriter, staticFileHandler http.FileSystem) *Webserver {
webserver := Webserver{
httpServer: httpServer,
router: router,
logger: logger,
logWriter: logWriter,
pasteService: pasteService,
staticFiles: staticFileHandler,
}
return &webserver
@ -60,3 +63,7 @@ func provideRouter() chi.Router {
func provideLogWriter(logger *logrus.Logger) *io.PipeWriter {
return logger.Writer()
}
func provideStaticFileHandler() http.FileSystem {
return packr.NewBox("./static/")
}

View File

@ -9,6 +9,7 @@ import (
"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"
@ -18,7 +19,7 @@ import (
// 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)
wire.Build(provideWebserver, provideHTTPServer, provideRouter, config.Get, paste.NewService, logHandler.Get, provideLogWriter, provideStaticFileHandler)
return &Webserver{}
}
@ -30,13 +31,14 @@ func provideHTTPServer(appConfig config.Config, router chi.Router, logWriter *io
}
}
func provideWebserver(httpServer *http.Server, router chi.Router, pasteService *paste.Service, logger *logrus.Logger, logWriter *io.PipeWriter) *Webserver {
func provideWebserver(httpServer *http.Server, router chi.Router, pasteService *paste.Service, logger *logrus.Logger, logWriter *io.PipeWriter, staticFileHandler http.FileSystem) *Webserver {
webserver := Webserver{
httpServer: httpServer,
router: router,
logger: logger,
logWriter: logWriter,
pasteService: pasteService,
staticFiles: staticFileHandler,
}
return &webserver
@ -50,3 +52,7 @@ func provideRouter() chi.Router {
func provideLogWriter(logger *logrus.Logger) *io.PipeWriter {
return logger.Writer()
}
func provideStaticFileHandler() http.FileSystem {
return packr.NewBox("./static/")
}