1.0.0 • Published 3 years ago

koa-template v1.0.0

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

koa-template

install package:

yarn add @mpecarina/koa-template

import and use:

import { initApps, logger, bodyParser, json, koaRouter } from "@mpecarina/koa-template"
import path from "path"

process.env.STATIC_DIR = path.join(__dirname, "../static")
process.env.STATIC_PATH = "/"

const routes = path.join(__dirname, "../routes.yaml")
const controllers = path.join(__dirname, "./controllers")

const [app, metricsApp] = initApps([
  logger(),
  bodyParser(),
  json({ pretty: false, param: "pretty", spaces: 2 }),
  koaRouter(routes, controllers),
])

app.listen(process.env.APP_PORT_0 || 3000)
metricsApp.listen(process.env.APP_PORT_1 || 3001)

serve static files

static files are served at "/" from the directory "dist/${pkg.name}" where pkg.name is the name value in package.json. in this example we override to the root of our repo in the static folder

create routes.json

a health check endpoint is enabled for static servers by default at /ping when no routes.json file is present but can be recreated with additional routes by creating a routes.json file in the root of your package

[
  {
    "name": "health-check",
    "controller": "health-check",
    "version": "v1",
    "proxy": {
      "enabled": false,
      "redirect": false,
      "url": "",
      "headers": []
    },
    "description": "",
    "method": ["get"],
    "route": "/ping",
    "handler": "ping",
    "auth": {
      "ldap": false,
      "sso": false
    }
  },
  {
    "name": "test",
    "controller": "health-check",
    "version": "v1",
    "proxy": {
      "enabled": false,
      "redirect": false,
      "url": "",
      "headers": []
    },
    "description": "",
    "method": ["get"],
    "route": "/test",
    "handler": "test",
    "auth": {
      "ldap": false,
      "sso": false
    }
  }
]

create a function matching the controller and handler values in routes.json. for this example bother handlers are in the controllers file controllers/health-check.ts

/* eslint-disable no-unused-vars */
import { BaseContext } from "koa"

export const ping = async (ctx: BaseContext) => {
  ctx.body = { msg: "pong", status: "success" }
}

export const test = async (ctx: BaseContext) => {
  ctx.body = { msg: "test response", status: "success" }
}

curl http://localhost:3000/ping

{"msg":"pong","status":"success"}

curl http://localhost:3000/ping?pretty

{
  "msg": "pong",
  "status": "success"
}

proxy downstream requests

enable the proxy section for a route in routes.json and enter the url destination. kibana is an example transparent redirection while the postman example endpoint is set to redirect: false

[
  {
    "name": "kibana",
    "controller": "",
    "version": "v1",
    "proxy": {
      "enabled": true,
      "redirect": true,
      "url": "http://kibana:5601",
      "headers": []
    },
    "description": "",
    "method": ["get"],
    "route": "/kibana",
    "handler": "",
    "auth": {
      "ldap": false,
      "sso": false
    }
  },
  {
    "name": "postman",
    "controller": "",
    "version": "v1",
    "proxy": {
      "enabled": true,
      "redirect": false,
      "url": "https://postman-echo.com/get?foo1=bar1&foo2=bar2",
      "headers": []
    },
    "description": "",
    "method": ["get"],
    "route": "/postman",
    "handler": "",
    "auth": {
      "ldap": false,
      "sso": false
    }
  }
]

proxy downstream requests

enable the proxy section for a route in routes.json and enter the url destination. kibana is an example transparent redirection while the postman example endpoint is set to redirect: false

access static files

create a static folder and index.html file at static/index.html

<!DOCTYPE html>
<html>
  <head>
    <title>static file server</title>
  </head>
  <body>
    <p>serving files at /</p>
  </body>
</html>

http://localhost:3000/

http://localhost:3000/index.html

koa-template also supports yaml

optionally create routes in yaml file routes.yaml or routes.yml

- name: health-check
  controller: health-check
  version: v1
  proxy:
    enabled: false
    redirect: false
    url: ""
    headers: []
  description: ""
  method:
    - get
  route: "/ping"
  handler: ping
  auth:
    ldap: false
    sso: false

- name: test
  controller: health-check
  version: v1
  proxy:
    enabled: false
    redirect: false
    url: ""
    headers: []
  description: ""
  method:
    - get
  route: "/test"
  handler: test
  auth:
    ldap: false
    sso: false

- name: kibana
  controller: ""
  version: v1
  proxy:
    enabled: true
    redirect: true
    url: http://kibana:5601
    headers: []
  description: ""
  method:
    - get
  route: "/kibana"
  handler: ""
  auth:
    ldap: false
    sso: false

- name: postman
  controller: ""
  version: v1
  proxy:
    enabled: true
    redirect: false
    url: https://postman-echo.com/get?foo1=bar1&foo2=bar2
    headers: []
  description: ""
  method:
    - get
  route: "/postman"
  handler: ""
  auth:
    ldap: false
    sso: false