snap/server.go

187 lines
3.9 KiB
Go

package snap
import (
"html/template"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/gorilla/mux"
"git.thirdmartini.com/pub/fancylog"
"git.thirdmartini.com/pub/snap/auth"
)
type Server struct {
address string
path string
theme string
debug bool
auth auth.AuthManager
router *mux.Router
templates string
cachedTmpl *template.Template
}
type SnapBaseContent struct {
Theme string
Content interface{}
}
func (s *Server) makeContext(auth *auth.AuthData, w http.ResponseWriter, r *http.Request) *Context {
c := &Context{
r: r,
w: w,
srv: s,
auth: auth,
vars: mux.Vars(r),
}
return c
}
func (s *Server) authenticated(auth auth.AuthManager, handle func(c *Context)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Debug("authenticated request: ", r.RequestURI)
rec, ok := auth.DoAuth(w, r)
if !ok {
http.Error(w, "Not authorized", 401)
} else {
c := s.makeContext(rec, w, r)
handle(c)
}
}
}
func (s *Server) wrapper(handle func(c *Context)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Debug("request: ", r.RequestURI)
c := s.makeContext(nil, w, r)
handle(c)
}
}
func (s *Server) loadTemplates() *template.Template {
tmpl := template.New("")
err := filepath.Walk(path.Join(s.path, s.templates), func(path string, info os.FileInfo, err error) error {
if strings.Contains(path, ".html") {
_, err = tmpl.ParseFiles(path)
if err != nil {
log.Println(err)
}
}
return err
})
if err != nil {
log.Fatal(err)
}
return tmpl
}
func (s *Server) getTemplates() *template.Template {
if s.debug {
return s.loadTemplates()
}
if s.cachedTmpl == nil {
s.cachedTmpl = s.loadTemplates()
}
return s.cachedTmpl
}
func (s *Server) render(w http.ResponseWriter, tmpl string, content interface{}) {
s.getTemplates().ExecuteTemplate(w, tmpl, content)
}
func (s *Server) reply(w http.ResponseWriter, msg string) {
w.Write([]byte(msg))
}
func (s *Server) renderError(w http.ResponseWriter, code int, msg string) {
w.WriteHeader(code)
w.Write([]byte(msg))
}
func (s *Server) HandleFuncAuthenticated(path string, f func(c *Context)) *mux.Route {
if s.auth == nil {
return nil
}
return s.router.HandleFunc(path, s.authenticated(s.auth, f))
}
func (s *Server) HandleFuncCustomAuth(auth auth.AuthManager, path string, f func(c *Context)) *mux.Route {
if auth == nil {
log.Warn("Nil auth on", path)
return nil
}
return s.router.HandleFunc(path, s.authenticated(auth, f))
}
func (s *Server) HandleFunc(path string, f func(c *Context)) error {
s.router.HandleFunc(path, s.wrapper(f))
return nil
}
func (s *Server) SetDebug(enable bool) {
s.debug = enable
}
func (s *Server) EnableStatus(path string) {
}
func (s *Server) SetTheme(theme string) {
s.theme = theme
}
func (s *Server) SetTemplatePath(path string) {
s.templates = path
}
func (s *Server) ServeTLS(keyPath string, certPath string) error {
srv := &http.Server{
Handler: s.router,
Addr: s.address,
// Good practice: enforce timeouts for servers you create!
WriteTimeout: 120 * time.Second,
ReadTimeout: 120 * time.Second,
}
return srv.ListenAndServeTLS(certPath, keyPath)
}
// Serve serve content forever
func (s *Server) Serve() error {
srv := &http.Server{
Handler: s.router,
Addr: s.address,
// Good practice: enforce timeouts for servers you create!
WriteTimeout: 120 * time.Second,
ReadTimeout: 120 * time.Second,
}
return srv.ListenAndServe()
}
func New(address string, path string, auth auth.AuthManager) *Server {
s := Server{
router: mux.NewRouter(),
auth: auth,
address: address,
path: path,
templates: "templates",
theme: "/static/css/default.css",
}
s.router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(s.path+"static/"))))
return &s
}