Golax, a go implementation for the Lax framework.

Related tags

Web Frameworks golax
Overview

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
Comments
  • Add Parameters map to context

    Add Parameters map to context

    Add parameters, for example, this handler: /{param1}/{param2}/{param3} With this url: /a/b/c Should be collected in golax.Context.Parameters:

    func my_handler(c *golax.Context) {
        c.Parameters // <<<
    }
    

    c.Parameters should be a map[string]string in the example:

    {
        "{param1}":"a",
        "{param2}":"b",
        "{param3}":"c"
    }
    

    Or this variant:

    {
        "param1":"a",
        "param2":"b",
        "param3":"c"
    }
    
    feature 
    opened by fulldump 1
  • Gopherize naming

    Gopherize naming

    By @joeybloggs

    function names + variable names should not contain underscores i.e. is_paramter should be isParameter, otherwise all of the linting tools will complain and tools like goreportcard.com will give your code a lower score.

    opened by fulldump 1
  • Context should retrieve the `handling path` and the `real path`

    Context should retrieve the `handling path` and the `real path`

    A path with parameters has 2 forms:

    • The HTTP path: /users/32123234/profile
    • The "nodes" or "handling" path: /users/{user_id}/profile

    Http path is useful for logging, but handling path is useful for stats.

    The proposal is to add PathHttp and PathHandlers to Context.

    feature 
    opened by fulldump 1
  • Support for `Context.Parameters`

    Support for `Context.Parameters`

    Issue #20 and Issue #22 In other words:

    a.Root.Node("{user_id}").Node("friends").Node("{friend_id}").
    Method("GET", func(c *Context) {
        fmt.Println("user_id:", c.Parameters["user_id"])
        fmt.Println("friend_id:", c.Parameters["friend_id"])
    })
    
    opened by fulldump 0
  • Parameter value is lost

    Parameter value is lost

    CRITICAL BUG For example, the following code demonstrate it:

    golax.NewApi.
        Root.
        Node("users").
        Node("{aa}").
        Interceptor(&golax.Interceptor{
            Before: func(c *golax.Context) {
                fmt.Println("Parameter!!!", c.Parameter)
            },
        }).
        Node("profile").Method("GET", func(c *golax.Context) {
            fmt.Fprintln(c.Response, "hi user")
        }).
        Serve()
    

    Fails misserably when:

    curl http://localhost:8000/users/22/profile
    
    bug 
    opened by fulldump 0
  • Allow regex expression in nodes with parameters

    Allow regex expression in nodes with parameters

    golax.NewApi().Root.Node("{everithing}")  // Parameter, accepts all
    golax.NewApi().Root.Node("([0-9]{1-10})") // Parameter, accepts if regex matches
    
    feature 
    opened by fulldump 0
  • Header `Host`

    Header `Host`

    By default, golang remove the Host header from its request.Headers() and put in its specific attribute request.Host. Unnaceptable! Maybe there is the same behaviour in other standard headers.

    enhancement 
    opened by fulldump 0
  • strings.ToUpper(r.Method)

    strings.ToUpper(r.Method)

    By @joeybloggs:

    No need to uppercase the request method i.e. method := strings.ToUpper(r.Method), it will always be uppercase on the request object ( unless otherwise manipulated )

    enhancement 
    opened by fulldump 0
  • Interceptors in methods?

    Interceptors in methods?

    If an interceptor should affect only a method (or two methods, for example GET and PUT):

    var my_interceptor := &golax.Interceptor{
        Before: func (c *golax.Context) {
            if "GET" == c.Request.Method || "PUT" == c.Request.Method {
                // Do things
            }
        },
    }
    

    If we implement the "MethodInterceptor":

    a := golax.NewApi()
    a.Root.
        MethodInterceptor("GET", func (c *golax.Context) {}, &golax.Interceptor{}).
        Method("POST", func (c *golax.Context) {}).
        Node("...")
    

    Or even worse: we will lose node chaining:

    a := golax.NewApi()
    a.Root.
        Node("a").
        Method("GET", func (c *golax.Context){}).
        Interceptor(func (c *golax.Context{}).   // interceptor added to method GET
        Interceptor(func (c *golax.Context{}).   // other interceptor added to method GET
        Doc(golax.Doc{}).  // Documentation added to method GET
        BackNode().  // Node a again
        Node("b").   // Nodes chain: /a/b
    

    Here is a good proposal!

    a := golax.NewApi()
    
    a.Root.
        Node("a").
        Doc(golax.Doc{Description: "Documentation for node 'a' "}).
        Interceptor(interceptor_one).   // Interceptor one belongs to 'a'
        Interceptor(interceptor_two).   // Interceptor two belongs to 'a'
        Method("GET", func (c *golax.Context) ).   // GET /a
        Doc(golax.Doc{Description: "Documentation for node a, method GET}).
        Interceptor(interceptor_X).    // Interceptor X belongs to node 'a', method 'GET'
        Interceptor(interceptor_three).    // Interceptor three belongs to node 'a', method 'GET'
        Method("POST", func (c *golax.Context) ).  // POST /a
        Doc(golax.Doc{Description: "Documentation for node a, method POST}).
        Interceptor(interceptor_X).    // Interceptor X belongs to node 'a', method 'POST'
        Interceptor(interceptor_four).    // Interceptor four belongs to node 'a', method 'POST'
        // HERE IS THE THING:  
        // `Node` method inside a `Method` has a implicit `BackNode`
        Node("b").   // Node 'b' is attached to node 'a'
        Interceptor(interceptor_seven).   // Interceptor seven belongs to node '/a/b'
        Method("GET", ....
    
    question 
    opened by fulldump 0
  • Add redirect 301

    Add redirect 301

    If a url like /users/ is received, the final slash is removed automatically, and it matches with the following nodes chain: <root> -> users However, it should match with: <root> -> users -> <empty string> So, the typical CRUD in the examples should be these nodes:

    <root>
        users
            <empty>  (handle GET, POST) `/users/`
            {user_id}  (handle GET, PATCH, DELETE) `/users/22`
    

    Finally, if the url matches <root> -> users, (the url_parts array still contains an empty item) so in this case, return an automatic redirect to the same url with slash (better a relative url).

    NOTE: this way needs an extra node for users

    enhancement feature 
    opened by fulldump 0
  • Powerize router processing

    Powerize router processing

    The main idea is that each node process the entire pending path and consume a part of a url, for example: GET /one/two/three/do

    Imagine an API with the following nodes: / -> one -> path -> do

    1. The node one receives /one/two/three/do and consumes /one
    2. The node path receives /two/three/do and consumes /two/three
    3. The node do receives /do and consumes /do
    4. All the url has been consumed, so the framework executed the method 'GET' for that node

    NOTE: this feature changes the way urls are processed, can introduce border cases to treat and can affect the performance.

    enhancement feature 
    opened by fulldump 0
Releases(v0.6.3)
Owner
Gerardo JT
Gerardo JT
Golanger Web Framework is a lightweight framework for writing web applications in Go.

/* Copyright 2013 Golanger.com. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except

golanger 299 Mar 3, 2022
The jin is a simplified version of the gin web framework that can help you quickly understand the core principles of a web framework.

jin About The jin is a simplified version of the gin web framework that can help you quickly understand the core principles of a web framework. If thi

null 8 Jul 14, 2022
laravel for golang,goal,fullstack framework,api framework

laravel for golang,goal,fullstack framework,api framework

桥边红药 17 Feb 24, 2022
Example Golang API backend rest implementation mini project Point Of Sale using Gin Framework and Gorm ORM Database.

Example Golang API backend rest implementation mini project Point Of Sale using Gin Framework and Gorm ORM Database.

Restu Wahyu Saputra 25 Aug 23, 2022
Gin is a HTTP web framework written in Go (Golang).

Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.

Gin-Gonic 62.9k Sep 19, 2022
An ideally refined web framework for Go.

Air An ideally refined web framework for Go. High-performance? Fastest? Almost all web frameworks are using these words to tell people that they are t

Aofei Sheng 418 Sep 14, 2022
An opinionated productive web framework that helps scaling business easier.

appy An opinionated productive web framework that helps scaling business easier, i.e. focus on monolith first, only move to microservices with GRPC la

appist 127 Sep 12, 2022
BANjO is a simple web framework written in Go (golang)

BANjO banjo it's a simple web framework for building simple web applications Install $ go get github.com/nsheremet/banjo Example Usage Simple Web App

Nazarii Sheremet 19 Jan 24, 2022
beego is an open-source, high-performance web framework for the Go programming language.

Beego Beego is used for rapid development of enterprise application in Go, including RESTful APIs, web apps and backend services. It is inspired by To

astaxie 532 Sep 25, 2022
High performance, minimalist Go web framework

Supported Go versions As of version 4.0.0, Echo is available as a Go module. Therefore a Go version capable of understanding /vN suffixed imports is r

LabStack LLC 23.9k Sep 28, 2022
⚡️ Express inspired web framework written in Go

Fiber is an Express inspired web framework built on top of Fasthttp, the fastest HTTP engine for Go. Designed to ease things up for fast development w

Fiber 22.4k Sep 19, 2022
Go web framework with a natural feel

Fireball Overview Fireball is a package for Go web applications. The primary goal of this package is to make routing, response writing, and error hand

Zack Patrick 58 Jul 22, 2022
Flexible E-Commerce Framework on top of Flamingo. Used to build E-Commerce "Portals" and connect it with the help of individual Adapters to other services.

Flamingo Commerce With "Flamingo Commerce" you get your toolkit for building fast and flexible commerce experience applications. A demoshop using the

Flamingo 354 Sep 20, 2022
Gearbox :gear: is a web framework written in Go with a focus on high performance

gearbox ⚙️ is a web framework for building micro services written in Go with a focus on high performance. It's built on fasthttp which is up to 10x fa

Gearbox 672 Sep 27, 2022
A small and evil REST framework for Go

go-rest A small and evil REST framework for Go Reflection, Go structs, and JSON marshalling FTW! go get github.com/ungerik/go-rest import "github.com/

Erik Unger 124 Sep 7, 2022
Goa is a web framework based on middleware, like koa.js.

Goa Goa is under construction, if you are familiar with koa or go and interested in this project, please join us. What is goa? goa = go + koa Just lik

null 46 May 17, 2022
:golf: The Golf web framework

A fast, simple and lightweight micro-web framework for Go, comes with powerful features and has no dependencies other than the Go Standard Library. Ho

null 258 Sep 9, 2022
The web framework for writing faster sites, faster

Gondola The web framework for writing faster sites, faster. Written in Go. View documentation at http://gondolaweb.com. Unless indicated otherwise at

Rainy Cape S.L. 309 Jan 24, 2022
Lightweight web framework based on net/http.

Goweb Light weight web framework based on net/http. Includes routing middleware logging easy CORS (experimental) Goweb aims to rely only on the standa

Travis Harmon 32 Aug 29, 2022