1.0.44 • Published 3 years ago

sonic-api-gateway v1.0.44

Weekly downloads
4
License
MIT
Repository
github
Last release
3 years ago

Sonic - Nodejs API GATEWAY

alt text

Sonic is a high-performance 100% open source, config based free API Gateway for Nodejs. All source code is available under the MIT License on GitHub.

Its core functionality is to create an API that acts as an aggregator of many microservices into single endpoints.

Features

  • A single API that exposes many.
  • Merge the content and modify of several APIs into one.
  • Device detection (mobile, desktop).
  • HTTP Caching(Redis, memory).
  • Unlimited endpoints and backends.
  • Secure the transport.
  • Error handling.
  • URL patterns and variables
  • Request body validation
  • Use middlewares(Response, Request, Error, and your own).
  • Simple JSON configuration.

Installing:

This is a Node.js module available through the npm registry.

Using npm:

$ npm install sonic-api-gateway --save

Using yarn:

$ yarn add sonic-api-gateway

Example:

Now that we have installed the Sonic API gateway module.

Let's create a Sonic project file.

app.js

const Sonic = require('sonic-api-gateway').Sonic

const Config = {
  port : 3014, // Server will run on port 3014 
  routes: [
    {
        endpoint: '/posts/:id', // http://localhost:3014/posts/:id
        method: 'get',
        backend: [
            {
                target: 'https://jsonplaceholder.typicode.com/posts/{id}',
                method: 'get',
                response_key : "posts",
            },
            {
                target: 'https://jsonplaceholder.typicode.com/posts',
                method: 'get',
                response_key : "all_posts",
            },
            // ...
        ]
    },
    // ...
  ]
}

new Sonic(Config);

Start Sonic server:

 node app.js 

Make a GET request (as we declare in config) with CURL:

curl -G http://localhost:3014/posts/1

Configuration options:

{
    version: String, // Optional
    logs: Boolean, // Optional
    port : Number, // Optional, Default 3000
    debug: Boolean, // Optional, Default true, for logging
    middlewares: Array, //Optional global middlewares in the format: (req, res, next) => next(), Default []
    cache: [ // Optional
        {
            driver: String, // Required, redis | memory
            host: String, // Optional
            port: Number // Optional
        }
        // ...
    ],
    routes: [
        {
            endpoint : String, // Required, ex: "/posts"
            method : String, // Required, GET, POST, DELETE, PUT
            cache: { // Optional,
                driver: String, // Required, redis | memory,
                ttl: Number, // Optional, 1 = 1 second
            },
            params : { // Optional
                [key: String] : String
            },
            onResponse?(req: any, res: any, next: any, data: any, route: any): Function; // Optional
            onRequest?(req: any, res: any, next: any): Function; // Optional
            backend: [
                {
                    target: String, // Required, ex: "https://jsonplaceholder.typicode.com/posts"
                    method: String, // Required, GET, POST, DELETE, PUT
                    response_key: String, // Optional
                    response_status : Number, // Optional
                    onResponse?(req: any, res: any, next: any, data: any, route: any): Function, // Optional
                    auth : Boolean, // Optional
                    onError?(req, res, next, proxyRoute, error) : Function // Optional
                    params : { // Optional
                        [key: String] : String
                    },
                    body: {
                        [key: string]: 'string' | 'files' | 'any' | 'number' | 'boolean' 
                    }, // Required if request method is POST, PUT. ex: body : {name : 'string'}
                    body_method: String, // Default : rawdata. Possible: formdata, urlencoded, raw
                    headers: Object // Optional, set request header
                    childRoutes: Backend[] // Experimental
                }
            ]
        }
        // ...
    ]
}

API


