Skip to content

Commit 2363fa6

Browse files
committed
📝 Improve documents
1 parent 93db9ab commit 2363fa6

File tree

4 files changed

+36
-36
lines changed

4 files changed

+36
-36
lines changed

docs/middleware.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,35 @@ nav_order: 2
1414

1515
---
1616

17-
Vox's core concept is the middleware system. You can think of a vox application is a chain of middlewares, when a request came in, the middlewares will be executed one by one to the last.
17+
Vox's core concept is the middleware system. You can think of a vox application as a chain of middlewares. When a request comes in, the middlewares will be executed one by one.
1818

19-
The middleware can pre-process the request, for example, extract cookies from HTTP header, transform it to user object or session object, store the result in context for future usage.
19+
The middleware can pre-process the request, for example, by extracting cookies from the HTTP header, transforming them into a user or session object, and storing the result in the context for future use.
2020

21-
And the middleware can terminate the execution of next middlewares and respond to users, for authentication or input validation scenarios.
21+
A middleware can also terminate the execution of the next middlewares and respond to the user. This is useful for authentication or input validation.
2222

23-
Middleware can also modify the request or response. You can parse input data from JSON to go struct for known schema, for you do not need to process it in your main actual business handler. You can also marshall the result/error to JSON or other encoding types in one place.
23+
Middleware can also modify the request or response. You can parse input data from JSON to a Go struct for a known schema, so you don't need to process it in your main business handler. You can also marshal the result/error to JSON or other encoding types in one place.
2424

25-
Your actual business handler can be a middleware also, and this usually intends to be the last in the middleware chain.
25+
Your actual business handler can also be a middleware, and this is usually intended to be the last in the middleware chain.
2626

2727
## A basic middleware
2828

29-
The simplest middleware is change the response body to a string like this:
29+
The simplest middleware changes the response body to a string like this:
3030

3131
```go
3232
func(ctx *vox.Context, req *vox.Request, res *vox.Response) {
3333
res.Body = "Hello, world!"
3434
}
3535
```
3636

37-
The `res.Body` should write to response HTTP body, if someone opened your website, you should see the string you wrote.
37+
The `res.Body` will be written to the response HTTP body. If someone opens your website, they should see the string you wrote.
3838

39-
## Middleware for pre/post-process
39+
## Middleware for pre/post-processing
4040

41-
There is an example for do something like record the current time, and call the next middlewares, and modify the response, wrote the whole processing time for current request to the HTTP header.
41+
Here is an example of a middleware that records the current time, calls the next middleware, and then modifies the response to include the total processing time for the current request in the HTTP header.
4242

43-
Please notice the `ctx.Next()`, in this call, the execution will be moved to next middlewares in chain, and when they finished, `ctx.Next()` will be returned.
43+
Please notice the `ctx.Next()` call. This call moves the execution to the next middleware in the chain. When the next middleware finishes, `ctx.Next()` will return.
4444

45-
The `ctx.Next()` takes no argument, and do have no return. or input or output should via the request/response/context or global variables if you like.
45+
The `ctx.Next()` function takes no arguments and has no return value. Input and output should be handled through the `Request`, `Response`, and `Context` objects, or through global variables if you prefer.
4646

