The library is used to build an API server.
$ go get -u github.com/xgfone/go-apiserver
package main
import (
"fmt"
"net/http"
"github.com/xgfone/go-apiserver/http/middleware"
"github.com/xgfone/go-apiserver/http/middleware/context"
"github.com/xgfone/go-apiserver/http/middleware/logger"
"github.com/xgfone/go-apiserver/http/middleware/recover"
"github.com/xgfone/go-apiserver/http/reqresp"
"github.com/xgfone/go-apiserver/http/router"
"github.com/xgfone/go-apiserver/http/router/ruler"
"github.com/xgfone/go-apiserver/http/server"
)
func httpHandler(route string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
c := reqresp.GetContext(r.Context())
fmt.Fprintf(w, "%s: %s", route, c.Data["id"])
}
}
func main() {
// Build the routes.
routeManager := ruler.NewRouter()
routeManager.Path("/path1/{id}").Method("GET").Handler(httpHandler("route1"))
routeManager.Path("/path2/{id}").GET(httpHandler("route2"))
router := router.NewRouter(routeManager)
router.Use(middleware.MiddlewareFunc(context.Context)) // Use a function as middleware
router.Use(
middleware.New("logger", 1, logger.Logger), // Use the named priority middleware
middleware.New("recover", 2, recover.Recover), // Use the named priority middleware
)
// http.ListenAndServe("127.0.0.1:80", router)
server.Start("127.0.0.1:80", router)
// Open a terminal and run the program:
// $ go run main.go
//
// Open another terminal and run the http client:
// $ curl http://127.0.0.1/path1/123
// route1: 123
// $ curl http://127.0.0.1/path2/123
// route2: 123
}