From fabbb6a52c221f779de77cb850a27a7d39773f3b Mon Sep 17 00:00:00 2001 From: ssobolewski Date: Sat, 10 Feb 2018 17:17:14 -0700 Subject: [PATCH] Add support for forms, error, etc --- context.go | 40 ++++++++++++++++++++++++++++++++++++++++ server.go | 21 +++++++++++++++------ 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/context.go b/context.go index f49b879..1ffa903 100644 --- a/context.go +++ b/context.go @@ -7,8 +7,13 @@ type Context struct { srv *server w http.ResponseWriter r *http.Request + vars map[string]string } +type SnapContent struct { + Username string + AppData interface{} +} func (c *Context) GetRequest() *http.Request { return c.r @@ -23,8 +28,43 @@ func (c *Context) GetUser() string { return c.Username } +func (c *Context) Error(code int, msg string) { + c.srv.renderError(c.w, code, msg) +} + + func (c *Context) Render(tmpl string, content interface{}) { c.srv.render(c.w, tmpl, content) } + +func (c *Context) RenderEx(tmpl string, content interface{}) { + cnt := SnapContent { + Username:c.Username, + AppData:content, + } + + c.srv.render(c.w, tmpl, &cnt) +} + +func (c *Context) GetVar(k string) ( string, bool) { + v,ok := c.vars[k] + return v, ok +} + +func (c *Context) GetVarDefault(k string,def string) (string) { + v,ok := c.vars[k] + if !ok { + return def + } + return v +} + +func (c *Context) ParseForm() error { + return c.r.ParseForm() +} + +func (c *Context) FormValue(k string) string { + return c.r.FormValue(k) +} \ No newline at end of file diff --git a/server.go b/server.go index 9ef7c34..1f4ee7b 100644 --- a/server.go +++ b/server.go @@ -9,7 +9,7 @@ import ( "strings" "time" "path" - "fmt" + "git.thirdmartini.com/pub/fancylog" "git.thirdmartini.com/pub/snap/auth" @@ -20,7 +20,7 @@ type Server interface { ServeTLS(keyPath string, certPath string) error SetDebug(enable bool) HandleFunc(path string, f func(c *Context)) error - HandleFuncAuthenticated(path string, f func(c *Context)) error + HandleFuncAuthenticated(path string, f func(c *Context)) *mux.Route } type server struct { @@ -58,6 +58,7 @@ func (s *server) authenticated(handle func(c *Context)) http.HandlerFunc { r: r, w: w, srv: s, + vars: mux.Vars(r), } handle(c) } @@ -71,6 +72,7 @@ func (s *server) wrapper(handle func(c *Context)) http.HandlerFunc { r: r, w: w, srv: s, + vars: mux.Vars(r), } handle(c) } @@ -114,15 +116,22 @@ func (s *server) render(w http.ResponseWriter, tmpl string, content interface{} s.getTemplates().ExecuteTemplate(w, tmpl, content) } -func (s *server) HandleFuncAuthenticated(path string, f func(c *Context)) error { +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 fmt.Errorf("no auth manager provided") + return nil } - s.router.HandleFunc(path, s.authenticated(f)) - return nil + return s.router.HandleFunc(path, s.authenticated(f)) } + + func (s *server) HandleFunc(path string, f func(c *Context)) error { s.router.HandleFunc(path, s.wrapper(f)) return nil