Skip to content
This repository has been archived by the owner on Dec 23, 2023. It is now read-only.
/ clevergo Public archive

👅 CleverGo is a lightweight, feature rich and high performance HTTP router for Go.

License

Notifications You must be signed in to change notification settings

clevergo/clevergo

Repository files navigation

CleverGo

Build Status Coverage Status Go Report Card GoDoc

简体中文

CleverGo is a lightweight, feature-rich and trie based high performance HTTP request router.

Contents

Benchmark

Date: 2020/02/11

Lower is better!

Benchmark

Features

  • High Performance: see Benchmark shown above.
  • Reverse Route Generation: there are two ways to generate URL by a route: named route and matched route.
  • Route Group: as known as subrouter, see route group.
  • Friendly to APIs: it is easy to design RESTful APIs and versioning your APIs by route group.
  • Middleware: allow to plug middleware in route group or particular route, supports global middleware as well, see middleware exmaple.

Examples

package main

import (
	"fmt"
	"net/http"

	"github.com/clevergo/clevergo"
)

func home(ctx *clevergo.Context) {
	ctx.WriteString("hello world")
}

func hello(ctx *clevergo.Context) {
	ctx.WriteString(fmt.Sprintf("hello %s", ctx.Params.String("name")))
}

func main() {
	router := clevergo.NewRouter()
	router.Get("/", home)
	router.Get("/hello/:name", hello)
	http.ListenAndServe(":8080", router)
}

Params

There are some useful functions to retrieve the parameter value.

func (ctx *clevergo.Context) {
	name := ctx.Params.String("name")
	page, err := ctx.Params.Int("page")
	num, err := ctx.Params.Int64("num")
	amount, err := ctx.Params.Uint64("amount")
	enable, err := ctx.Params.Bool("enable")
	price, err := ctx.Params.Float64("price")
}

Static Files

router.ServeFiles("/static/*filepath", http.Dir("/path/to/static"))

// sometimes, it is useful to treat http.FileServer as NotFoundHandler,
// such as "/favicon.ico".
router.NotFound = http.FileServer(http.Dir("public"))

Reverse Route Generation

queryPost := func (ctx *clevergo.Context) {
	// generates URL by matched route.
	url, _ := ctx.Route.URL("year", "2020", "month", "02", "slug", "hello world")
}

router.Get("/posts/:year/:month/:slug", queryPost, router.RouteName("post"))

// generates URL by named route.
url, _ := router.URL("post", "year", "2020", "month", "02", "slug", "hello world")

Middleware

Middleware is a function defined as func (clevergo.Handle) clevergo.Handle.

authenticator := func (handle clevergo.Handle) clevergo.Handle {
    return func(ctx *clevergo.Context) {
	// authenticate, terminate request if failed.
		
	// share data between middlewares and handle.
        ctx.WithValue("user", "foo")
        handle(ctx)
    }
}

router.Get("/auth", func(ctx *clevergo.Context) {
    ctx.WriteString(fmt.Sprintf("hello %s", ctx.Value("user")))
}, RouteMiddleware(
	// middleware for current route.
	authenticator,
))

// global middleware, takes gorilla compress middleware as exmaple.
http.ListenAndServe(":8080", handlers.CompressHandler(router))

Middleware also can be used in route group, see Route Group for details.

Route Group

router := clevergo.NewRouter()

api := router.Group("/api", clevergo.RouteGroupMiddleware(
    // middlewares for APIs, such as CORS, authenticator, authorization
))

apiV1 := api.Group("/v1", clevergo.RouteGroupMiddleware(
    // middlewares for v1's APIs
))

apiV2 := api.Group("v2", clevergo.RouteGroupMiddleware(
    // middlewares for v2's APIs
))

RESTful APIs

router.Get("/users", queryUsers)
router.Post("/users", createUser)
router.Get("/users/:id", queryUser)
router.Put("/users/:id", updateUser)
router.Delete("/users/:id", deleteUser)

See Route Group for versioning your APIs.

Contribute

  • Give it a ⭐ and spread the package.
  • File an issue for features or bugs.
  • Fork and make a pull request.

Credit