Skip to content
This repository has been archived by the owner on Dec 23, 2023. It is now read-only.

Context enhancement #27

Merged
merged 38 commits into from
Apr 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
8554735
Add Context.JSON
razonyang Apr 2, 2020
c632a4d
Add Context.String
razonyang Apr 2, 2020
f88af6f
Fix typo
razonyang Apr 2, 2020
ad625b1
Improve Context.JSON unit test
razonyang Apr 2, 2020
d8ea2f4
Add Context.XML
razonyang Apr 2, 2020
01ad987
Update Context.JSON unit test
razonyang Apr 2, 2020
eebb001
Content.SetContentTypeJSON and Content.SetContentTypeXML append chars…
razonyang Apr 2, 2020
1826de2
Add Context.HTML
razonyang Apr 2, 2020
a514ae7
Add Context.Cookie and Context.Cookies
razonyang Apr 2, 2020
6ea73f9
Add Context.FormValue
razonyang Apr 2, 2020
ac7b896
Add Context.PostFormValue
razonyang Apr 2, 2020
4cfb0b5
Add Context.QueryString
razonyang Apr 3, 2020
92a4ace
Add Context.QueryParams and Context.QueryParam
razonyang Apr 3, 2020
200bbc6
Add Context.JSONP and Context.JSONPCallback
razonyang Apr 3, 2020
82d0c78
Update README
razonyang Apr 3, 2020
87e6e98
Add Context.Render
razonyang Apr 3, 2020
f273951
Add Context.RouteURL to generate URL of the naming route
razonyang Apr 3, 2020
2312e70
Update README
razonyang Apr 3, 2020
f3164b3
Fix typo
razonyang Apr 3, 2020
8a90ec8
Use sync.Pool for retrieving buffer
razonyang Apr 3, 2020
f4fae86
Update README
razonyang Apr 3, 2020
aec1c54
Fix unit test of Context.RouteURL
razonyang Apr 3, 2020
30fd5e7
Minor improvement
razonyang Apr 3, 2020
662c236
Remove init()
razonyang Apr 3, 2020
1bea186
Add Content-Type constants
razonyang Apr 3, 2020
3da7518
Add Context.Emit and Context.Blob
razonyang Apr 3, 2020
7a4cf62
Add Context.HtmlBlob
razonyang Apr 3, 2020
e4c07bd
Update CHANGELOG.md
razonyang Apr 3, 2020
fc9e18a
Add Context.XMLBlob
razonyang Apr 3, 2020
5daba16
Add Context.JSONBlob
razonyang Apr 3, 2020
8ea7fb3
Clean up unit test
razonyang Apr 3, 2020
f3ee58c
Add Context.JSONPBlob and Context.JSONPCallbackBlob
razonyang Apr 3, 2020
cd5a20a
JSONP get rid of fmt
razonyang Apr 3, 2020
5d6bd4d
Move params unit test to params_test.go
razonyang Apr 4, 2020
4aa2e7f
Update unit test
razonyang Apr 4, 2020
0f2dd6e
Add RecoveryLogger
razonyang Apr 4, 2020
b02c65b
Update README
razonyang Apr 4, 2020
fb396b1
Release v1.9.0
razonyang Apr 4, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,28 @@ Change Log
under development
-----------------

v1.9.0 April 4, 2020
--------------------
- Add `Context.JSON` to send JSON response.
- Add `Context.String` to send string response.
- Add `Context.XML` to send XML response.
- `Content.SetContentTypeJSON` and `Content.SetContentTypeXML` append `charset=utf-8` to content type header.
- Add `Context.HTML` to send HTML response.
- Add `Context.Cookie` and `Context.Cookies`.
- Add `Context.FormValue`.
- Add `Context.PostFormValue`.
- Add `Context.QueryString`.
- Add `Context.QueryParams` and `Context.QueryParam`.
- Add `Context.JSONP` and `Context.JSONPCallback`.
- Add `Context.Render` to render a template.
- Add `Context.RouteURL` to generate URL of the naming route.
- Add `Context.Emit` and `Context.Blob`.
- Add `Context.HtmlBlob`.
- Add `Context.XMLBlob`.
- Add `Context.JSONBlob`.
- Add `Context.JSONPBlob` and `Context.JSONPCallbackBlob`.
- Add `RecoveryLogger`.

v1.8.1 April 2, 2020
--------------------
- Fix `WrapHH` doesn't returns the error of final handle.
Expand Down
3 changes: 2 additions & 1 deletion CREDIT.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ Creadit

