nf/xtest/basic/main.go

32 lines
729 B
Go
Raw Permalink Normal View History

2024-01-12 19:18:33 +08:00
package main
import (
"errors"
2024-01-12 19:18:33 +08:00
"github.com/loveuer/nf"
"github.com/loveuer/nf/nft/resp"
2024-01-12 19:18:33 +08:00
"log"
2024-01-13 17:45:44 +08:00
"net"
2024-01-14 19:10:05 +08:00
"time"
2024-01-12 19:18:33 +08:00
)
func main() {
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})
})
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})
})
app.Get("/error", func(c *nf.Ctx) error {
return resp.RespError(c, resp.NewError(404, "not found", errors.New("NNNot Found"), nil))
})
2024-01-12 19:18:33 +08:00
2024-01-13 17:45:44 +08:00
ln, _ := net.Listen("tcp", ":80")
log.Fatal(app.RunListener(ln))
2024-01-12 19:18:33 +08:00
}