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
run: *.go .EXPORT_ALL_VARIABLES
go build && ./happy -develop
go build && ./cabinet -develop
.PHONY: run frontend frontend-watch .EXPORT_ALL_VARIABLES

View File

@ -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()
}

View File

@ -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,
}

View File

@ -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)
}