Skip to content
This repository has been archived by the owner on Apr 11, 2020. It is now read-only.

Commit

Permalink
refactor: update module
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Aug 15, 2019
1 parent 73ae301 commit 6e695ad
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 31 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
# cod-etag
# elton-etag

[![Build Status](https://img.shields.io/travis/vicanso/cod-etag.svg?label=linux+build)](https://travis-ci.org/vicanso/cod-etag)
[![Build Status](https://img.shields.io/travis/vicanso/elton-etag.svg?label=linux+build)](https://travis-ci.org/vicanso/elton-etag)

ETag middleware for cod, generate response's ETag header by `sha1`.
ETag middleware for elton, generate response's ETag header by `sha1`.

```go
package main

import (
"bytes"

"github.com/vicanso/cod"
"github.com/vicanso/elton"

etag "github.com/vicanso/cod-etag"
etag "github.com/vicanso/elton-etag"
)

func main() {

d := cod.New()
d := elton.New()
d.Use(etag.NewDefault())

d.GET("/", func(c *cod.Context) (err error) {
d.GET("/", func(c *elton.Context) (err error) {
c.BodyBuffer = bytes.NewBufferString("abcd")
return
})
Expand Down
16 changes: 8 additions & 8 deletions etag.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import (
"fmt"
"net/http"

"github.com/vicanso/cod"
"github.com/vicanso/elton"
)

type (
// Config eTag config
Config struct {
Skipper cod.Skipper
Skipper elton.Skipper
}
)

Expand All @@ -43,17 +43,17 @@ func gen(buf []byte) string {
}

// NewDefault create a default ETag middleware
func NewDefault() cod.Handler {
func NewDefault() elton.Handler {
return New(Config{})
}

// New create a ETag middleware
func New(config Config) cod.Handler {
func New(config Config) elton.Handler {
skipper := config.Skipper
if skipper == nil {
skipper = cod.DefaultSkipper
skipper = elton.DefaultSkipper
}
return func(c *cod.Context) (err error) {
return func(c *elton.Context) (err error) {
if skipper(c) {
return c.Next()
}
Expand All @@ -65,7 +65,7 @@ func New(config Config) cod.Handler {
// 如果无内容或已设置 ETag ,则跳过
// 因为没有内容也不生成 ETag
if bodyBuf == nil || bodyBuf.Len() == 0 ||
c.GetHeader(cod.HeaderETag) != "" {
c.GetHeader(elton.HeaderETag) != "" {
return
}
// 如果响应状态码不为0 而且( < 200 或者 >= 300),则跳过
Expand All @@ -77,7 +77,7 @@ func New(config Config) cod.Handler {
return
}
eTag := gen(bodyBuf.Bytes())
c.SetHeader(cod.HeaderETag, eTag)
c.SetHeader(elton.HeaderETag, eTag)
return
}
}
16 changes: 8 additions & 8 deletions etag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/vicanso/cod"
"github.com/vicanso/elton"
)

var testData []byte
Expand Down Expand Up @@ -40,7 +40,7 @@ func TestETag(t *testing.T) {
t.Run("curstom error", func(t *testing.T) {
assert := assert.New(t)
resp := httptest.NewRecorder()
c := cod.NewContext(resp, nil)
c := elton.NewContext(resp, nil)
customErr := errors.New("abcd")
c.Next = func() error {
return customErr
Expand All @@ -52,19 +52,19 @@ func TestETag(t *testing.T) {
t.Run("no body", func(t *testing.T) {
assert := assert.New(t)
resp := httptest.NewRecorder()
c := cod.NewContext(resp, nil)
c := elton.NewContext(resp, nil)
c.Next = func() error {
return nil
}
err := fn(c)
assert.Nil(err)
assert.Empty(c.GetHeader(cod.HeaderETag))
assert.Empty(c.GetHeader(elton.HeaderETag))
})

t.Run("error status", func(t *testing.T) {
assert := assert.New(t)
resp := httptest.NewRecorder()
c := cod.NewContext(resp, nil)
c := elton.NewContext(resp, nil)
c.Next = func() error {
c.Body = map[string]string{
"name": "tree.xie",
Expand All @@ -75,12 +75,12 @@ func TestETag(t *testing.T) {
}
err := fn(c)
assert.Nil(err)
assert.Empty(c.GetHeader(cod.HeaderETag))
assert.Empty(c.GetHeader(elton.HeaderETag))
})

t.Run("gen eTag", func(t *testing.T) {
resp := httptest.NewRecorder()
c := cod.NewContext(resp, nil)
c := elton.NewContext(resp, nil)
c.Next = func() error {
c.Body = map[string]string{
"name": "tree.xie",
Expand All @@ -92,7 +92,7 @@ func TestETag(t *testing.T) {
if err != nil {
t.Fatalf("eTag middleware fail, %v", err)
}
if c.GetHeader(cod.HeaderETag) != `"13-yo9YroUOjW1obRvVoXfrCiL2JGE="` {
if c.GetHeader(elton.HeaderETag) != `"13-yo9YroUOjW1obRvVoXfrCiL2JGE="` {
t.Fatalf("gen eTag fail")
}
})
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module github.com/vicanso/cod-etag
module github.com/vicanso/elton-etag

go 1.12

require (
github.com/stretchr/testify v1.3.0
github.com/vicanso/cod v0.1.1
github.com/vicanso/elton v0.2.0
)
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
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=
github.com/vicanso/cod v0.0.7 h1:CHyIL7zkNOhEv0JosDDyRHCUALovry1P+dLISaANj1w=
github.com/vicanso/cod v0.0.7/go.mod h1:tHGTs/JDTcp/OE4VaCutVMpYL69aR1rKUggsHviTuFc=
github.com/vicanso/cod v0.1.1 h1:l4dbZSGhGRVnrxLbArC/60GXNxDo0O/RlY5z4iHSa7I=
github.com/vicanso/cod v0.1.1/go.mod h1:T5GOazXuYrwwE1qMA0G3zka7NVwwkoL2fYIWNfeEpJw=
github.com/vicanso/hes v0.1.4 h1:n8kG8krvNJF4Sj1PvZOEUzdUsmDSbCcGr8C1nYnoP+o=
github.com/vicanso/hes v0.1.4/go.mod h1:bG0UJ3EihDKObFcLLwJYjxHHr9saFllsFcepyDIvFlo=
github.com/vicanso/elton v0.2.0 h1:QlXgmq6m+9wZN7FeLD25/EhBkl8blzppviVY5U5PTm0=
github.com/vicanso/elton v0.2.0/go.mod h1:ynAUOSkZQ+pFaUsxlG5hYnJFjPpMwz8YyEBPzNh0pSg=
github.com/vicanso/hes v0.2.1 h1:jRFEADmiQ30koVY/sKwlkhyXM5B3QbVVizLqrjNJgPw=
github.com/vicanso/hes v0.2.1/go.mod h1:QcxOFmFfBQMhASTaLgnFayXYCgevdSeBVprt+o+3eKo=
github.com/vicanso/keygrip v0.1.0 h1:/zYzoVIbREAvaxSM7bo3/oSXuuYztaP71dPBfhRoNkM=
github.com/vicanso/keygrip v0.1.0/go.mod h1:cI05iOjY00NJ7oH2Z9Zdm9eJPUkpoex3XnEubK78nho=

0 comments on commit 6e695ad

Please sign in to comment.