Skip to content

Commit

Permalink
Merge pull request #31 from ferry-go/feature/core/added-linting
Browse files Browse the repository at this point in the history
added linting
  • Loading branch information
ferry-go authored Jul 21, 2020
2 parents 6237b4d + a81d486 commit 79fc9f3
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ jobs:
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep ensure
fi
go get -u golang.org/x/lint/golint
- name: Format
run : chmod +x .github/workflows/scripts/format.sh && bash .github/workflows/scripts/format.sh

Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/scripts/format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@
if [ "$(go fmt | wc -l)" -gt 0 ]; then
echo "run go fmt before PR"
exit 1
fi

if [ "$(golint | wc -l)" -gt 0 ]; then
echo "run go lint before PR"
exit 1
fi
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ferry

import "github.com/go-playground/validator/v10"

// Config -- Configuration for ferry
type Config struct {
Validator *validator.Validate
}
15 changes: 7 additions & 8 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/valyala/fasthttp"
)

// Ctx -- route level context
type Ctx struct {
RequestCtx *fasthttp.RequestCtx
Next func() error
Expand All @@ -19,9 +20,9 @@ type Ctx struct {
queryPath string
}

// Json Sending application/json response
func (ctx *Ctx) Json(statusCode int, payload interface{}) error {
ctx.RequestCtx.Response.Header.Set(ContentType, ApplicationJson)
// JSON Sending application/json response
func (ctx *Ctx) JSON(statusCode int, payload interface{}) error {
ctx.RequestCtx.Response.Header.Set(ContentType, ApplicationJSON)
ctx.RequestCtx.SetStatusCode(statusCode)
response, err := json.Marshal(payload)
if err != nil {
Expand All @@ -38,7 +39,7 @@ func (ctx *Ctx) Send(statusCode int, payload string) error {
return nil
}

// Send Sending a text response
// SendStatusCode -- sending response without any body
func (ctx *Ctx) SendStatusCode(statusCode int) error {
ctx.RequestCtx.SetStatusCode(statusCode)
return nil
Expand Down Expand Up @@ -138,24 +139,22 @@ func (ctx *Ctx) GetParams() map[string]string {
return getParamsFromPath(ctx.routerPath, string(ctx.RequestCtx.Path()))
}

// returns value of a single query Param
// GetQueryParam returns value of a single query Param
//
// route path /hello?key=test&value=bbp
//
// keyValue = GetQueryParam(key)
//
// keyValue = test

func (ctx *Ctx) GetQueryParam(name string) string {
return getQueryParam(ctx.queryPath, name)
}

// returns map of query Params
// GetQueryParams returns map of query Params
//
// route path /hello?key=test&value=bbp
//
// returns {key : test, value : bbp}

func (ctx *Ctx) GetQueryParams() map[string]string {
return getAllQueryParams(ctx.queryPath)
}
Expand Down
6 changes: 3 additions & 3 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (suite *ContextSuite) TestJsonResponse() {
"user": "ferry",
}
suite.Ferry.Get(path, func(ctx *Ctx) error {
return ctx.Json(http.StatusOK, response)
return ctx.JSON(http.StatusOK, response)
})
r, err := http.NewRequest(GET, "http://test"+path, nil)
if assert.Nil(suite.T(), err) {
Expand All @@ -56,7 +56,7 @@ func (suite *ContextSuite) TestJsonResponse() {
if err != nil {
suite.T().Error(err)
}
assert.Equal(suite.T(), res.Header.Get(ContentType), ApplicationJson)
assert.Equal(suite.T(), res.Header.Get(ContentType), ApplicationJSON)
assert.Equal(suite.T(), res.StatusCode, http.StatusOK)
assert.Equal(suite.T(), body, bytes)
}
Expand Down Expand Up @@ -86,7 +86,7 @@ func (suite *ContextSuite) TestBind() {
suite.Ferry.Post(path, func(ctx *Ctx) error {
body := login{}
_ = ctx.Bind(&body)
return ctx.Json(http.StatusOK, body)
return ctx.JSON(http.StatusOK, body)
})
postBody, _ := json.Marshal(login{
Username: "Ferry",
Expand Down
6 changes: 3 additions & 3 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func main() {
app := ferry.InitServer(&config)

app.HandleNotFound(func(ctx *ferry.Ctx) error {
return ctx.Json(http.StatusNotFound, "check url idiot")
return ctx.JSON(http.StatusNotFound, "check url idiot")
})
app.HandleErrors(func(ctx *ferry.Ctx, err error) error {
return ctx.Send(http.StatusInternalServerError, err.Error())
Expand Down Expand Up @@ -62,14 +62,14 @@ func main() {
if err != nil {
return err
}
return ctx.Json(http.StatusOK, map[string]string{
return ctx.JSON(http.StatusOK, map[string]string{
"message": fmt.Sprintf("Welcome %s", login.Username),
})
})

app.Get("/hello", func(ctx *ferry.Ctx) error {
params := ctx.GetQueryParams()
return ctx.Json(http.StatusOK, params)
return ctx.JSON(http.StatusOK, params)
})

app.Get("/hello/single", func(ctx *ferry.Ctx) error {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ require (
github.com/go-playground/validator/v10 v10.3.0
github.com/stretchr/testify v1.6.1
github.com/valyala/fasthttp v1.14.0
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
golang.org/x/tools v0.0.0-20200721032237-77f530d86f9a // indirect
)
21 changes: 21 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,37 @@ github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyC
github.com/valyala/fasthttp v1.14.0 h1:67bfuW9azCMwW/Jlq/C+VeihNpAuJMWkYPBig1gdi3A=
github.com/valyala/fasthttp v1.14.0/go.mod h1:ol1PCaL0dX20wC0htZ7sYCsvCYmrouYra0zHzaclZhE=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b h1:Wh+f8QHJXR411sJR8/vRBTZ7YapZaRvUcLFFJhusH0k=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e h1:3G+cUijn7XD+S4eJFddp53Pv7+slrESplyjG25HgL+k=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7 h1:EBZoQjiKKPaLbPrbpssUfuHtwM6KV/vb4U85g/cigFY=
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.0.0-20200721032237-77f530d86f9a h1:kVMPw4f6EVqYdfGQTedjrpw1dbE2PEMfw4jwXsNdn9s=
golang.org/x/tools v0.0.0-20200721032237-77f530d86f9a/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
Expand Down
2 changes: 2 additions & 0 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type router struct {
handlers []handler
}

// Group -- router group
type Group struct {
path string
ferry *Ferry
Expand Down Expand Up @@ -55,6 +56,7 @@ func (g *Group) Delete(path string, h handler) {
g.addRoute(DELETE, path, h)
}

// Use -- group level middleware
func (g *Group) Use(h handler) {
g.ferry.groupMiddlewareMap[g.path] = append(g.ferry.groupMiddlewareMap[g.path], h)
}
Expand Down
6 changes: 4 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/valyala/fasthttp"
)

// Ferry -- Ferry config
type Ferry struct {
config *Config
routerMap map[string][]router
Expand All @@ -21,7 +22,7 @@ type Ferry struct {
errorHandler errHandler
}

// Init server
// InitServer -- initializing server with ferry config
func InitServer(config *Config) *Ferry {
return &Ferry{
config: config,
Expand All @@ -36,6 +37,7 @@ func requestHandler(c *fasthttp.RequestCtx, ferry *Ferry) {
appLevelMiddleware(ctx, ferry)
}

// Listen -- starting server with given host
func (ferry *Ferry) Listen(host string) error {
handler := func(c *fasthttp.RequestCtx) {
requestHandler(c, ferry)
Expand Down Expand Up @@ -66,7 +68,7 @@ func (ferry *Ferry) addRoute(method, path string, h []handler) {
})
}

// application level middleware
// Use -- application level middleware
func (ferry *Ferry) Use(h handler) {
ferry.middleware = append(ferry.middleware, h)
}
Expand Down
4 changes: 2 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import (
"strings"
)

// ...
const (
GET = http.MethodGet
POST = http.MethodPost
PUT = http.MethodPut
DELETE = http.MethodDelete

// headers
ContentType = "Content-Type"
ContentDeposition = "Content-Disposition"
ApplicationJson = "application/json"
ApplicationJSON = "application/json"
Attachment = "attachment"

routerRegexReplace = "[a-zA-Z0-9_-]*"
Expand Down

0 comments on commit 79fc9f3

Please sign in to comment.