Skip to content

Commit

Permalink
add render
Browse files Browse the repository at this point in the history
  • Loading branch information
chonglou committed May 11, 2017
1 parent 8068446 commit e92dbdf
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 14 deletions.
28 changes: 22 additions & 6 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package h2o

import (
"context"
"encoding/json"
"encoding/xml"
"net"
"net/http"
"strings"

validator "gopkg.in/go-playground/validator.v9"

"github.com/go-playground/form"
"github.com/unrolled/render"
)

// K key type
Expand All @@ -21,6 +24,9 @@ type Context struct {
Request *http.Request

vars map[string]string
val *validator.Validate
dec *form.Decoder
rdr *render.Render
}

// Set set
Expand Down Expand Up @@ -75,12 +81,22 @@ func (p *Context) ClientIP() string {

// JSON write json
func (p *Context) JSON(s int, v interface{}) error {
enc := json.NewEncoder(p.Writer)
return enc.Encode(v)
return p.rdr.JSON(p.Writer, s, v)
}

// XML write xml
func (p *Context) XML(s int, v interface{}) error {
enc := xml.NewEncoder(p.Writer)
return enc.Encode(v)
return p.rdr.XML(p.Writer, s, v)
}

// Bind binds the passed struct pointer
func (p *Context) Bind(v interface{}) error {
e := p.Request.ParseForm()
if e == nil {
e = p.dec.Decode(v, p.Request.Form)
}
if e == nil {
e = p.val.Struct(v)
}
return e
}
13 changes: 8 additions & 5 deletions i18n/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@ const (
)

// Middleware detect language from http request
func (p *I18n) Middleware(c *h2o.Context) error {
func (p *I18n) Middleware() (h2o.HandlerFunc, error) {
langs, err := p.Store.Languages()
if err != nil {
return err
return nil, err
}
var tags []language.Tag
for _, l := range langs {
tags = append(tags, language.Make(l))
}
matcher := language.NewMatcher(tags)
tag, _, _ := matcher.Match(language.Make(p.detect(c.Request)))
c.Set(LOCALE, tag.String())
return nil

return func(c *h2o.Context) error {
tag, _, _ := matcher.Match(language.Make(p.detect(c.Request)))
c.Set(LOCALE, tag.String())
return nil
}, nil
}

func (p *I18n) detect(r *http.Request) string {
Expand Down
42 changes: 40 additions & 2 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import (
"time"

log "github.com/Sirupsen/logrus"
"github.com/go-playground/form"
"github.com/gorilla/mux"
"github.com/rs/cors"
"github.com/unrolled/render"
validator "gopkg.in/go-playground/validator.v9"
)

// HandlerFunc an adapter to allow the use of ordinary functions as HTTP handler
Expand All @@ -33,6 +36,21 @@ type Router struct {
routes []*route
}

// Use use middlewares
func (p *Router) Use(handlers ...HandlerFunc) {
p.handlers = append(p.handlers, handlers...)
}

// Crud crud
func (p *Router) Crud(path string, list []HandlerFunc, create []HandlerFunc, read []HandlerFunc, update []HandlerFunc, delete []HandlerFunc) {
p.GET(path, list...)
p.POST(path, create...)
child := path + "/{id}"
p.GET(child, read...)
p.POST(child, update...)
p.DELETE(child, delete...)
}

// Group creates a new router group
func (p *Router) Group(group func(*Router), path string, handlers ...HandlerFunc) {
rt := &Router{
Expand Down Expand Up @@ -82,21 +100,28 @@ func (p *Router) Handle(methods []string, path string, handlers ...HandlerFunc)
}

// Run attaches the router to a http.Server and starts listening and serving HTTP requests.
func (p *Router) Run(port int, grace bool, options cors.Options) error {
func (p *Router) Run(port int, grace bool, cro cors.Options, rdo render.Options) error {
addr := fmt.Sprintf(":%d", port)
log.Infof(
"application starting on http://localhost:%d",
port,
)
// --------------
rt := mux.NewRouter()
va := validator.New()
de := form.NewDecoder()
rd := render.New(rdo)
for _, r := range p.routes {
rt.HandleFunc(r.path, func(w http.ResponseWriter, r *http.Request) {
begin := time.Now()
ctx := Context{
Request: r,
Writer: w,
vars: mux.Vars(r),

dec: de,
val: va,
rdr: rd,
}
log.Infof("%s %s %s %s", r.Proto, r.Method, r.RequestURI, ctx.ClientIP())
for _, h := range p.handlers {
Expand All @@ -114,7 +139,7 @@ func (p *Router) Run(port int, grace bool, options cors.Options) error {
}).Methods(r.methods...)
}
// ----------------
hnd := cors.New(options).Handler(rt)
hnd := cors.New(cro).Handler(rt)
// ----------------
if grace {
srv := &http.Server{Addr: addr, Handler: hnd}
Expand Down Expand Up @@ -143,3 +168,16 @@ func (p *Router) Run(port int, grace bool, options cors.Options) error {
// ----------------
return http.ListenAndServe(addr, hnd)
}

// WalkFunc walk func
type WalkFunc func(methods []string, path string, handlers ...HandlerFunc) error

// Walk walk routes
func (p *Router) Walk(f WalkFunc) error {
for _, r := range p.routes {
if e := f(r.methods, r.path, r.handlers...); e != nil {
return e
}
}
return nil
}
38 changes: 37 additions & 1 deletion vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,30 @@
"revision": "e7fea39b01aea8d5671f6858f0532f56e8bff3a5",
"revisionTime": "2017-03-28T15:39:02Z"
},
{
"checksumSHA1": "sk+CtFJzus412jVGy9WlCQPbkEQ=",
"path": "github.com/go-playground/form",
"revision": "20d04065dbc6f79aac03bbfd49b6a9e0658f5842",
"revisionTime": "2017-03-27T19:14:18Z"
},
{
"checksumSHA1": "pI0js2qnSKXOzUq9AoA5CJDJNRw=",
"path": "github.com/go-playground/locales",
"revision": "1e5f1161c6416a5ff48840eb8724a394e48cc534",
"revisionTime": "2017-03-27T19:14:50Z"
},
{
"checksumSHA1": "YWIa2z/i81ttb1Db9fm95XnIgwI=",
"path": "github.com/go-playground/locales/currency",
"revision": "1e5f1161c6416a5ff48840eb8724a394e48cc534",
"revisionTime": "2017-03-27T19:14:50Z"
},
{
"checksumSHA1": "7QcjNTtW0Rc/5BPw1MPaQ6YNjso=",
"path": "github.com/go-playground/universal-translator",
"revision": "71201497bace774495daed26a3874fd339e0b538",
"revisionTime": "2017-03-27T19:17:03Z"
},
{
"checksumSHA1": "WMTjT/WhwABW0CP22n1KeXxb96w=",
"path": "github.com/google/uuid",
Expand Down Expand Up @@ -73,7 +97,13 @@
"path": "github.com/streadway/amqp",
"revision": "afe8eee29a74d213b1f3fb2586058157c397da60",
"revisionTime": "2017-03-13T17:48:48Z"
},
},
{
"checksumSHA1": "XQ2GsdTmZgUO2xycRg02vxBYi50=",
"path": "github.com/unrolled/render",
"revision": "50716a0a853771bb36bfce61a45cdefdb98c2e6e",
"revisionTime": "2017-01-09T14:32:44Z"
},
{
"checksumSHA1": "Y+HGqEkYM15ir+J93MEaHdyFy0c=",
"path": "golang.org/x/net/context",
Expand Down Expand Up @@ -109,6 +139,12 @@
"path": "golang.org/x/text/unicode/cldr",
"revision": "470f45bf29f4147d6fbd7dfd0a02a848e49f5bf4",
"revisionTime": "2017-04-25T18:31:26Z"
},
{
"checksumSHA1": "goAsvFRjrpp8ix5zo+IPyZAdOeg=",
"path": "gopkg.in/go-playground/validator.v9",
"revision": "6d8c18553ea1ac493d049edd6f102f52e618f085",
"revisionTime": "2017-04-05T11:11:23Z"
}
],
"rootPath": "github.com/kapmahc/h2o"
Expand Down

0 comments on commit e92dbdf

Please sign in to comment.