Coder Social home page Coder Social logo

go-router's Introduction

GO ROUTER

Router framework with support of go-app-env for server properties.

Properties

Name Data type Remark
server.port string In address format. Ex. ":8080", "127.0.0.1:9000"
server.requestTimeout string In time.Duration format. Ex. "1m10s"
server.writeTimeout string In time.Duration format. Ex. "1m10s"

URL Parameter

Router read path parameter name and segment location from configured route that has colon(:) prefix in the path segment and put into request context with key contextKey.PathParam{} of type map[string]string

Example:

r.AddRoute(httpMethod.GET, "/users/:type/:id", handler)
r.AddRoute(httpMethod.GET, "/users/:id", handler)
r.AddRoute(httpMethod.GET, "/users/:id/items/:itemId", handler)


/**
call GET: /users/employee/123
request context key "contextKey.PathParam{}" will contains
map[type:employee id:123]

call GET: /users/123
request context key "contextKey.PathParam{}" will contains
map[id:123]

call GET: /users/123/items/i-001
request context key "contextKey.PathParam{}" will contains
map[id:123 itemId:i-001]
*/

Trace ID

Router use UUID string for trace id, any match route can access trace id from request context key contextKey.TraceId{}.

For client side can access trace id from response header name x-trace-id

Example of usage

package main
//... import
func main() {
	r := router.NewRouter(nil, nil) // default router

    /* customize router
    appEnv := goappenv.NewAppEnv(fsys)
    logger := log.New(os.Stdout, "", log.Ldate)
    r := router.NewRouter(appEnv, logger) 
    */

	r.AddRoute(httpMethod.GET, "/", api.Home)
	r.AddRoute(httpMethod.GET, "/user/:id", api.GetUser)
	server, err := r.NewServer()
	if err != nil {
		log.Fatalln("Cannot create route server", err)
	}
	log.Fatalln(server.ListenAndServe())
}

go-router's People

Contributors

natepboat avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.