Skip to content

Commit

Permalink
update readme and format codes
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 28, 2019
1 parent 5c7d41b commit 94e126a
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 81 deletions.
15 changes: 7 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
language: go
go:
- '1.8'
- '1.9'
- 1.10.x
- 1.11.x
- '1.10'
- '1.11'
- '1.12'

before_install:
- go get github.com/mattn/goveralls
- go get golang.org/x/tools/cmd/cover
- go get github.com/mattn/goveralls
- go get golang.org/x/tools/cmd/cover

script:
# - go test -v -cover
- $HOME/gopath/bin/goveralls -v -service=travis-ci
# - go test -v -cover
- $HOME/gopath/bin/goveralls -v -service=travis-ci
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# rux router
# Rux

[![GoDoc](https://godoc.org/github.com/gookit/rux?status.svg)](https://godoc.org/github.com/gookit/rux)
[![Build Status](https://travis-ci.org/gookit/rux.svg?branch=master)](https://travis-ci.org/gookit/rux)
Expand All @@ -7,7 +7,7 @@

Simple and fast web framework for build golang HTTP applications.

> **[中文说明](README_cn.md)**
> **[中文说明](README.zh-CN.md)**
- fast route match, support route group
- support route path params
Expand Down
6 changes: 3 additions & 3 deletions README_cn.md → README.zh-CN.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# rux 路由器
# Rux

[![GoDoc](https://godoc.org/github.com/gookit/rux?status.svg)](https://godoc.org/github.com/gookit/rux)
[![Build Status](https://travis-ci.org/gookit/rux.svg?branch=master)](https://travis-ci.org/gookit/rux)
[![Coverage Status](https://coveralls.io/repos/github/gookit/rux/badge.svg?branch=master)](https://coveralls.io/github/gookit/rux?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/gookit/rux)](https://goreportcard.com/report/github.com/gookit/rux)

简单且快速的 Go HTTP 请求路由器,支持中间件,兼容 http.Handler 接口。
简单且快速的 Go web 框架,支持中间件,兼容 http.Handler 接口。

> **[EN README](README.md)**
- 支持路由参数,支持路由组,支持给路由命名
- 支持方便的静态文件/目录处理
- 支持缓存最近访问的动态路由以获得更高性能
- 支持中间件: 路由中间件,组中间件,全局中间件
- 支持中间件: 路由中间件,组中间件,全局中间件
- 兼容支持 `http.Handler` 接口,可以直接使用其他的常用中间件
- 支持添加 `NotFound``NotAllowed` 处理

Expand Down
6 changes: 5 additions & 1 deletion _examples/graceful/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,9 @@ func grace2() {
}
}
}()
srv.ListenAndServe()

if err := srv.ListenAndServe(); err != http.ErrServerClosed {
// Error starting or closing listener:
log.Printf("HTTP server ListenAndServe: %v", err)
}
}
3 changes: 2 additions & 1 deletion _examples/more_middle_use.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ package main

import (
"encoding/json"
"net/http"

"github.com/gookit/rux"
"github.com/thoas/stats"
"net/http"
)

func main() {
Expand Down
15 changes: 12 additions & 3 deletions _examples/multi-port/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"log"
"net/http"
"sync"
)
Expand All @@ -16,11 +17,19 @@ func main() {
http.HandleFunc("/", Hello)

Launch(func() {
http.ListenAndServe(PORT, nil)
err := http.ListenAndServe(PORT, nil)
if err != nil && err != http.ErrServerClosed {
// Error starting or closing listener:
log.Printf("HTTP server ListenAndServe: %v", err)
}
})

Launch(func() {
http.ListenAndServeTLS(":443", "cert.pem", "key.pem", nil)
err := http.ListenAndServeTLS(":443", "cert.pem", "key.pem", nil)
if err != nil && err != http.ErrServerClosed {
// Error starting or closing listener:
log.Printf("HTTP server ListenAndServeTLS: %v", err)
}
})

wg.Wait()
Expand All @@ -37,5 +46,5 @@ func Launch(fn func()) {

func Hello(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, "hello, gopher")
_,_= fmt.Fprintf(w, "hello, gopher")
}
10 changes: 5 additions & 5 deletions _examples/raw-serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ import "net/http"

func main() {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("-O-"))
_,_ = w.Write([]byte("-O-"))
})

mdl1 := func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("a"))
_,_ = w.Write([]byte("a"))
h.ServeHTTP(w, r)
w.Write([]byte("A"))
_,_ = w.Write([]byte("A"))
})
}
mdl2 := func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("b"))
_,_ = w.Write([]byte("b"))
h.ServeHTTP(w, r)
w.Write([]byte("B"))
_,_ = w.Write([]byte("B"))
})
}

Expand Down
49 changes: 49 additions & 0 deletions _examples/serve/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import "github.com/gookit/rux"

// SiteController define a controller
type SiteController struct {
}

// AddRoutes for the controller
func (c *SiteController) AddRoutes(r *rux.Router) {
r.GET("{id}", c.Get)
r.POST("", c.Post)

// mp := map[string]rux.HandlerFunc{
// "get,{id}": c.Get,
// }
}

// Get action
func (c *SiteController) Get(ctx *rux.Context) {
ctx.WriteString("hello, in " + ctx.URL().Path)
ctx.WriteString("\n ok")
}

// Post action
func (c *SiteController) Post(ctx *rux.Context) {
ctx.WriteString("hello, in " + ctx.URL().Path)
}

// BlogController define a controller
type BlogController struct {
}

// AddRoutes for the controller
func (c *BlogController) AddRoutes(r *rux.Router) {
r.GET("{id}", c.Get)
r.POST("", c.Post)
}

// Get action
func (c *BlogController) Get(ctx *rux.Context) {
ctx.WriteString("hello, in " + ctx.URL().Path)
ctx.WriteString("\nok")
}

// Post action
func (c *BlogController) Post(ctx *rux.Context) {
ctx.Text(200, "hello, in "+ctx.URL().Path)
}
55 changes: 7 additions & 48 deletions _examples/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@ func main() {

r.GET("/", func(c *rux.Context) {
c.Text(200, "hello "+c.URL().Path)
})

r.GET("/bauth", func(c *rux.Context) {
c.Text(200, "hello "+c.URL().Path)
}).Use(handlers.HTTPBasicAuth(map[string]string{
// "test": "123",
"test": "123",
}))

r.GET("/routes", handlers.DumpRoutesHandler())
r.GET("/about[.html]", defHandle)
r.GET("/hi-{name}", defHandle).NamedTo("my-route", r)
Expand Down Expand Up @@ -84,12 +89,12 @@ func main() {
})

r.Controller("/blog", &BlogController{})
r.Controller("/site", &SiteController{})

fmt.Println(r)

// quick start
r.Listen(":18080")

// apply pre-handlers
// http.ListenAndServe(":18080", handlers.HTTPMethodOverrideHandler(r))
}
Expand Down Expand Up @@ -128,49 +133,3 @@ func newProxy(targetUrl string) *httputil.ReverseProxy {
func defHandle(ctx *rux.Context) {
ctx.WriteString("hello, in " + ctx.URL().Path)
}

// SiteController define a controller
type SiteController struct {
}

// AddRoutes for the controller
func (c *SiteController) AddRoutes(r *rux.Router) {
r.GET("{id}", c.Get)
r.POST("", c.Post)

// mp := map[string]rux.HandlerFunc{
// "get,{id}": c.Get,
// }
}

// Get action
func (c *SiteController) Get(ctx *rux.Context) {
ctx.WriteString("hello, in " + ctx.URL().Path)
ctx.WriteString("\n ok")
}

// Post action
func (c *SiteController) Post(ctx *rux.Context) {
ctx.WriteString("hello, in " + ctx.URL().Path)
}

// BlogController define a controller
type BlogController struct {
}

// AddRoutes for the controller
func (c *BlogController) AddRoutes(r *rux.Router) {
r.GET("{id}", c.Get)
r.POST("", c.Post)
}

// Get action
func (c *BlogController) Get(ctx *rux.Context) {
ctx.WriteString("hello, in " + ctx.URL().Path)
ctx.WriteString("\nok")
}

// Post action
func (c *BlogController) Post(ctx *rux.Context) {
ctx.Text(200, "hello, in "+ctx.URL().Path)
}
3 changes: 2 additions & 1 deletion context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package rux

import (
"bytes"
"github.com/stretchr/testify/assert"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func mockContext(method, uri string, body io.Reader, header m) *Context {
Expand Down
7 changes: 4 additions & 3 deletions dispatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package rux

import (
"fmt"
"github.com/gookit/goutil/testutil"
"github.com/stretchr/testify/assert"
"net/http"
"os"
"runtime"
"testing"

"github.com/gookit/goutil/testutil"
"github.com/stretchr/testify/assert"
)

func ExampleRouter_ServeHTTP() {
Expand Down Expand Up @@ -391,7 +392,7 @@ func TestContext_Cookie(t *testing.T) {
}

func TestHandleError(t *testing.T) {
r := New()
r := New()
ris := assert.New(t)

r.GET("/test", func(c *Context) {
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module github.com/gookit/rux

go 1.12
go 1.11

require (
github.com/gookit/goutil v0.1.2
github.com/gookit/color v1.1.7
github.com/gookit/goutil v0.1.4
github.com/gorilla/websocket v1.4.0
github.com/stretchr/testify v1.3.0
)
15 changes: 13 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gookit/goutil v0.1.2 h1:ynF8c/MEo4YwW9MG9tVq09hwt9tRTkWJaTgijahOVn0=
github.com/gookit/goutil v0.1.2/go.mod h1:3lBTrzsCHr3OpPJIsaRy+MRiLEXkb20MGLmEmYnlfto=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gookit/color v1.1.7 h1:WR5I/mhSHzemW2DzG54hTsUb7OzaREvkcmUG4/WST4Q=
github.com/gookit/color v1.1.7/go.mod h1:R3ogXq2B9rTbXoSHJ1HyUVAZ3poOJHpd9nQmyGZsfvQ=
github.com/gookit/goutil v0.1.4 h1:oD+UCY7jk5Bj7I5Npgg3Tvky9bzQQ5lYuebRyNG2StQ=
github.com/gookit/goutil v0.1.4/go.mod h1:LRPzhcWV540XDMnW+7oXy5VRW+lyQ7dWi7blm187ILo=
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
3 changes: 2 additions & 1 deletion middleware_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package rux

import (
"github.com/stretchr/testify/assert"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func TestRouteMiddleware(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package rux

import (
"fmt"
"github.com/stretchr/testify/assert"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func Example() {
Expand Down

0 comments on commit 94e126a

Please sign in to comment.