0.0.1 • Published 3 years ago

routerling v0.0.1

Weekly downloads
-
License
-
Repository
-
Last release
3 years ago

Do Not Install - Still in Development

Routerling

A new born baby router on it's way to being a web development platform. Routerling is a router multiplexer built with 1 goal in mind.

  • Make it stupid simple to build high performance web applications

How?

Routerling has only ONE API you need to learn - Router. The API is also deliberately made consistent across the 3 different supported languages Golang, Python, Typescript/JavaScript. See Similarities

import Router, { HttpRequest, ResponseWriter, Context } from 'routerling';

import Container from './dependency.injection.example.etc.ts'; //optional


router = new Router();


router.before('*', async (req: HttpRequest, res: ResponseWriter, ctx: Context) => {
    //inversify or typedi or any other DI container if DI is desired
    const baseRepository: BaseRepository = Container.get('my-dependency')

    return ctx.baseRepository = baseRepository;
});


router.get('/users/:id', async(r: HttpRequest, w: ResponseWriter, c: Context) => {
    if(c.skip === true) return 
})


router.after(['/users/*', '/users'], async() => {
    //no-op
});

router.post('/users', async(r: HttpRequest, w: ResponseWriter, c: Context) => {
    //the order of the calls don't mean squat so go crazy
});


//host, port, debug
router.listen("localhost", 8081, true);


//-------------------------- Another Example -------------------------------------//
import { Router, Request, Response, Context } from 'routerling';


Router.before('*', (req: Request, res: Response, ctx: Context) => {
    const authorizationToken: string = request.headers.CONTENT_TYPE

    //for customer headers use the following syntax
    authorizationToken = request.headers.get('X-Auth-Token');
    if(authorizationToken) {
        return ctx.set("myPersonalDBToHoldStuff", authorizationToken)
    }

    //you can abort with the helper abort method, return is not required but used for consistent interface with golang routex
    return req.abort(401, {message: 'Unauthorized'})

    //raising an exception will also prevent further processing of the request
    //to prevent routex from sending back 500, use a custom HttpException i.e. see how to create one or use a library {message, code}
    throw new Error("something happened")
})
import (
    "fmt"
    "strings"
    rg "github.com/rayattack/routerling"
)

func init(){
    //do whatever you fancy here...
}

func main(){
    const router := rg.Router()
    router.BEFORE("*", func(r rg.HttpRequest, w *rg.ResponseWriter, ctx: *rg.Ctx){
        fmt.Println("I will be called before every request is handled because of my wildcard")
    })

    router.AFTER("customers/:id", func(r rg.HttpRequest, w *rg.ResponseWriter, ctx: *rg.Cache) error {
        authorizationToken := r.headers.get("X-Auth-Token")
        token := strings.Split(authorizationToken, " ")
        if(len(token) == 2) {
            ctx.Set("some-custom-key-as-my-db", token)
            return nil
        }
        //this will abort
        return r.abort()

        //this will
        return fmt.Errorf("This is some error alright...")
    })

    router.GET("/customers/:id", func(req *routerling.Request, res routerling.Response, ctx routerling.Context){
        //go crazy here...
    }, "optional-name-for-this")

    router.listen("localhost", 8701, true)
}

Request Object

Documentation coming soon.

Response Object

Documentation coming soon.

Context Object

Documentation coming soon.

Example Application

Documentation coming soon.

FAQs

  • What happens if I try to register the same route with two different functions?
    • Routex will throw an error you can only register a path with one handler
  • What happens if I use a specific method like GET for '/customers' then a generic one also Router.route('/customers')
    • Your specific function will override the non-specific one for the method that matches and use the function for all others
  • So how does the all handler work?
    • Routex will call the all handler after nothing matches and you also can not define more than one handler for the wildcard route
  • You can have multiple before and after functions but only one handler routes use strict equality checking when assigning a function handler and multiplexing when looking for matches

Similarities

CAPITALS are used for router handlers to match HTTP convention, and also due Golang's private/public semantics. ResponseWriter and HttpRequest were also so named to match Golan interfaces and provide an ubiquitous experience across programming languages/environments.