mirror of
https://github.com/cubixle/tuu.git
synced 2026-04-24 23:04:43 +01:00
initial commit
This commit is contained in:
12
context.go
Normal file
12
context.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package tuu
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Context interface {
|
||||
context.Context
|
||||
Response() http.ResponseWriter
|
||||
Request() *http.Request
|
||||
}
|
||||
103
default_context.go
Normal file
103
default_context.go
Normal file
@@ -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
|
||||
}
|
||||
31
default_router.go
Normal file
31
default_router.go
Normal file
@@ -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,
|
||||
})
|
||||
}
|
||||
24
route.go
Normal file
24
route.go
Normal file
@@ -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()))
|
||||
}
|
||||
}
|
||||
9
router.go
Normal file
9
router.go
Normal file
@@ -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
|
||||
}
|
||||
22
tuu.go
Normal file
22
tuu.go
Normal file
@@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user