Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: goroute/route
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.0.1
Choose a base ref
...
head repository: goroute/route
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 4 commits
  • 6 files changed
  • 1 contributor

Commits on Sep 24, 2019

  1. Update README.md

    anjmao authored Sep 24, 2019

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    6583bd1 View commit details
  2. Update README.md

    anjmao authored Sep 24, 2019
    Copy the full SHA
    233defc View commit details

Commits on Nov 3, 2019

  1. Copy the full SHA
    70307fb View commit details

Commits on Dec 23, 2019

  1. Update README.md

    anjmao authored Dec 23, 2019
    Copy the full SHA
    7a57421 View commit details
Showing with 38 additions and 36 deletions.
  1. +7 −8 README.md
  2. +5 −5 context.go
  3. +2 −2 context_test.go
  4. +2 −0 go.mod
  5. +19 −18 mux.go
  6. +3 −3 mux_test.go
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -7,14 +7,13 @@

</div>

## Features

* Minimal
* <b>Zero</b> external runtime dependencies
* Smart HTTP Routing
* Data binding for JSON form payload
* Middlewares on global, group or single route level
* Full control of http server
## Few main features

* Minimal core.
* No external runtime dependencies. Custom middlewares which requires 3th party dependecies are places in separates repositories under goroute org.
* HTTP Routing.
* Middlewares support.
* Global error handling.

## Getting Started

10 changes: 5 additions & 5 deletions context.go
Original file line number Diff line number Diff line change
@@ -83,7 +83,7 @@ type (
// Set saves data in the context.
Set(key string, val interface{})

// Bind binds the request body into provided type `i`. The default binder
// Bind binds the request body into provided type `i`. The default Binder
// does it based on Content-Type header.
Bind(i interface{}) error

@@ -276,15 +276,15 @@ func (c *context) Set(key string, val interface{}) {
}

func (c *context) Bind(i interface{}) error {
return c.mux.binder.Bind(i, c)
return c.mux.Binder.Bind(i, c)
}

func (c *context) Render(code int, name string, data interface{}) (err error) {
if c.mux.renderer == nil {
if c.mux.Renderer == nil {
return ErrRendererNotRegistered
}
buf := new(bytes.Buffer)
if err = c.mux.renderer.Render(buf, name, data, c); err != nil {
if err = c.mux.Renderer.Render(buf, name, data, c); err != nil {
return
}
return c.HTMLBlob(code, buf.Bytes())
@@ -391,7 +391,7 @@ func (c *context) Redirect(code int, url string) error {
}

func (c *context) Error(err error) {
c.mux.httpErrorHandler(err, c)
c.mux.HTTPErrorHandler(err, c)
}

func (c *context) Handler() HandlerFunc {
4 changes: 2 additions & 2 deletions context_test.go
Original file line number Diff line number Diff line change
@@ -47,14 +47,14 @@ func TestContext(t *testing.T) {
tmpl := &Template{
templates: template.Must(template.New("hello").Parse("Hello, {{.}}!")),
}
c.mux.renderer = tmpl
c.mux.Renderer = tmpl
err := c.Render(http.StatusOK, "hello", "Jon Snow")
if assert.NoError(err) {
assert.Equal(http.StatusOK, rec.Code)
assert.Equal("Hello, Jon Snow!", rec.Body.String())
}

c.mux.renderer = nil
c.mux.Renderer = nil
err = c.Render(http.StatusOK, "hello", "Jon Snow")
assert.Error(err)

2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -5,3 +5,5 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.2.2
)

go 1.13
37 changes: 19 additions & 18 deletions mux.go
Original file line number Diff line number Diff line change
@@ -18,16 +18,17 @@ import (
type (
// Mux is the top-level framework instance.
Mux struct {
premiddleware []MiddlewareFunc
middleware []MiddlewareFunc
maxParam *int
router *router
notFoundHandler HandlerFunc
pool sync.Pool
premiddleware []MiddlewareFunc
middleware []MiddlewareFunc
maxParam *int
router *router
notFoundHandler HandlerFunc
pool sync.Pool

Debug bool
httpErrorHandler HTTPErrorHandler
binder Binder
renderer Renderer
HTTPErrorHandler HTTPErrorHandler
Binder Binder
Renderer Renderer
}

// Route contains a handler and information for matching against requests.
@@ -167,7 +168,7 @@ var (
ErrRequestTimeout = NewHTTPError(http.StatusRequestTimeout)
ErrServiceUnavailable = NewHTTPError(http.StatusServiceUnavailable)
ErrValidatorNotRegistered = errors.New("validator not registered")
ErrRendererNotRegistered = errors.New("renderer not registered")
ErrRendererNotRegistered = errors.New("Renderer not registered")
ErrInvalidRedirectCode = errors.New("invalid redirect status code")
ErrCookieNotFound = errors.New("cookie not found")
)
@@ -192,14 +193,14 @@ type options struct {
// A Option sets options such as credentials, tls, etc.
type Option func(*options)

// WithBinder allows to override default mux binder.
// WithBinder allows to override default mux Binder.
func WithBinder(binder Binder) Option {
return func(o *options) {
o.binder = binder
}
}

// WithRenderer allows to register mux view renderer.
// WithRenderer allows to register mux view Renderer.
func WithRenderer(renderer Renderer) Option {
return func(o *options) {
o.renderer = renderer
@@ -225,15 +226,15 @@ func NewServeMux(opt ...Option) (e *Mux) {

e = &Mux{
maxParam: new(int),
binder: opts.binder,
renderer: opts.renderer,
Binder: opts.binder,
Renderer: opts.renderer,
}

// http error handler must be set after mux instance.
if opts.httpErrorHandler != nil {
e.httpErrorHandler = opts.httpErrorHandler
e.HTTPErrorHandler = opts.httpErrorHandler
} else {
e.httpErrorHandler = e.defaultHTTPErrorHandler
e.HTTPErrorHandler = e.defaultHTTPErrorHandler
}

e.pool.New = func() interface{} {
@@ -439,7 +440,7 @@ func (mux *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request) {

// Execute chain
if err := h(c); err != nil {
mux.httpErrorHandler(err, c)
mux.HTTPErrorHandler(err, c)
}

// Release context
@@ -503,7 +504,7 @@ func handlerName(h HandlerFunc) string {
return t.String()
}

// NewDefaultTemplateRenderer creates default template renderer with given pattern.
// NewDefaultTemplateRenderer creates default template Renderer with given pattern.
func NewDefaultTemplateRenderer(pattern string) *templateRenderer {
return &templateRenderer{
templates: template.Must(template.ParseGlob(pattern)),
6 changes: 3 additions & 3 deletions mux_test.go
Original file line number Diff line number Diff line change
@@ -88,9 +88,9 @@ func TestMuxWithOptions(t *testing.T) {
WithHTTPErrorHandler(mockHTTPErrorHandler),
)

assert.Equal(t, binder, mux.binder)
assert.Equal(t, renderer, mux.renderer)
assert.NotNil(t, mux.httpErrorHandler)
assert.Equal(t, binder, mux.Binder)
assert.Equal(t, renderer, mux.Renderer)
assert.NotNil(t, mux.HTTPErrorHandler)
}

func TestMuxFile(t *testing.T) {