From 54b95fa5877bcaa0f648a8f2ac438e1c38a28e3e Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Thu, 31 Oct 2019 11:14:11 -0400 Subject: [PATCH] make the damn thing run --- Makefile | 2 +- main.go | 2 +- web/routes.go | 4 +--- web/web_handlers.go | 19 ++++++++++++++++--- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 17dc299..4be27bd 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,6 @@ frontend-watch: cd frontend && yarn build --watch run: *.go .EXPORT_ALL_VARIABLES - go build && ./happy -develop + go build && ./cabinet -develop .PHONY: run frontend frontend-watch .EXPORT_ALL_VARIABLES diff --git a/main.go b/main.go index 259b094..63dfb70 100644 --- a/main.go +++ b/main.go @@ -38,6 +38,6 @@ func main() { Msg("could not connect to database") } - s := web.New(*httpAddr, "pub", db, box) + s := web.New(*httpAddr, db, box) s.Serve() } diff --git a/web/routes.go b/web/routes.go index 1d6d803..32262dc 100644 --- a/web/routes.go +++ b/web/routes.go @@ -16,17 +16,15 @@ import ( type Web struct { addr string - assetPath string db *db.Database salt string h *hashids.HashID box *packr.Box } -func New(addr, assetPath string, db *db.Database, box *packr.Box) *Web { +func New(addr string, db *db.Database, box *packr.Box) *Web { w := &Web{ addr: addr, - assetPath: assetPath, db: db, box: box, } diff --git a/web/web_handlers.go b/web/web_handlers.go index 80b7744..7ec52bd 100644 --- a/web/web_handlers.go +++ b/web/web_handlers.go @@ -1,8 +1,10 @@ package web import ( + "mime" "net/http" "path" + "path/filepath" "github.com/rs/zerolog/log" ) @@ -17,7 +19,7 @@ func (web *Web) indexHandler(entryPoint string) func(w http.ResponseWriter, r *h log.Error().Err(err).Msg("Error finding file") w.WriteHeader(http.StatusNotFound) } - w.Write(f) + write(w, f, p) return } if web.box.HasDir(p) && web.box.Has(path.Join(p, "index.html")) { @@ -26,12 +28,12 @@ func (web *Web) indexHandler(entryPoint string) func(w http.ResponseWriter, r *h log.Error().Err(err).Msg("Error finding file") w.WriteHeader(http.StatusNotFound) } - w.Write(f) + write(w, f, p) return } if f, err := web.box.Find(p); err != nil { - w.Write(f) + write(w, f, p) return } @@ -39,3 +41,14 @@ func (web *Web) indexHandler(entryPoint string) func(w http.ResponseWriter, r *h } return fn } + +func write(w http.ResponseWriter, f []byte, path string) { + ctype := mime.TypeByExtension(filepath.Ext(path)) + if ctype == "" { + ctype = http.DetectContentType(f) + } + if ctype != "" { + w.Header().Set("Content-Type", ctype) + } + w.Write(f) +}