Skip to content

xgfone/go-apiserver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-apiserver

Build Status GoDoc License Minimum Go Version Latest SemVer

The library is used to build an API server.

Install

$ go get -u github.com/xgfone/go-apiserver

Example

Simple HTTP Router

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
}