nf/example/simple/main.go

51 lines
785 B
Go
Raw Permalink Normal View History

2024-01-10 20:26:19 +08:00
package main
import (
"errors"
2024-01-11 18:16:41 +08:00
"fmt"
2024-01-10 20:26:19 +08:00
"log"
"nf"
2024-01-11 18:16:41 +08:00
"time"
2024-01-10 20:26:19 +08:00
)
func main() {
app := nf.New()
app.Get("/hello", func(c *nf.Ctx) error {
return c.SendString("world")
})
app.Get("/world", func(c *nf.Ctx) error {
return errors.New("nice")
})
2024-01-11 18:16:41 +08:00
v1 := app.Group("/v1")
2024-01-11 22:29:30 +08:00
v1.Use(NewRecovery())
2024-01-11 18:16:41 +08:00
v1.Get("/hello", func(c *nf.Ctx) error {
return c.JSON(nf.Map{"version": "v1", "date": time.Now()})
})
v1.Get(
"/name",
func(c *nf.Ctx) error {
2024-01-11 22:29:30 +08:00
c.Locals("name", "zyp")
panic("name")
2024-01-11 18:16:41 +08:00
return c.Next()
},
func(c *nf.Ctx) error {
2024-01-11 22:29:30 +08:00
return c.SendString(fmt.Sprintf("hi, %s", c.Locals("name1")))
},
)
v2 := v1.Group("/v2")
v2.Use(Logger())
v2.Get(
"/name",
func(c *nf.Ctx) error {
return c.SendString("hi, loveuer")
2024-01-11 18:16:41 +08:00
},
)
2024-01-10 20:26:19 +08:00
log.Fatal(app.Run(":7733"))
}