aofei / air

An ideally refined web framework for Go.
https://pkg.go.dev/github.com/aofei/air
MIT License
440 stars 37 forks source link

How to write test becnhmark #21

Closed dtmkeng closed 5 years ago

dtmkeng commented 5 years ago
func airHandler(req *air.Request, res *air.Response) error {
    return nil
}
func airHandlerWrite(req *air.Request, res *air.Response) error {
    // io.WriteString(res, s)
    return res.WriteString(req.Param("name").Value().String())
}
func airHandlerTest(req *air.Request, res *air.Response) error {
    return res.WriteString(req.Path)
}

// func (a *air.Air) ServeHTTP(w http.ResponseWriter, r *http.Request) {
//  // a := air.New()
//  a.server.ServeHTTP(r, w)
// }
func loadAir(routes []route) http.Handler {
    h := airHandler
    if loadTestHandler {
        h = airHandlerTest
    }
    app := air.New()
    for _, r := range routes {
        switch r.method {
        case "GET":
            app.GET(r.path, h)
        case "POST":
            app.POST(r.path, h)
        case "PUT":
            app.PUT(r.path, h)
        case "PATCH":
            app.PATCH(r.path, h)
        case "DELETE":
            app.DELETE(r.path, h)
        default:
            panic("Unknow HTTP method: " + r.method)
        }
    }
    return app
}
func loadAirSingle(method, path string, h air.Handler) http.Handler {

    app := air.New()
    // app.Middleware.Skip(nil, h)
    switch method {
    case "GET":
        app.GET(path, h)
    case "POST":
        app.POST(path, h)
    case "PUT":
        app.PUT(path, h)
    case "PATCH":
        app.PATCH(path, h)
    case "DELETE":
        app.DELETE(path, h)
    default:
        panic("Unknow HTTP method: " + method)
    }

    return app
}

Error

./router.go:557:6: cannot define new methods on non-local type air.Air
./router.go:559:3: a.server undefined (cannot refer to unexported field or method server)
./router.go:583:2: cannot use app (type *air.Air) as type http.Handler in return argument:
        *air.Air does not implement http.Handler (missing ServeHTTP method)
./router.go:604:2: cannot use app (type *air.Air) as type http.Handler in return argument:
        *air.Air does not implement http.Handler (missing ServeHTTP method)