Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/fulldump/golax

Golax, a go implementation for the Lax framework.
https://github.com/fulldump/golax

Last synced: about 1 month ago
JSON representation

Golax, a go implementation for the Lax framework.

Awesome Lists containing this project

README

        




GoDoc

Golax is the official go implementation for the _Lax_ framework.

- [About Lax](#about-lax)
- [Getting started](#getting-started)
- [Routing example](#routing-example)
- [Performance](#performance)
- [How interceptor works](#how-interceptor-works)
- [Handling parameters](#handling-parameters)
- [Support for Google custom methods](#support-for-google-custom-methods)
- [Sample use cases](#sample-use-cases)

Related docs:

* [Developer notes](doc/developer.md)
* [TODO list](doc/todo.md)

## 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

```go
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](http://www.gorillatoolkit.org/) 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 `` to `.../stats` are executed:

![Normal flow](doc/figure_1_normal_flow.png)

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

![Break flow](doc/figure_2_break_flow.png)

## Handling parameters

```go
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:

```go
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](https://cloud.google.com/apis/design/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:

```go
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