feat: add nft(nf_util) package: resp(wrap status, data, msg ...); add config: enable_not_impled_handler(default return 501 not impled)

This commit is contained in:
loveuer
2024-01-29 15:48:09 +08:00
parent 7b62a82b42
commit 79e94dfd21
9 changed files with 254 additions and 16 deletions

View File

@ -1,2 +1,6 @@
### basic - get
GET http://127.0.0.1/hello/nf
GET http://127.0.0.1/hello/nf
### test resp error
GET http://127.0.0.1/error

View File

@ -1,24 +1,30 @@
package main
import (
"errors"
"github.com/loveuer/nf"
"github.com/loveuer/nf/nft/resp"
"log"
"net"
"time"
)
func main() {
app := nf.New()
app := nf.New(nf.Config{EnableNotImplementHandler: true})
app.Get("/hello/:name", func(c *nf.Ctx) error {
name := c.Param("name")
return c.JSON(nf.Map{"status": 200, "data": "hello, " + name})
})
app.Get("/not_impl")
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})
})
app.Get("/error", func(c *nf.Ctx) error {
return resp.RespError(c, resp.NewError(404, "not found", errors.New("NNNot Found"), nil))
})
ln, _ := net.Listen("tcp", ":80")
log.Fatal(app.RunListener(ln))