Options:

  1. PORT - Sonic server port
  2. VERSION - Informing client about Sonic version
  3. DEBUG - Sonic debug mode
  4. LOGS - Log incoming requests info
  5. MIDDLEWARES - Middlewares
  6. CACHE - Caching responses
  7. ROUTES - API
  8. ROUTES.ENDPOINT - API endpoint
  9. ROUTES.METHOD API method
  10. ROUTES.onResponse - Handle response
  11. ROUTES.CACHE
  12. ROUTES.BACKEND
  13. ROUTES.BACKEND.TARGET
  14. ROUTES.BACKEND.METHOD
  15. ROUTES.BACKEND.RESPONSE_KEY
  16. ROUTES.BACKEND.RESPONSE_STATUS
  17. ROUTES.BACKEND.ONRESPONSE
  18. ROUTES.BACKEND.AUTH
  19. ROUTES.BACKEND.BODY
  20. ROUTES.BACKEND.BODY_METHOD
  21. ROUTES.BACKEND.HEADERS
  22. ROUTES.BACKEND.CHILD_ROUTES

port - Creates Sonic server

Start a server listening for connections. It will create simple http server with express.

Type: Number Default: 3000 Required: false

Example:

new Sonic({
    port : 3014
    // ...
})

curl -G http://localhost:3014

:warning: If address allready in use server will not start

version - Informing client for Sonic version with header

Seting response header for every response wich sending from server to client.

Type: String Default: 1.0 Required: false

Example:

new Sonic({
    version: 2.0,
    // ...
})

debug - Sonic debug mode

Debug styled server actions in your node.js console. It will log incoming requests, server status, caching servers status, invalid config error

Type: Boolean Default: true Required: false

Example:

new Sonic({
    debug: true
    // ...
})

alt text

logs - Logs incoming request into file

Save request date and client ip into the file in root directory _log/{date)

Type: Boolean Default: false Required: false

Example:

new Sonic({
    logs : true,
    // ...
})

middlewares - Express Middlewares

Use Express middlewares.

Type: Array:Function(req,res,next) Default: none Required: false

Example:

new Sonic({
    middlewares: [
        (req: Request, res: Response, next: NextFunction) => {
            console.log(req.method)
            next() // Don't miss 
        },
        morgan(),
        // ...
    ]
    // ...
})

cache - Caching backend response

With caching option you can cache every response to a redis or disk memory. This caching technique applies to traffic between Sonic and your microservices endpoints.

Type: Array:Object (port?: Number, host?: String, driver: 'redis' || 'memory') Default: none Required: false Possible cache drivers: redis | memory

Example:

new Sonic({
    cache : [
        {
            driver: 'redis',
            host: '1231.1.2.3.4',
            port: 12701
        },
        {
            driver: 'memory',
        }
    ],
    routes : [
        {
            endpoint: "/v1/test",
            method: "get",
            cache: {
                driver: "memory",
            },
            backend: [
                // ...
            ]
        },
        {
            endpoint: "/v1/test/pets",
            method: "get",
            cache: {
                driver: "redis",
            },
            backend: [
                // ...
            ]
        }
    ]
    // ...
})

routes - API routes

An array of endpoint objects offered by the gateway, all the associated backends and configurations.

Type: Array:Object Default: none Required: true

Example:

new Sonic({
    routes : [
        {
            endpoint: "/v1/test",
            method: "get", 
            backend: [
                // ...
            ]
        },
        // ...
    ]
    // ...
})

routes.endpoint - Api endpoint name

The resource URL you want to expose

Type: String Default: none Required: true Note: Endpoints starts with "/"

Example

new Sonic({
    // ... 
    routes : {
        endpoint : "/v1/test-endpoint"
        // ... 
    }
    // ... 
})
curl http://localhost:3014/v1/test-endpoint

routes.method - Api request method

Request method, GET, POST, PUT or DELETE

Type: String Default: get Required: true Note: Possible : GET, POST, PUT, DELETE

Example

new Sonic({
    // ...
    routes : {
        endpoint : "/v1/test-endpoint",
        method : "get"
        // ...
    }
    // ...
})
curl http://localhost:1001/v1/test-endpoint

Documentation in progress.