4747
```go
4848
func(ctx *vox.Context, req *vox.Request, res *vox.Response) {
@@ -55,7 +55,7 @@ func(ctx *vox.Context, req *vox.Request, res *vox.Response) {
5555

5656
## Terminate execution
5757

58-
This is a simple validation example, you can validate the token in the request HTTP header. If it's validated, call `ctx.Next` for future middleware executions. Otherwise, set the status code and body in response for an error message, and return to previous middlewares until the top to respond to users.
58+
This is a simple validation example. You can validate a token in the request's HTTP header. If it's validated, call `ctx.Next()` to continue to the next middleware. Otherwise, set the status code and body in the response for an error message and return to the previous middlewares until the top to respond to the user.
5959

6060
```go
6161
func(ctx *vox.Context, req *vox.Request, res *vox.Response) {

docs/request.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,26 @@ nav_order: 5
77

88
Vox's `Request` object is built on top of go's native [`net/http.Request`](https://golang.org/pkg/net/http/#Request).
99

10-
Actually, a `vox.Request` is [embedding](https://golang.org/doc/effective_go.html#embedding) a [`net/http.Request`](https://golang.org/pkg/net/http/#Request) in it's struct definitation. So you can access any [`net/http.Request`](https://golang.org/pkg/net/http/#Request)'s public fields or methods from a `vox.Request`.
10+
Actually, a `vox.Request` is [embedding](https://golang.org/doc/effective_go.html#embedding) a [`net/http.Request`](https://golang.org/pkg/net/http/#Request) in its struct definition. So you can access any of [`net/http.Request`](https://golang.org/pkg/net/http/#Request)'s public fields or methods from a `vox.Request`.
1111

12-
For example, you can access a reequest's HTTP header like this:
12+
For example, you can access a request's HTTP header like this:
1313

1414
```go
15-
func ExampleHandler(ctx *vox.Context, req *vox.Request, res *vox.Respomse) {
15+
func ExampleHandler(ctx *vox.Context, req *vox.Request, res *vox.Response) {
1616
fmt.Println("secret from request header: ", req.Header.Get("X-Secret"))
1717
}
1818
```
1919

20-
Additionally, `vox.Request` have some extra fields / methods which [`net/http.Request`](https://golang.org/pkg/net/http/#Request) dose not provides.
20+
Additionally, `vox.Request` has some extra fields/methods that [`net/http.Request`](https://golang.org/pkg/net/http/#Request) does not provide.
2121

22-
For example, vox has a `JSON` method to decode JSON request body to go values, with additional functionality to check the content-type header from the request. If the content-type header does not start with "application/json" or decode errors, this function will return an error and set the response status code to 406.
22+
For example, vox has a `JSON` method to decode a JSON request body to go values, with additional functionality to check the content-type header from the request. If the content-type header does not start with "application/json" or a decode error occurs, this function will return an error and set the response status code to 406.
2323

2424
```go
25-
func PostJSONHandler(ctx *vox.Context, req *vox.Request, res *vox.Respomse) {
25+
func PostJSONHandler(ctx *vox.Context, req *vox.Request, res *vox.Response) {
2626
body := map[string]string{}
2727
if err := req.JSON(&body); err != nil {
28-
return // You do not need to set the response's status code for vox have set it.
28+
return // You do not need to set the response's status code, as vox has already set it.
2929
}
30-
...
30+
// ...
3131
}
3232
```

docs/run.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Run
33
nav_order: 3
44
---
55

6-
# Run You Application
6+
# Run Your Application
77
{: .no_toc }
88

99
## Table of contents
@@ -16,20 +16,20 @@ nav_order: 3
1616

1717
## Run
1818

19-
You can run your vox application by simply call the `Run` method:
19+
You can run your vox application by simply calling the `Run` method:
2020

2121
```go
2222
app := vox.New()
2323
app.Run("localhost:3000")
2424
```
2525

26-
Now your application has listened on port 3000 with localhost. `Run` accept whatever [net/http.ListenAndServe](https://golang.org/pkg/net/http/#ListenAndServe) takes in first parameter.
26+
Now your application is listening on port 3000. The `Run` method accepts the same arguments as [`net/http.ListenAndServe`](https://golang.org/pkg/net/http/#ListenAndServe).
2727

28-
## Integrate with your exists HTTP server
28+
## Integrate with an existing HTTP server
2929

30-
If you have an http server in go already, you can integrate vox to it. This may help you migrate from and to vox.
30+
If you already have an HTTP server in Go, you can integrate vox with it. This can help you migrate to and from vox.
3131

32-
Actually, `vox.Application` implemented the [net/http.Handler](https://golang.org/pkg/net/http/#Handler) interface. So you can pass a `vox.Application` instance to where [net/http.Handler](https://golang.org/pkg/net/http/#Handler) are accepted, like `http.Handle`:
32+
Actually, `vox.Application` implements the [`net/http.Handler`](https://golang.org/pkg/net/http/#Handler) interface. So you can pass a `vox.Application` instance to any function that accepts a [`net/http.Handler`](https://golang.org/pkg/net/http/#Handler), like `http.Handle`:
3333

3434
```go
3535
func rawHandler(w http.ResponseWriter, _ *http.Request) {

docs/usage.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,21 @@ import (
2929
func main() {
3030
app := vox.New()
3131

32-
// custom middleware that add a x-response-time to the response header
33-
app.Use(func(ctx *vox.Context, *vox.Request, res *vox.Response) {
32+
// custom middleware that adds an x-response-time to the response header
33+
app.Use(func(ctx *vox.Context, req *vox.Request, res *vox.Response) {
3434
start := time.Now()
3535
ctx.Next()
3636
duration := time.Now().Sub(start)
3737
res.Header.Set("X-Response-Time", fmt.Sprintf("%s", duration))
3838
})
3939

4040
// router param
41-
app.Get("/hello/{name}", func(ctx *vox.Context, *vox.Request, res *vox.Response) {
41+
app.Get("/hello/{name}", func(ctx *vox.Context, req *vox.Request, res *vox.Response) {
4242
res.Body = "Hello, " + req.Params["name"] + "!"
4343
})
4444

4545
// response
46-
app.Get("/", func(ctx *vox.Context, *vox.Request, res *vox.Response) {
46+
app.Get("/", func(ctx *vox.Context, req *vox.Request, res *vox.Response) {
4747
// get the query string
4848
name := req.URL.Query().Get("name")
4949
if name == "" {
@@ -65,7 +65,7 @@ import (
6565
"github.com/aisk/vox"
6666
)
6767

68-
func handler(ctx *vox.Context, *vox.Request, res *vox.Response) {
68+
func handler(ctx *vox.Context, req *vox.Request, res *vox.Response) {
6969
// Get the current request's HTTP method and put it to the result page.
7070
res.Body = "HTTP Method is: " + req.Method
7171
}
@@ -97,7 +97,7 @@ import (
9797
"github.com/aisk/vox"
9898
)
9999

100-
func hello(ctx *vox.Context, *vox.Request, res *vox.Response) {
100+
func hello(ctx *vox.Context, req *vox.Request, res *vox.Response) {
101101
name := req.Params["name"]
102102
res.Body = "Hello, " + name + "!"
103103
}
@@ -118,7 +118,7 @@ import (
118118
"github.com/aisk/vox"
119119
)
120120

121-
func hello(ctx *vox.Context, *vox.Request, res *vox.Response) {
121+
func hello(ctx *vox.Context, req *vox.Request, res *vox.Response) {
122122
name := req.URL.Query().Get("name")
123123
res.Body = "Hello, " + name + "!"
124124
}
@@ -139,8 +139,8 @@ import (
139139
"github.com/aisk/vox"
140140
)
141141

142-
func towel(ctx *vox.Context, *vox.Request, res *vox.Response) {
143-
// Set the response body, it can be string or []byte or any thing that json.Marshal accepts.
142+
func towel(ctx *vox.Context, req *vox.Request, res *vox.Response) {
143+
// Set the response body, it can be a string, []byte, or anything that json.Marshal accepts.
144144
res.Body = "new towel is created!"
145145
// Set the response status code.
146146
res.Status = 201
@@ -178,7 +178,7 @@ type Towel struct {
178178
Size string `json:"size"`
179179
}
180180

181-
func towel(ctx *vox.Context, *vox.Request, res *vox.Response) {
181+
func towel(ctx *vox.Context, req *vox.Request, res *vox.Response) {
182182
if !strings.HasPrefix(req.Header.Get("Content-Type"), "application/json") {
183183
res.Status = http.StatusUnsupportedMediaType // or just 415
184184
// Set the body with a map, vox will marshal it to JSON automatically for you.

0 commit comments

Comments
 (0)