refactory: rebuild route tree

This commit is contained in:
loveuer
2024-02-20 15:34:00 +08:00
parent 137d4ee5c8
commit 039f4cf8c0
19 changed files with 1738 additions and 324 deletions

View File

@ -1,49 +1,16 @@
package main
import (
"errors"
"github.com/loveuer/nf"
"github.com/loveuer/nf/nft/resp"
"log"
"time"
)
func main() {
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))
})
app.Post("/data", func(c *nf.Ctx) error {
type Req struct {
Name string `json:"name"`
}
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})
api := app.Group("/api")
api.Get("/1", func(c *nf.Ctx) error {
return c.SendString("nice")
})
log.Fatal(app.Run(":80"))

View File

@ -6,7 +6,7 @@ import (
)
func main() {
app := nf.New(nf.Config{BodyLimit: -1})
app := nf.New(nf.Config{BodyLimit: 30})
app.Post("/data", func(c *nf.Ctx) error {
type Req struct {

View File

@ -11,19 +11,17 @@ func main() {
app.Get("/hello", func(c *nf.Ctx) error {
return c.SendString("world")
})
app.Get("/panic", func(c *nf.Ctx) error {
panic("panic")
})
app.Use(ml())
log.Fatal(app.Run(":7777"))
log.Fatal(app.Run(":80"))
}
func ml() nf.HandlerFunc {
return func(c *nf.Ctx) error {
log.Printf("[ML] [%s] - [%s]", c.Method, c.Path())
index := []byte(`<h1>my not found</h1>`)
c.Set("Content-Type", "text/html")
c.Status(403)
_, err := c.Write(index)
return err
}

View File

@ -10,7 +10,7 @@ func main() {
app.Get("/nice", h1, h2)
log.Fatal(app.Run(":3333"))
log.Fatal(app.Run(":80"))
}
func h1(c *nf.Ctx) error {
@ -19,7 +19,8 @@ func h1(c *nf.Ctx) error {
return c.JSON(nf.Map{"status": 201, "msg": "nice to meet you"})
}
return c.Next()
//return c.Next()
return nil
}
func h2(c *nf.Ctx) error {

View File

@ -7,7 +7,7 @@ import (
func main() {
app := nf.New(nf.Config{
DisableRecover: true,
DisableRecover: false,
})
app.Get("/hello/:name", func(c *nf.Ctx) error {