Add options for themes and templates
This commit is contained in:
parent
6ed95d15cf
commit
6a63c7cc76
|
@ -7,6 +7,7 @@
|
||||||
# Folders
|
# Folders
|
||||||
_obj
|
_obj
|
||||||
_test
|
_test
|
||||||
|
.idea
|
||||||
|
|
||||||
# Architecture specific extensions/prefixes
|
# Architecture specific extensions/prefixes
|
||||||
*.[568vq]
|
*.[568vq]
|
||||||
|
|
43
server.go
43
server.go
|
@ -1,21 +1,20 @@
|
||||||
package snap
|
package snap
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gorilla/mux"
|
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"path"
|
|
||||||
|
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
|
||||||
"git.thirdmartini.com/pub/fancylog"
|
"git.thirdmartini.com/pub/fancylog"
|
||||||
"git.thirdmartini.com/pub/snap/auth"
|
"git.thirdmartini.com/pub/snap/auth"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
address string
|
address string
|
||||||
path string
|
path string
|
||||||
|
@ -23,16 +22,16 @@ type Server struct {
|
||||||
debug bool
|
debug bool
|
||||||
auth auth.AuthManager
|
auth auth.AuthManager
|
||||||
router *mux.Router
|
router *mux.Router
|
||||||
|
templates string
|
||||||
cachedTmpl *template.Template
|
cachedTmpl *template.Template
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
type SnapBaseContent struct {
|
type SnapBaseContent struct {
|
||||||
Theme string
|
Theme string
|
||||||
Content interface{}
|
Content interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) makeContext( auth *auth.AuthData, w http.ResponseWriter, r *http.Request ) (*Context) {
|
func (s *Server) makeContext(auth *auth.AuthData, w http.ResponseWriter, r *http.Request) *Context {
|
||||||
c := &Context{
|
c := &Context{
|
||||||
r: r,
|
r: r,
|
||||||
w: w,
|
w: w,
|
||||||
|
@ -43,23 +42,16 @@ func (s *Server) makeContext( auth *auth.AuthData, w http.ResponseWriter, r *htt
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (s *Server) plain(f func(c *Context)) http.HandlerFunc {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
c := s.makeContext( nil, w,r)
|
|
||||||
f(c)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
func (s *Server) authenticated(auth auth.AuthManager, handle func(c *Context)) http.HandlerFunc {
|
func (s *Server) authenticated(auth auth.AuthManager, handle func(c *Context)) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
rec, ok := auth.DoAuth( w, r )
|
log.Debug("authenticated request: ", r.RequestURI)
|
||||||
|
|
||||||
|
rec, ok := auth.DoAuth(w, r)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
http.Error(w, "Not authorized", 401)
|
http.Error(w, "Not authorized", 401)
|
||||||
} else {
|
} else {
|
||||||
c := s.makeContext( rec, w,r)
|
c := s.makeContext(rec, w, r)
|
||||||
handle(c)
|
handle(c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -67,16 +59,16 @@ func (s *Server) authenticated(auth auth.AuthManager, handle func(c *Context)) h
|
||||||
|
|
||||||
func (s *Server) wrapper(handle func(c *Context)) http.HandlerFunc {
|
func (s *Server) wrapper(handle func(c *Context)) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
c := s.makeContext( nil, w,r)
|
log.Debug("request: ", r.RequestURI)
|
||||||
|
c := s.makeContext(nil, w, r)
|
||||||
handle(c)
|
handle(c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (s *Server) loadTemplates() *template.Template {
|
func (s *Server) loadTemplates() *template.Template {
|
||||||
tmpl := template.New("")
|
tmpl := template.New("")
|
||||||
|
|
||||||
err := filepath.Walk(path.Join(s.path, "templates"), func(path string, info os.FileInfo, err error) error {
|
err := filepath.Walk(path.Join(s.path, s.templates), func(path string, info os.FileInfo, err error) error {
|
||||||
if strings.Contains(path, ".html") {
|
if strings.Contains(path, ".html") {
|
||||||
_, err = tmpl.ParseFiles(path)
|
_, err = tmpl.ParseFiles(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -104,7 +96,7 @@ func (s *Server) getTemplates() *template.Template {
|
||||||
return s.cachedTmpl
|
return s.cachedTmpl
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) render(w http.ResponseWriter, tmpl string, content interface{} ) {
|
func (s *Server) render(w http.ResponseWriter, tmpl string, content interface{}) {
|
||||||
s.getTemplates().ExecuteTemplate(w, tmpl, content)
|
s.getTemplates().ExecuteTemplate(w, tmpl, content)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,7 +109,6 @@ func (s *Server) renderError(w http.ResponseWriter, code int, msg string) {
|
||||||
w.Write([]byte(msg))
|
w.Write([]byte(msg))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (s *Server) HandleFuncAuthenticated(path string, f func(c *Context)) *mux.Route {
|
func (s *Server) HandleFuncAuthenticated(path string, f func(c *Context)) *mux.Route {
|
||||||
if s.auth == nil {
|
if s.auth == nil {
|
||||||
return nil
|
return nil
|
||||||
|
@ -134,13 +125,11 @@ func (s *Server) HandleFuncCustomAuth(auth auth.AuthManager, path string, f func
|
||||||
return s.router.HandleFunc(path, s.authenticated(auth, f))
|
return s.router.HandleFunc(path, s.authenticated(auth, f))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (s *Server) HandleFunc(path string, f func(c *Context)) error {
|
func (s *Server) HandleFunc(path string, f func(c *Context)) error {
|
||||||
s.router.HandleFunc(path, s.wrapper(f))
|
s.router.HandleFunc(path, s.wrapper(f))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (s *Server) SetDebug(enable bool) {
|
func (s *Server) SetDebug(enable bool) {
|
||||||
s.debug = enable
|
s.debug = enable
|
||||||
}
|
}
|
||||||
|
@ -148,11 +137,14 @@ func (s *Server) SetDebug(enable bool) {
|
||||||
func (s *Server) EnableStatus(path string) {
|
func (s *Server) EnableStatus(path string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (s *Server) SetTheme(theme string) {
|
func (s *Server) SetTheme(theme string) {
|
||||||
s.theme = theme
|
s.theme = theme
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) SetTemplatePath(path string) {
|
||||||
|
s.templates = path
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func (s *Server) ServeTLS(keyPath string, certPath string) error {
|
func (s *Server) ServeTLS(keyPath string, certPath string) error {
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
|
@ -163,7 +155,7 @@ func (s *Server) ServeTLS(keyPath string, certPath string) error {
|
||||||
ReadTimeout: 120 * time.Second,
|
ReadTimeout: 120 * time.Second,
|
||||||
}
|
}
|
||||||
|
|
||||||
return srv.ListenAndServeTLS(certPath,keyPath)
|
return srv.ListenAndServeTLS(certPath, keyPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serve serve content forever
|
// Serve serve content forever
|
||||||
|
@ -185,6 +177,7 @@ func New(address string, path string, auth auth.AuthManager) *Server {
|
||||||
auth: auth,
|
auth: auth,
|
||||||
address: address,
|
address: address,
|
||||||
path: path,
|
path: path,
|
||||||
|
templates: "templates",
|
||||||
theme: "/static/css/default.css",
|
theme: "/static/css/default.css",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue