Skip to content

Commit

Permalink
Merge pull request #1 from vicanso/trie
Browse files Browse the repository at this point in the history
Trie
  • Loading branch information
vicanso authored Apr 11, 2020
2 parents e3a0f12 + 8a2cf46 commit 3e706ec
Show file tree
Hide file tree
Showing 50 changed files with 5,720 additions and 507 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ sudo: required
go:
- "1.12"
- "1.13"
- "1.14"
- master

script:
Expand Down
51 changes: 27 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,19 @@ e.Method(path string, ...func(*elton.Context) error)
elton的路由使用[httprouter](https://github.com/julienschmidt/httprouter),下面是两个简单的示例,更多的使用方式可以参考httprouter。

```go
// 带参数的路由配置
e.GET("/books/:type", func(c *elton.Context) error {
c.BodyBuffer = bytes.NewBufferString(c.Param("type"))
return nil
// 带参数路由
e.GET("/users/{type}", func(c *elton.Context) error {
c.BodyBuffer = bytes.NewBufferString(c.Param("type"))
return nil
})

// 带中间件的路由配置
e.GET("/users/me", func(c *elton.Context) error {
c.Set("account", "tree.xie")
return c.Next()
c.Set("account", "tree.xie")
return c.Next()
}, func(c *elton.Context) error {
c.BodyBuffer = bytes.NewBufferString(c.GetString("account"))
return nil
c.BodyBuffer = bytes.NewBufferString(c.GetString("account"))
return nil
})
```

Expand All @@ -80,21 +80,21 @@ e.GET("/users/me", func(c *elton.Context) error {

### responser

HTTP请求响应数据时,需要将数据转换为Buffer返回,而在应用时响应数据一般为各类的struct或map等结构化数据,因此elton提供了Body(interface{})字段来保存这些数据,再使用自定义的中间件将数据转换为对应的字节数据,[elton-responder](https://github.com/vicanso/elton-responder)提供了转数据转换为json字节并设置对应的Content-Type。
HTTP请求响应数据时,需要将数据转换为Buffer返回,而在应用时响应数据一般为各类的struct或map等结构化数据,因此elton提供了Body(interface{})字段来保存这些数据,再使用自定义的中间件将数据转换为对应的字节数据,`elton-responder`提供了将struct(map)转换为json字节并设置对应的Content-Type,对于string([]byte)则直接输出

```go
package main

import (
"github.com/vicanso/elton"
responder "github.com/vicanso/elton-responder"
"github.com/vicanso/elton/middleware"
)

func main() {

e := elton.New()
// 对响应数据 c.Body 转换为相应的json响应
e.Use(responder.NewDefault())
e.Use(middleware.NewDefaultResponder())

getSession := func(c *elton.Context) error {
c.Set("account", "tree.xie")
Expand All @@ -116,27 +116,26 @@ func main() {
panic(err)
}
}

```

### error-handler
### error

当请求处理失败时,直接返回error则可,elton从error中获取出错信息并输出。默认的出错处理并不适合实际应用场景,建议使用自定义出错类配合中间件,便于统一的错误处理,程序监控。
当请求处理失败时,直接返回error则可,elton从error中获取出错信息并输出。默认的出错处理并不适合实际应用场景,建议使用自定义出错类配合中间件,便于统一的错误处理,程序监控,下面是引入错误中间件将出错转换为json形式的响应

```go
package main

import (
"github.com/vicanso/elton"
errorhandler "github.com/vicanso/elton-error-handler"
"github.com/vicanso/elton/middleware"
"github.com/vicanso/hes"
)

func main() {

e := elton.New()
// 指定出错以json的形式返回
e.Use(errorhandler.New(errorhandler.Config{
e.Use(middleware.NewError(middleware.ErrorConfig{
ResponseType: "json",
}))

Expand All @@ -159,12 +158,16 @@ func main() {
## bench

```
BenchmarkRoutes-8 3489271 343 ns/op 376 B/op 4 allocs/op
BenchmarkGetFunctionName-8 129711422 9.23 ns/op 0 B/op 0 allocs/op
BenchmarkContextGet-8 14131228 84.4 ns/op 16 B/op 1 allocs/op
BenchmarkContextNewMap-8 183387170 6.52 ns/op 0 B/op 0 allocs/op
BenchmarkConvertServerTiming-8 1430475 839 ns/op 360 B/op 11 allocs/op
BenchmarkGetStatus-8 1000000000 0.272 ns/op 0 B/op 0 allocs/op
BenchmarkRWMutexSignedKeys-8 35028435 33.5 ns/op
BenchmarkAtomicSignedKeys-8 602747588 1.99 ns/op
BenchmarkRoutes-8 5626749 220 ns/op 152 B/op 3 allocs/op
BenchmarkGetFunctionName-8 121043851 9.61 ns/op 0 B/op 0 allocs/op
BenchmarkContextGet-8 14413359 82.3 ns/op 16 B/op 1 allocs/op
BenchmarkContextNewMap-8 182361898 6.60 ns/op 0 B/op 0 allocs/op
BenchmarkConvertServerTiming-8 1377422 878 ns/op 360 B/op 11 allocs/op
BenchmarkGetStatus-8 1000000000 0.275 ns/op 0 B/op 0 allocs/op
BenchmarkStatic-8 21412 55142 ns/op 25940 B/op 628 allocs/op
BenchmarkGitHubAPI-8 13143 91191 ns/op 33816 B/op 812 allocs/op
BenchmarkGplusAPI-8 271857 4359 ns/op 2144 B/op 52 allocs/op
BenchmarkParseAPI-8 136795 8806 ns/op 4287 B/op 104 allocs/op
BenchmarkRWMutexSignedKeys-8 34447268 33.8 ns/op
BenchmarkAtomicSignedKeys-8 1000000000 0.412 ns/op
```
2 changes: 1 addition & 1 deletion SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@

* [Custom compress](./docs/custom_compress.md)

* [Router param validate](./docs/router_param_validate.md)
* [Router params](./docs/router_params.md)
22 changes: 22 additions & 0 deletions bench_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
// MIT License

// Copyright (c) 2020 Tree Xie

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package elton

import (
Expand Down
64 changes: 30 additions & 34 deletions context.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
// Copyright 2018 tree xie
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// MIT License

// Copyright (c) 2020 Tree Xie

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package elton

Expand All @@ -30,7 +38,6 @@ import (
"sync/atomic"
"time"

"github.com/julienschmidt/httprouter"
"github.com/vicanso/hes"
intranetip "github.com/vicanso/intranet-ip"
"github.com/vicanso/keygrip"
Expand All @@ -48,8 +55,6 @@ type (
Context struct {
Request *http.Request
Response http.ResponseWriter
// Headers http response's header, it's equal to response's header
Headers http.Header
// Committed commit the data to response, when it's true, the response has been sent.
// If using custom response handler, please set it true.
Committed bool
Expand All @@ -60,9 +65,7 @@ type (
// Next next function, it will be auto generated.
Next func() error
// Params route params
Params map[string]string
// RawParams http router params
RawParams httprouter.Params
Params *RouteParams
// StatusCode http response's status code, default is 0 which will be handle as 200
StatusCode int
// Body http response's body, which should be converted to bytes by response middlware.
Expand Down Expand Up @@ -100,21 +103,18 @@ const (
func (c *Context) Reset() {
c.Request = nil
c.Response = nil
c.Headers = nil
c.Committed = false
c.ID = ""
c.Route = ""
c.Next = nil
c.Params = nil
c.RawParams = nil
c.Params.Reset()
c.StatusCode = 0
c.Body = nil
c.BodyBuffer = nil
c.RequestBody = nil
c.m = nil
c.realIP = ""
c.clientIP = ""
c.elton = nil
c.reuseStatus = ReuseContextEnabled
c.cacheQuery = nil
}
Expand Down Expand Up @@ -192,7 +192,7 @@ func (c *Context) Param(name string) string {
if c.Params == nil {
return ""
}
return c.Params[name]
return c.Params.Get(name)
}

// getCacheQuery get the cache query
Expand Down Expand Up @@ -363,7 +363,7 @@ func (c *Context) AddRequestHeader(key, value string) {

// Header get headers of http response
func (c *Context) Header() http.Header {
return c.Headers
return c.Response.Header()
}

// WriteHeader set the http status code
Expand All @@ -381,23 +381,23 @@ func (c *Context) Write(buf []byte) (int, error) {

// GetHeader get header from http response
func (c *Context) GetHeader(key string) string {
return c.Headers.Get(key)
return c.Header().Get(key)
}

// SetHeader set http header to response.
// It replaces any existing values of the key.
func (c *Context) SetHeader(key, value string) {
if value == "" {
c.Headers.Del(key)
c.Header().Del(key)
return
}
c.Headers.Set(key, value)
c.Header().Set(key, value)
}

// AddHeader add http header to response.
// It appends to any existing value of the key.
func (c *Context) AddHeader(key, value string) {
c.Headers.Add(key, value)
c.Header().Add(key, value)
}

// ResetHeader reset response header
Expand Down Expand Up @@ -565,8 +565,7 @@ func (c *Context) DisableReuse() {
}

func (c *Context) isReuse() bool {
v := atomic.LoadInt32(&c.reuseStatus)
return v == ReuseContextEnabled
return atomic.LoadInt32(&c.reuseStatus) == ReuseContextEnabled
}

// Push http server push
Expand Down Expand Up @@ -626,8 +625,5 @@ func NewContext(resp http.ResponseWriter, req *http.Request) *Context {
c := &Context{}
c.Request = req
c.Response = resp
if resp != nil {
c.Headers = resp.Header()
}
return c
}
Loading

0 comments on commit 3e706ec

Please sign in to comment.