You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/middleware.md
+12-12Lines changed: 12 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,35 +14,35 @@ nav_order: 2
14
14
15
15
---
16
16
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.
18
18
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.
20
20
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.
22
22
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.
24
24
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.
26
26
27
27
## A basic middleware
28
28
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:
30
30
31
31
```go
32
32
func(ctx *vox.Context, req *vox.Request, res *vox.Response) {
33
33
res.Body = "Hello, world!"
34
34
}
35
35
```
36
36
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.
38
38
39
-
## Middleware for pre/post-process
39
+
## Middleware for pre/post-processing
40
40
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.
42
42
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.
44
44
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.
46
46
47
47
```go
48
48
func(ctx *vox.Context, req *vox.Request, res *vox.Response) {
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.
59
59
60
60
```go
61
61
func(ctx *vox.Context, req *vox.Request, res *vox.Response) {
Copy file name to clipboardExpand all lines: docs/request.md
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,26 +7,26 @@ nav_order: 5
7
7
8
8
Vox's `Request` object is built on top of go's native [`net/http.Request`](https://golang.org/pkg/net/http/#Request).
9
9
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`.
11
11
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:
13
13
14
14
```go
15
-
funcExampleHandler(ctx *vox.Context, req *vox.Request, res *vox.Respomse) {
15
+
funcExampleHandler(ctx *vox.Context, req *vox.Request, res *vox.Response) {
16
16
fmt.Println("secret from request header: ", req.Header.Get("X-Secret"))
17
17
}
18
18
```
19
19
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.
21
21
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.
23
23
24
24
```go
25
-
funcPostJSONHandler(ctx *vox.Context, req *vox.Request, res *vox.Respomse) {
25
+
funcPostJSONHandler(ctx *vox.Context, req *vox.Request, res *vox.Response) {
26
26
body:=map[string]string{}
27
27
iferr:= 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.
Copy file name to clipboardExpand all lines: docs/run.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ title: Run
3
3
nav_order: 3
4
4
---
5
5
6
-
# Run You Application
6
+
# Run Your Application
7
7
{: .no_toc }
8
8
9
9
## Table of contents
@@ -16,20 +16,20 @@ nav_order: 3
16
16
17
17
## Run
18
18
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:
20
20
21
21
```go
22
22
app:= vox.New()
23
23
app.Run("localhost:3000")
24
24
```
25
25
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).
27
27
28
-
## Integrate with your exists HTTP server
28
+
## Integrate with an existing HTTP server
29
29
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.
31
31
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`:
0 commit comments