From a1898815681dc7dcba942f6915c5f94c0e54380a Mon Sep 17 00:00:00 2001 From: lrodham Date: Fri, 10 Nov 2017 06:54:12 +0000 Subject: [PATCH] initial commit --- context.go | 12 ++++++ default_context.go | 103 +++++++++++++++++++++++++++++++++++++++++++++ default_router.go | 31 ++++++++++++++ route.go | 24 +++++++++++ router.go | 9 ++++ tuu.go | 22 ++++++++++ 6 files changed, 201 insertions(+) create mode 100644 context.go create mode 100644 default_context.go create mode 100644 default_router.go create mode 100644 route.go create mode 100644 router.go create mode 100644 tuu.go diff --git a/context.go b/context.go new file mode 100644 index 0000000..eb41113 --- /dev/null +++ b/context.go @@ -0,0 +1,12 @@ +package tuu + +import ( + "context" + "net/http" +) + +type Context interface { + context.Context + Response() http.ResponseWriter + Request() *http.Request +} diff --git a/default_context.go b/default_context.go new file mode 100644 index 0000000..d97f908 --- /dev/null +++ b/default_context.go @@ -0,0 +1,103 @@ +package tuu + +import ( + "context" + "net/http" + "net/url" + + "github.com/gobuffalo/plush" + + "github.com/gobuffalo/buffalo/render" +) + +func NewContext(r Route, res http.ResponseWriter, req *http.Request) *DefaultContext { + data := make(map[string]interface{}) + data["path"] = r.Path + + return &DefaultContext{ + response: res, + request: req, + params: req.URL.Query(), + data: data, + } +} + +type DefaultContext struct { + context.Context + response http.ResponseWriter + request *http.Request + params url.Values + contentType string + data map[string]interface{} +} + +// Response returns the original Response for the request. +func (d *DefaultContext) Response() http.ResponseWriter { + return d.response +} + +// Request returns the original Request. +func (d *DefaultContext) Request() *http.Request { + return d.request +} + +// Params returns all of the parameters for the request, +// including both named params and query string parameters. +func (d *DefaultContext) Params() url.Values { + return d.params +} + +// Param returns a param, either named or query string, +// based on the key. +func (d *DefaultContext) Param(key string) string { + return d.Params().Get(key) +} + +// Set a value onto the Context. Any value set onto the Context +// will be automatically available in templates. +func (d *DefaultContext) Set(key string, value interface{}) { + d.data[key] = value +} + +// Value that has previously stored on the context. +func (d *DefaultContext) Value(key interface{}) interface{} { + if k, ok := key.(string); ok { + if v, ok := d.data[k]; ok { + return v + } + } + return d.Context.Value(key) +} + +func (d *DefaultContext) Render(status int, rr render.Renderer) error { + p := plush.NewContext() + + /*if rr != nil { + data := d.data + pp := map[string]string{} + for k, v := range d.params { + pp[k] = v[0] + } + + data["params"] = pp + data["request"] = d.Request() + bb := &bytes.Buffer{} + + err := rr.Render(bb, data) + if err != nil { + return HTTPError{Status: 500, Cause: errors.WithStack(err)} + } + + d.Response().Header().Set("Content-Type", rr.ContentType()) + d.Response().WriteHeader(status) + _, err = io.Copy(d.Response(), bb) + if err != nil { + return HTTPError{Status: 500, Cause: errors.WithStack(err)} + } + + return nil + } + */ + d.Response().WriteHeader(status) + return nil +} diff --git a/default_router.go b/default_router.go new file mode 100644 index 0000000..1214cdb --- /dev/null +++ b/default_router.go @@ -0,0 +1,31 @@ +package tuu + +import "net/http" + +func NewRouter() *DefaultRouter { + return &DefaultRouter{} +} + +type DefaultRouter struct { + Routes []*Route +} + +func (r *DefaultRouter) GET(path string, h Handler) { + r.addRoute(http.MethodGet, path, h) +} + +func (r *DefaultRouter) POST(path string, h Handler) { + r.addRoute(http.MethodPost, path, h) +} + +func (r *DefaultRouter) GetRoutes() []*Route { + return r.Routes +} + +func (r *DefaultRouter) addRoute(m, p string, h Handler) { + r.Routes = append(r.Routes, &Route{ + Method: m, + Path: p, + Handler: h, + }) +} diff --git a/route.go b/route.go new file mode 100644 index 0000000..27aa462 --- /dev/null +++ b/route.go @@ -0,0 +1,24 @@ +package tuu + +import ( + "net/http" + + gcontext "github.com/gorilla/context" +) + +type Route struct { + Method string + Path string + Handler Handler +} + +func (r *Route) ServeHTTP(res http.ResponseWriter, req *http.Request) { + defer gcontext.Clear(req) + + c := NewContext(*r, res, req) + + if err := r.Handler(c); err != nil { + c.Response().WriteHeader(500) + c.Response().Write([]byte(err.Error())) + } +} diff --git a/router.go b/router.go new file mode 100644 index 0000000..a835879 --- /dev/null +++ b/router.go @@ -0,0 +1,9 @@ +package tuu + +type Handler func(Context) error + +type Router interface { + GET(path string, ctx Context) + POST(path string, ctx Context) + GetRoutes() []*Route +} diff --git a/tuu.go b/tuu.go new file mode 100644 index 0000000..31ffeb0 --- /dev/null +++ b/tuu.go @@ -0,0 +1,22 @@ +package tuu + +import ( + "github.com/gorilla/mux" +) + +func New(r Router) *App { + return &App{router: r} +} + +type App struct { + router Router +} + +func (a *App) Serve() { + r := mux.NewRouter() + + for _, route := range a.router.GetRoutes() { + r.Handle(route.Path, route).Methods(route.Method) + } + +}