gaston

command module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 4, 2020 License: GPL-3.0 Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

gaston

Tests Status Docker Image

Gaston is the json-rpc 2.0 API gateway.

Usage

First of all, you need services with json-rpc 2.0 API which will be behind gaston. For example

# docker-compose.yaml
version: "3"
services:
  s1:
    image: fullpipe/jmock:latest
    volumes:
      - ./examples/mocks/s1:/mocks
  s2:
    image: fullpipe/jmock:latest
    volumes:
      - ./examples/mocks/s2:/mocks

Next we add gaston as gateway fo this services

# docker-compose.yaml
version: "3"
services:
  ...
  gaston:
    image: fullpipe/gaston:latest
    volumes:
      - ./examples/methods:/methods
    environment:
      GASTON_JWT_HMACSECRET: qwertyuiopasdfghjklzxcvbnm123456
    ports:
      - 8080:8080
methods.json example
[
  {
    "host": "http://user:8080/rpc", // hidden service url
    "name": "user.get", // method name for gaston clients
    "remoteName": "getUser", // method name on hidden service
    "roles": ["ROLE_USER"], // client has to have at least on of the roles, to get access to hidden service
    "paramConverters": [ // paramConverters do some work on client params
      { "type": "rename", "from": "userId", "to": "id" }
    ],
    "resultConverters": [ // resultConverters do some work on hidden service results
      { "type": "rename", "from": "firstname", "to": "name" }
      { "type": "overwrite", "name": "age", "newValue": 18 }
    ]
  },
  {
    "host": "http://email:8080/rpc",
    "name": "foo",
    "remoteName": "bar",
    "paramConverters": [
      { "type": "rename", "from": "email_input", "to": "email" }
    ]
  }
]
user.get flow
Client to Gaston
{
	"jsonrpc": "2.0",
	"method": "user.get",
	"id": 1,
	"params": {
		"userId": 123
	}
}
Gaston to Service
{
	"jsonrpc": "2.0",
	"method": "getUser", // method name converted to remoteName
	"id": 1,
	"params": {
		"id": 123 // userId was converted to id by paramConverter
	}
}
Service to Gaston
{
  "jsonrpc": "2.0",
  "result": {
    "id": 123,
    "firstname": "John",
    "lastname": "Doe",
    "age": 99
  },
  "id": 1
}
Gaston to Client
{
  "jsonrpc": "2.0",
  "result": {
    "id": 123,
    "name": "John", // firstname was converted to name by resultConverter
    "lastname": "Doe",
    "age": 18 // age was overwritten to 18 by resultConverter
  },
  "id": 1
}

Converters

rename

Renames param key

{ "type": "rename", "from": "oldName", "to": "newName" }
overwrite

Overwrite param/result by name. If param/result with name not exists, does nothing.

{ "type": "overwrite", "name": "age", "newValue": 18 }
snake_case

Transform param name to its snake_cased version

{ "type": "snakeCase", "name": "fooBar" } // will convert to `foo_bar`
// or
{ "type": "snake_case", "name": "fooBar" }
remove

Removes param by name if exists

{ "type": "remove", "name": "fooBar" }
// or
{ "type": "delete", "name": "fooBar" }
castNumber

Cast param value to number

{ "type": "castNumber", "name": "userId" } // { "userId: "123" } -> { "userId: 123 }
{ "type": "castNumber", "name": "price" } // { "price: "1.333" } -> { "userId: 1.333 }
true -> 1
false -> 0
null -> 0
empty string -> 0
"1" -> 1
"1.1" -> 1.1
other -> null

Env var

Here available env vars, with their default values

GASTON_SERVER_ROUTE: / # route, http://gaston/, http://gaston/v1, http://gaston/v2
GASTON_SERVER_PORT: 8080
GASTON_SERVER_METHODSPATH: "/methods/*.json" # glob path to lookup methods
GASTON_SERVER_REMOTETIMEOUT: 5 # timeout for remote requests
GASTON_JWT_HEADER: Authorization # header name to read JWT token from
GASTON_JWT_SCHEME: Bearer # Authorization scheme
GASTON_JWT_HMACSECRET: # HmacSecret to validate JWT token
GASTON_JWT_ROLESCLAIM: roles # claim key to get user roles
GASTON_JWT_USERCLAIM: sub # claim key to get user id
GASTON_JWT_REMOTEUSERHEADER: X-Verified-User # header name to pass user id to "hidden" services
GASTON_JWT_REMOTEROLESHEADER: X-Verified-Roles # header name to pass user roles to "hidden" services

todo

  • More converters
    • rename, rename param key
    • overwrite, overwrite param value if param exists
    • snakeCase, convert param name to snake_case. userId -> user_id
    • cammelCase, convert param name to cammelCase. user_id -> userId
    • remove, remove param by name
    • default, setup param if not exists
    • set, overwrite + default
    • castNumber, cast value to number
    • castString, cast value to string
    • castBoolean, cast value to boolean
    • nullFix, convert string "null" to real null
  • Examples

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL