Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
9530fa863f | |||
f3fb259eee | |||
67c15513a2 | |||
7cf7ec32ac |
15
app.go
15
app.go
@ -139,6 +139,10 @@ func (a *App) addRoute(method, path string, handlers ...HandlerFunc) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) handleHTTPRequest(c *Ctx) {
|
func (a *App) handleHTTPRequest(c *Ctx) {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
httpMethod := c.Request.Method
|
httpMethod := c.Request.Method
|
||||||
rPath := c.Request.URL.Path
|
rPath := c.Request.URL.Path
|
||||||
unescape := false
|
unescape := false
|
||||||
@ -166,8 +170,11 @@ func (a *App) handleHTTPRequest(c *Ctx) {
|
|||||||
if value.handlers != nil {
|
if value.handlers != nil {
|
||||||
c.handlers = value.handlers
|
c.handlers = value.handlers
|
||||||
c.fullPath = value.fullPath
|
c.fullPath = value.fullPath
|
||||||
// todo
|
|
||||||
c.Next()
|
if err = c.Next(); err != nil {
|
||||||
|
serveError(c, errorHandler)
|
||||||
|
}
|
||||||
|
|
||||||
c.writermem.WriteHeaderNow()
|
c.writermem.WriteHeaderNow()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -210,8 +217,8 @@ func (a *App) handleHTTPRequest(c *Ctx) {
|
|||||||
serveError(c, a.config.NotFoundHandler)
|
serveError(c, a.config.NotFoundHandler)
|
||||||
}
|
}
|
||||||
|
|
||||||
func errorHandler(c *Ctx) {
|
func errorHandler(c *Ctx) error {
|
||||||
_ = c.Status(500).SendString(_500)
|
return c.Status(500).SendString(_500)
|
||||||
}
|
}
|
||||||
|
|
||||||
func serveError(c *Ctx, handler HandlerFunc) {
|
func serveError(c *Ctx, handler HandlerFunc) {
|
||||||
|
33
ctx.go
33
ctx.go
@ -110,17 +110,22 @@ func (c *Ctx) Cookies(key string, defaultValue ...string) string {
|
|||||||
func (c *Ctx) Next() error {
|
func (c *Ctx) Next() error {
|
||||||
c.index++
|
c.index++
|
||||||
|
|
||||||
var err error
|
if c.index >= len(c.handlers) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
for c.index < len(c.handlers) {
|
var (
|
||||||
if c.handlers[c.index] != nil {
|
err error
|
||||||
if err = c.handlers[c.index](c); err != nil {
|
handler = c.handlers[c.index]
|
||||||
|
)
|
||||||
|
|
||||||
|
if handler != nil {
|
||||||
|
if err = handler(c); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.index++
|
c.index++
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -146,11 +151,24 @@ func (c *Ctx) Form(key string) string {
|
|||||||
return c.Request.FormValue(key)
|
return c.Request.FormValue(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FormValue fiber ctx function
|
||||||
|
func (c *Ctx) FormValue(key string) string {
|
||||||
|
return c.Request.FormValue(key)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Ctx) FormFile(key string) (*multipart.FileHeader, error) {
|
func (c *Ctx) FormFile(key string) (*multipart.FileHeader, error) {
|
||||||
_, fh, err := c.Request.FormFile(key)
|
_, fh, err := c.Request.FormFile(key)
|
||||||
return fh, err
|
return fh, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Ctx) MultipartForm() (*multipart.Form, error) {
|
||||||
|
if err := c.Request.ParseMultipartForm(c.app.config.BodyLimit); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Request.MultipartForm, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Ctx) Query(key string) string {
|
func (c *Ctx) Query(key string) string {
|
||||||
return c.Request.URL.Query().Get(key)
|
return c.Request.URL.Query().Get(key)
|
||||||
}
|
}
|
||||||
@ -244,6 +262,11 @@ func (c *Ctx) SetHeader(key string, value string) {
|
|||||||
c.writermem.Header().Set(key, value)
|
c.writermem.Header().Set(key, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Ctx) SendStatus(code int) error {
|
||||||
|
c.writermem.WriteHeader(code)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Ctx) SendString(data string) error {
|
func (c *Ctx) SendString(data string) error {
|
||||||
c.SetHeader("Content-Type", "text/plain")
|
c.SetHeader("Content-Type", "text/plain")
|
||||||
_, err := c.Write([]byte(data))
|
_, err := c.Write([]byte(data))
|
||||||
|
@ -6,7 +6,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := nf.New(nf.Config{EnableNotImplementHandler: true})
|
app := nf.New(nf.Config{})
|
||||||
|
|
||||||
|
app.Get("/ok", func(c *nf.Ctx) error {
|
||||||
|
return c.SendStatus(200)
|
||||||
|
})
|
||||||
|
|
||||||
api := app.Group("/api")
|
api := app.Group("/api")
|
||||||
api.Get("/1", func(c *nf.Ctx) error {
|
api.Get("/1", func(c *nf.Ctx) error {
|
||||||
|
34
xtest/midd2/main.go
Normal file
34
xtest/midd2/main.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"github.com/loveuer/nf"
|
||||||
|
"github.com/loveuer/nf/nft/resp"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
app := nf.New()
|
||||||
|
|
||||||
|
api := app.Group("/api")
|
||||||
|
|
||||||
|
api.Get("/hello",
|
||||||
|
auth(),
|
||||||
|
func(c *nf.Ctx) error {
|
||||||
|
return resp.Resp403(c, errors.New("in hello"))
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
log.Fatal(app.Run(":80"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func auth() nf.HandlerFunc {
|
||||||
|
return func(c *nf.Ctx) error {
|
||||||
|
token := c.Query("token")
|
||||||
|
if token != "zyp" {
|
||||||
|
return resp.Resp401(c, errors.New("no auth"))
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Next()
|
||||||
|
}
|
||||||
|
}
|
33
xtest/multipart_form/main.go
Normal file
33
xtest/multipart_form/main.go
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/loveuer/nf"
|
||||||
|
"github.com/loveuer/nf/nft/resp"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
app := nf.New(nf.Config{BodyLimit: 10 * 1024 * 1024})
|
||||||
|
|
||||||
|
app.Post("/upload", func(c *nf.Ctx) error {
|
||||||
|
fs, err := c.MultipartForm()
|
||||||
|
if err != nil {
|
||||||
|
return resp.Resp400(c, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
fm := make(map[string][]string)
|
||||||
|
for key := range fs.File {
|
||||||
|
if _, exist := fm[key]; !exist {
|
||||||
|
fm[key] = make([]string, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
for f := range fs.File[key] {
|
||||||
|
fm[key] = append(fm[key], fs.File[key][f].Filename)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp.Resp200(c, nf.Map{"value": fs.Value, "files": fm})
|
||||||
|
})
|
||||||
|
|
||||||
|
log.Fatal(app.Run(":13322"))
|
||||||
|
}
|
Reference in New Issue
Block a user