cabinet/web/routes.go

53 lines
1014 B
Go
Raw Normal View History

2019-10-30 15:31:07 +00:00
package web
import (
"time"
"code.chrissexton.org/cws/idea/db"
packr "github.com/gobuffalo/packr/v2"
"github.com/speps/go-hashids"
"github.com/gorilla/mux"
"github.com/rs/zerolog/log"
"github.com/stretchr/graceful"
)
type Web struct {
addr string
assetPath string
db *db.Database
salt string
h *hashids.HashID
box *packr.Box
}
2019-10-30 15:37:45 +00:00
func New(addr, assetPath string, db *db.Database, box *packr.Box) *Web {
2019-10-30 15:31:07 +00:00
w := &Web{
addr: addr,
assetPath: assetPath,
db: db,
box: box,
}
if err := db.MakeDB(); err != nil {
log.Fatal().
Err(err).
Msg("could not create database")
}
return w
}
func (web *Web) routeSetup() *mux.Router {
r := mux.NewRouter()
2019-10-30 15:37:45 +00:00
//api := r.PathPrefix("/v1/").Subrouter()
2019-10-30 15:31:07 +00:00
r.PathPrefix("/").HandlerFunc(web.indexHandler("/index.html"))
return r
}
func (web *Web) Serve() {
middle := web.routeSetup()
log.Info().Str("addr", "http://"+web.addr).Msg("serving HTTP")
graceful.Run(web.addr, 10*time.Second, middle)
}