Golax, a go implementation for the Lax framework.
Clone or download
Latest commit 4a347c8 Jun 3, 2018
Permalink
Failed to load latest commit information.
doc Add support for google operations Jun 1, 2017
example
.gitignore Add panic message to description and avoid Headers duplication Nov 17, 2017
.travis.yml
CODE_OF_CONDUCT.md
LICENSE add license Jan 30, 2016
Makefile Fix testing issue Feb 24, 2016
README.md
api.go Solve some golint warnings May 22, 2018
api_test.go
context.go Solve some golint warnings May 22, 2018
default_handler_500_test.go Add handler_500 error Sep 28, 2017
doc.go Solve some golint warnings May 22, 2018
extended_writer.go
extended_writer_test.go Solve some golint warnings May 22, 2018
handler.go
icon.png
icon2.png
interceptor.go Solve some golint warnings May 22, 2018
interceptordeep_test.go Solve some golint warnings May 22, 2018
interceptors.go Solve some golint warnings May 22, 2018
logo.png Add icon and logo Jan 30, 2016
node.go
operation.go Solve some golint warnings May 22, 2018
operation_test.go Fix: Append operation to c.PathHandlers Sep 28, 2017
request_test.go "gopherize" code style Feb 4, 2016
response_test.go "gopherize" code style Feb 4, 2016
splittail.go Solve some golint warnings May 22, 2018
splittail_test.go Add support for google operations Jun 1, 2017
world_test.go

README.md

GoDoc

Golax is the official go implementation for the Lax framework.

Related docs:

About Lax

Lax wants to be the best "user experience" for developers making REST APIs.

The design principles for Lax are:

  • The lowest language overhead
  • Extremely fast to develop
  • Very easy to read and trace.

Getting started

my_api := golax.NewApi()

my_api.Root.
    Interceptor(golax.InterceptorError).
    Interceptor(myLogingInterceptor)

my_api.Root.Node("hello").
    Method("GET", func(c *golax.Context) {
        // At this point, Root interceptors has been already executed
        fmt.Fprintln(c.Response, "Hello world!")
    })

my_api.Serve()

Routing example

Routing is based on nodes.

There are three types: static, regex and parameter.

  • static: Only matches with the url part if it is exactly the same.
  • regex: Surrounded by ( and ), if the regex match.
  • parameter: Surrounded by { and }, always matches.

Performance

The performance compared with the most popular alternative is very similar (actually golax performs slightly better) however code readability and maintainability is far better with golax implementation.

Tests has been executed in a Intel(R) Core(TM) i5-3210M CPU @ 2.50GHz.

Learn more about this https://github.com/fulldump/golax-performance.

How interceptor works

If I want to handle a GET /users/1234/stats request, all interceptors in nodes from <root> to .../stats are executed:

Normal flow

To abort the execution, call to c.Error(404, "Resource not found"):

Break flow

Handling parameters

my_api := golax.NewApi()

my_api.Root.
    Node("users").
    Node("{user_id}").
    Method("GET", func (c *golax.Context) {
        fmt.Fprintln(c.Response, "You are looking for user " + c.Parameter)
    })

my_api.Serve()

It is also possible get all parameters:

func (c *golax.Context) {
    fmt.Fprintln(c.Response, "All parameters:", c.Parameters)
}

Support for Google custom methods

According to Google's API design guidelines to map RPC services to REST HTTP, it describes custom methods as extra operations that can not be easyly mapped to HTTP verbs. More info about custom methods

For example, this URL has a custom method :activate:

https://my.service.com/v1/users/31231231231:activate

Golax support custom methods as operations:

my_api.Root.
    Node("v1").
    Node("users").
    Node("{user_id}").
    Operation("activate").
    Method("POST", func(c *golax.Context) {
        user_id := c.Parameters["{user_id}"]"
        fmt.Fprintln(c.Response, "Here is custom method ':activate' for user "+user_id)
    })

Sample use cases

TODO: put here some examples to cover cool things:

  • fluent implementation
  • node cycling
  • readability
  • node preference
  • sample logging interceptor
  • sample auth interceptor
  • sample api errors