chore: update go module, readme
This commit is contained in:
parent
5263cba44a
commit
66d48d631e
59
readme.md
59
readme.md
@ -5,8 +5,9 @@
|
|||||||
##### basic usage
|
##### basic usage
|
||||||
|
|
||||||
- get param
|
- get param
|
||||||
```go
|
|
||||||
func main() {
|
```go
|
||||||
|
func main() {
|
||||||
app := nf.New()
|
app := nf.New()
|
||||||
|
|
||||||
app.Get("/hello/:name", func(c *nf.Ctx) error {
|
app.Get("/hello/:name", func(c *nf.Ctx) error {
|
||||||
@ -15,12 +16,13 @@ func main() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
log.Fatal(app.Run("0.0.0.0:80"))
|
log.Fatal(app.Run("0.0.0.0:80"))
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
- parse request query
|
- parse request query
|
||||||
```go
|
|
||||||
func handleQuery(c *nf.Ctx) error {
|
```go
|
||||||
|
func handleQuery(c *nf.Ctx) error {
|
||||||
type Req struct {
|
type Req struct {
|
||||||
Name string `query:"name"`
|
Name string `query:"name"`
|
||||||
Addr []string `query:"addr"`
|
Addr []string `query:"addr"`
|
||||||
@ -36,12 +38,13 @@ func handleQuery(c *nf.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return c.JSON(nf.Map{"query": req})
|
return c.JSON(nf.Map{"query": req})
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
- parse application/json body
|
- parse application/json body
|
||||||
```go
|
|
||||||
func handlePost(c *nf.Ctx) error {
|
```go
|
||||||
|
func handlePost(c *nf.Ctx) error {
|
||||||
type Req struct {
|
type Req struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Addr []string `json:"addr"`
|
Addr []string `json:"addr"`
|
||||||
@ -63,5 +66,37 @@ func handlePost(c *nf.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return c.JSON(nf.Map{"struct": req, "map": reqMap})
|
return c.JSON(nf.Map{"struct": req, "map": reqMap})
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
- pass local value
|
||||||
|
|
||||||
|
```go
|
||||||
|
type User struct {
|
||||||
|
Id int
|
||||||
|
Username string
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
app := nf.New()
|
||||||
|
app.Use(auth())
|
||||||
|
|
||||||
|
app.Get("/item/list", list)
|
||||||
|
}
|
||||||
|
|
||||||
|
func auth() nf.HandlerFunc {
|
||||||
|
return func(c *nf.Ctx) error {
|
||||||
|
c.Locals("user", &User{Id: 1, Username:"user"})
|
||||||
|
return c.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func list(c *nf.Ctx) error {
|
||||||
|
user, ok := c.Locals("user").(*User)
|
||||||
|
if !ok {
|
||||||
|
return c.Status(401).SendString("login required")
|
||||||
|
}
|
||||||
|
|
||||||
|
...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
Loading…
x
Reference in New Issue
Block a user