make the damn thing run

This commit is contained in:
Chris Sexton 2019-10-31 11:14:11 -04:00
parent bb3d76418f
commit 54b95fa587
4 changed files with 19 additions and 8 deletions

View File

@ -13,6 +13,6 @@ frontend-watch:
cd frontend && yarn build --watch cd frontend && yarn build --watch
run: *.go .EXPORT_ALL_VARIABLES run: *.go .EXPORT_ALL_VARIABLES
go build && ./happy -develop go build && ./cabinet -develop
.PHONY: run frontend frontend-watch .EXPORT_ALL_VARIABLES .PHONY: run frontend frontend-watch .EXPORT_ALL_VARIABLES

View File

@ -38,6 +38,6 @@ func main() {
Msg("could not connect to database") Msg("could not connect to database")
} }
s := web.New(*httpAddr, "pub", db, box) s := web.New(*httpAddr, db, box)
s.Serve() s.Serve()
} }

View File

@ -16,17 +16,15 @@ import (
type Web struct { type Web struct {
addr string addr string
assetPath string
db *db.Database db *db.Database
salt string salt string
h *hashids.HashID h *hashids.HashID
box *packr.Box 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{ w := &Web{
addr: addr, addr: addr,
assetPath: assetPath,
db: db, db: db,
box: box, box: box,
} }

View File

@ -1,8 +1,10 @@
package web package web
import ( import (
"mime"
"net/http" "net/http"
"path" "path"
"path/filepath"
"github.com/rs/zerolog/log" "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") log.Error().Err(err).Msg("Error finding file")
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
} }
w.Write(f) write(w, f, p)
return return
} }
if web.box.HasDir(p) && web.box.Has(path.Join(p, "index.html")) { 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") log.Error().Err(err).Msg("Error finding file")
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
} }
w.Write(f) write(w, f, p)
return return
} }
if f, err := web.box.Find(p); err != nil { if f, err := web.box.Find(p); err != nil {
w.Write(f) write(w, f, p)
return return
} }
@ -39,3 +41,14 @@ func (web *Web) indexHandler(entryPoint string) func(w http.ResponseWriter, r *h
} }
return fn 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)
}