first working version

This commit is contained in:
ston1th 2019-04-28 18:16:40 +02:00
commit de359ab415
47 changed files with 1016 additions and 2012 deletions

View file

@ -8,7 +8,9 @@ import (
"errors"
"git.giftfish.de/ston1th/docstore/pkg/core"
"git.giftfish.de/ston1th/docstore/pkg/db"
"git.giftfish.de/ston1th/docstore/pkg/fs"
"git.giftfish.de/ston1th/docstore/pkg/log"
"git.giftfish.de/ston1th/docstore/pkg/tesseract"
"git.giftfish.de/ston1th/godrop/v2"
"git.giftfish.de/ston1th/jwt/v3"
"github.com/gorilla/mux"
@ -19,7 +21,7 @@ import (
)
const (
cookieName = "gosession"
cookieName = "docstore_session"
)
type HTTPServer struct {
@ -29,8 +31,10 @@ type HTTPServer struct {
srv *http.Server
handler http.Handler
DB *db.DB
JWT *jwt.JWT
DB *db.DB
JWT *jwt.JWT
Tesseract *tesseract.Tesseract
FS *fs.Filesystem
templ map[string]*template.Template
res map[string][]byte
@ -40,14 +44,22 @@ func NewHTTPServer(cfg core.Config, l net.Listener) (srv *HTTPServer) {
srv = &HTTPServer{
Config: cfg,
listener: l,
handler: srv.buildRoutes(),
}
srv.handler = srv.buildRoutes()
srv.loadTemplates()
srv.srv = &http.Server{}
return
}
func (s *HTTPServer) Start() (err error) {
s.FS, err = fs.NewFilesystem(s.Config.DataDir)
if err != nil {
return errors.New("fs: " + err.Error())
}
s.Tesseract, err = tesseract.NewTesseract(s.Config.DataDir, nil, time.Second*30)
if err != nil {
return errors.New("tesseract: " + err.Error())
}
s.DB, err = db.New(s.Config)
if err != nil {
return errors.New("db: " + err.Error())
@ -65,6 +77,7 @@ func (s *HTTPServer) Start() (err error) {
return errors.New("server: " + err.Error())
}
if !s.DB.UserExists() {
log.Debug("server: starting register handler")
s.srv.Handler = s.register()
} else {
s.srv.Handler = s.handler
@ -84,29 +97,28 @@ func (s *HTTPServer) Stop() error {
}
func (s *HTTPServer) register() http.Handler {
m := mux.NewRouter()
m.NotFoundHandler = &registerNotFoundHandler{s}
for _, v := range register {
m.HandleFunc(v.Path, s.contextWrapper(v.Handler)).Methods(v.Methods...)
r := mux.NewRouter()
r.NotFoundHandler = &registerNotFoundHandler{s}
for _, v := range registerRoutes {
r.HandleFunc(v.Path, s.contextWrapper(v.Handler)).Methods(v.Methods...)
}
return m
return r
}
func (s *HTTPServer) buildRoutes() http.Handler {
m := mux.NewRouter()
m.NotFoundHandler = &notFoundHandler{s}
r := mux.NewRouter()
r.NotFoundHandler = &notFoundHandler{s}
for _, v := range routes {
m.HandleFunc(v.Path, s.contextWrapper(v.Handler)).Methods(v.Methods...)
r.HandleFunc(v.Path, s.contextWrapper(v.Handler)).Methods(v.Methods...)
}
return m
for _, v := range prefixRoutes {
r.PathPrefix(v.Path).HandlerFunc(s.contextWrapper(v.Handler)).Methods(v.Methods...)
}
return r
}
func (s *HTTPServer) contextWrapper(h ctxHandler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := newContext(w, r, s)
if ctx == nil {
return
}
h(ctx)
h(newContext(w, r, s))
}
}