- Thanks to all [contributors](https://github.com/clevergo/clevergo/graphs/contributors).
- [Razon Yang](https://github.com/razonyang): author and maintainer of [CleverGo](https://github.com/clevergo).
- [Julien Schmidt](https://github.com/julienschmidt): the author of [julienschmidt/httprouter](https://github.com/julienschmidt/httprouter).
- [Julien Schmidt](https://github.com/julienschmidt): the author of [julienschmidt/httprouter](https://github.com/julienschmidt/httprouter).
- [Echo](https://github.com/labstack/echo): Renderer is inspired by Echo framework.
42 changes: 38 additions & 4 deletions README-ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,11 @@ import (
)

func home(ctx *clevergo.Context) error {
ctx.WriteString("hello world")
return nil
return ctx.String(http.StatusOK, "hello world")
}

func hello(ctx *clevergo.Context) error {
ctx.WriteString(fmt.Sprintf("hello %s", ctx.Params.String("name")))
return nil
return ctx.HTML(http.StatusOk, fmt.Sprintf("hello %s", ctx.Params.String("name")))
}

func main() {
Expand All @@ -74,6 +72,38 @@ func main() {
}
```

### 响应

```go
func text(ctx *clevergo.Context) error {
return ctx.String(http.StatusOK, "hello world")
}

func html(ctx *clevergo.Context) error {
return ctx.HTML(http.StatusOK, "<html><body>hello world</body></html>")
}

func json(ctx *clevergo.Context) error {
return ctx.JSON(http.StatusOK, data)
}

func jsonp(ctx *clevergo.Context) error {
// equals to ctx.JSONPCallback(http.StatusOK, "callback", data)
return ctx.JSONP(http.StatusOK, data)
}

func xml(ctx *clevergo.Context) error {
return ctx.XML(http.StatusOK, data)
}

// 渲染一个模板,需要注册 Render。
// https://github.com/clevergo/jetrenderer
// router.Renderer = jetrenderer.New(jet.NewHTMLSet("./views"))
func render(ctx *clevergo.Context) error {
return ctx.Render(http.StatusOK, view, data)
}
```

### 参数

可以通过多种方式获取各种类型的参数值。
Expand Down Expand Up @@ -112,6 +142,10 @@ router.NotFound = http.FileServer(http.Dir("public"))
queryPost := func (ctx *clevergo.Context) error {
// 通过匹配路由生成 URL
url, _ := ctx.Route.URL("year", "2020", "month", "02", "slug", "hello world")

// 通过命名路由生成 URL
// url, _ := ctx.RouteURL("post", "year", "2020", "month", "02", "slug", "hello world")

return nil
}

Expand Down
46 changes: 40 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,11 @@ import (
)

func home(ctx *clevergo.Context) error {
ctx.WriteString("hello world")
return nil
return ctx.String(http.StatusOK, "hello world")
}

func hello(ctx *clevergo.Context) error {
ctx.WriteString(fmt.Sprintf("hello %s", ctx.Params.String("name")))
return nil
return ctx.HTML(http.StatusOk, fmt.Sprintf("hello %s", ctx.Params.String("name")))
}

func main() {
Expand All @@ -72,6 +70,38 @@ func main() {
}
```

### Response

```go
func text(ctx *clevergo.Context) error {
return ctx.String(http.StatusOK, "hello world")
}

func html(ctx *clevergo.Context) error {
return ctx.HTML(http.StatusOK, "<html><body>hello world</body></html>")
}

func json(ctx *clevergo.Context) error {
return ctx.JSON(http.StatusOK, data)
}

func jsonp(ctx *clevergo.Context) error {
// equals to ctx.JSONPCallback(http.StatusOK, "callback", data)
return ctx.JSONP(http.StatusOK, data)
}

func xml(ctx *clevergo.Context) error {
return ctx.XML(http.StatusOK, data)
}

// Renders a template, you should register renderer first.
// https://github.com/clevergo/jetrenderer
// router.Renderer = jetrenderer.New(jet.NewHTMLSet("./views"))
func render(ctx *clevergo.Context) error {
return ctx.Render(http.StatusOK, view, data)
}
```

### Params

There are some useful functions to retrieve the parameter value.
Expand Down Expand Up @@ -110,12 +140,16 @@ router.NotFound = http.FileServer(http.Dir("public"))
queryPost := func (ctx *clevergo.Context) error {
// generates URL by matched route.
url, _ := ctx.Route.URL("year", "2020", "month", "02", "slug", "hello world")

// generates URL by naming route.
// url, _ := ctx.RouteURL("post", "year", "2020", "month", "02", "slug", "hello world")

return nil
}

router.Get("/posts/:year/:month/:slug", queryPost, router.RouteName("post"))

// generates URL by named route.
// generates URL by naming route.
url, _ := router.URL("post", "year", "2020", "month", "02", "slug", "hello world")
```

Expand All @@ -142,7 +176,7 @@ router.ErrorHandler = MyErrorHandler{

### Middleware

Middleware is a function `func(next Handle) Handle`. [WrapHH](https://pkg.go.dev/github.com/clevergo/clevergo?tab=doc#WrapHH) is an adapter that converts `func(http.Handler) http.Handler` as a middleware.
Middleware is a function `func(next Handle) Handle`. [WrapHH](https://pkg.go.dev/github.com/clevergo/clevergo?tab=doc#WrapHH) is an adapter that converts `func(http.Handler) http.Handler` to a middleware.

**Built-in middlewares:**

Expand Down