2024-01-12 19:18:33 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-01-29 15:48:09 +08:00
|
|
|
"errors"
|
2024-01-12 19:18:33 +08:00
|
|
|
"github.com/loveuer/nf"
|
2024-01-29 15:48:09 +08:00
|
|
|
"github.com/loveuer/nf/nft/resp"
|
2024-01-12 19:18:33 +08:00
|
|
|
"log"
|
2024-01-14 19:10:05 +08:00
|
|
|
"time"
|
2024-01-12 19:18:33 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-01-29 15:48:09 +08:00
|
|
|
app := nf.New(nf.Config{EnableNotImplementHandler: true})
|
2024-01-12 19:18:33 +08:00
|
|
|
|
|
|
|
app.Get("/hello/:name", func(c *nf.Ctx) error {
|
|
|
|
name := c.Param("name")
|
|
|
|
return c.JSON(nf.Map{"status": 200, "data": "hello, " + name})
|
|
|
|
})
|
2024-01-29 15:48:09 +08:00
|
|
|
app.Get("/not_impl")
|
2024-01-14 19:10:05 +08:00
|
|
|
app.Patch("/world", func(c *nf.Ctx) error {
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
c.Status(404)
|
|
|
|
return c.JSON(nf.Map{"method": c.Method, "status": c.StatusCode})
|
|
|
|
})
|
2024-01-29 15:48:09 +08:00
|
|
|
app.Get("/error", func(c *nf.Ctx) error {
|
|
|
|
return resp.RespError(c, resp.NewError(404, "not found", errors.New("NNNot Found"), nil))
|
|
|
|
})
|
2024-02-01 18:06:07 +08:00
|
|
|
app.Post("/data", func(c *nf.Ctx) error {
|
|
|
|
type Req struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
2024-01-12 19:18:33 +08:00
|
|
|
|
2024-02-01 18:06:07 +08:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
req = new(Req)
|
|
|
|
rm = make(map[string]any)
|
|
|
|
)
|
|
|
|
|
|
|
|
if err = c.BodyParser(req); err != nil {
|
|
|
|
return c.JSON(nf.Map{"status": 400, "msg": err.Error()})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = c.BodyParser(&rm); err != nil {
|
|
|
|
return c.JSON(nf.Map{"status": 400, "msg": err.Error()})
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(nf.Map{"status": 200, "data": req, "map": rm})
|
|
|
|
})
|
|
|
|
|
|
|
|
log.Fatal(app.Run(":80"))
|
2024-01-12 19:18:33 +08:00
|
|
|
}
|