nf/example/simple/main.go

40 lines
611 B
Go
Raw 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")
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 {
c.Params["name"] = "zyp"
return c.Next()
},
func(c *nf.Ctx) error {
return c.SendString(fmt.Sprintf("hi, %s", c.Params["name"]))
},
)
2024-01-10 20:26:19 +08:00
log.Fatal(app.Run(":7733"))
}