Sign inSign up

fredlackey/complexapi

By fredlackey

Updated over 2 years ago

Simple Node API to help with prototyping and scaffolding service layers as well as training.

Image
0

569

fredlackey/complexapi repository overview

Complex API (complexapi)

Simple Node API to help with prototyping and scaffolding service layers as well as training.

Background

From time to time I need to carve out the infrastructure prior to beginning a project. This means setting up CICD pipelines, accounts within AWS, Azure, or GCP, and then creating the scaffolding for the service layer. This project acts as a placeholder for the various microservices within each zone. By deploying complexapi I can quickly create a "hello world" structure, to ensure the back end is in place, before starting work. As I develop each service, I can replace the complexapi placeholder with the actual service.

Default Routes

The default routes available are:

RouteDetails
/Simple response with local parameters.
/statusExpanded response including environment variables and a ping (/) of upstream URL(s).
/testSame as /status but invokes a recursive test upstream URL(s) on their /test route .

Routes When NODE_BASE Is Set

The default routes available are:

RouteDetails
/Simple response with local parameters.
/%NODE_BASE%Simple response with local parameters.
/%NODE_BASE%/statusExpanded response including environment variables and a ping (/) of upstream URL(s).
/%NODE_BASE%/testSame as /status but invokes a recursive test upstream URL(s) on their /test route .

Upstream Prefix URLs

You are able to add as many upstream URLs as you like by setting the UPSTREAM_* environment variables prefix. Some examples are:

VariableValueDescription
UPSTREAM_Alocalhost:3001Call to the default route.
UPSTREAM_COOLROUTEmycontainer:8080/publicCall to a container using a base route.

Variables

The following environment variables are available:

VariableDefaultDescription
NODE_ALIASOptional alias used for logging clarity.
NODE_BASE/Optional base route for the API.
NODE_PORT3000Port the API listens to.

Examples

The source code from Git contains three example scenarios.

Simple Example examples/a-single

The first is a very simple example that shows how to stand up a simple version of the service.

Execution
git clone https://github.com/FredLackey/complexapi.git
cd ./complexapi/examples/a-single
docker compose up
Docker Compose File
services:

  SINGLE_SERVICE:
    image: fredlackey/complexapi:0.0.3
    container_name: complexapi-single
    ports:
      - "3000:3000"
    networks:
      - complexapi-network

networks:
  complexapi-network:
    drive
Output
{
  "name" : "complex-api-example",
  "alias": "(not set)",
  "base" : "(not set)",
  "desc" : "Complex API Example",
  "env"  : "production",
  "ver"  : "0.0.3",
  "date" : "2024-04-11T07:16:27.413Z",
  "vars" : {
    "HOME"        : "/home/node",
    "HOSTNAME"    : "18acc4934cff",
    "NODE_ENV"    : "production",
    "NODE_VERSION": "18.19.1",
    "PATH"        : "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
    "PWD"         : "/home/node/app",
    "YARN_VERSION": "1.22.19"
  },
  "tests": null
}
Parent-Child Example examples/b-parentchild

The second example combines two instances: one parent and one child.

Executing the example
git clone https://github.com/FredLackey/complexapi.git
cd ./complexapi/examples/b-parentchild
docker compose up
Docker Compose File
services:

  COMPLEXAPI_PRIVATE:
    image: fredlackey/complexapi:0.0.3
    container_name: complexapi-private
    environment:
      - NODE_PORT=3000
      - NODE_ALIAS=PRIVATE
    networks:
      - complexapi-network

  COMPLEXAPI_PUBLIC:
    image: fredlackey/complexapi:0.0.3
    container_name: complexapi-public
    environment:
      - NODE_ALIAS=PUBLIC
      - UPSTREAM_PRIVATE=complexapi-private:3000
    networks:
      - complexapi-network
    ports:
      - "3000:3000"
    depends_on:
      - COMPLEXAPI_PRIVATE

networks:
  complexapi-network:
    driver: bridge
Output

Output from a GET call to http://localhost:3000/test. Note the calls to the the upstream service using its alias as the output payload:

{
  "name" : "complex-api-example",
  "alias": "PUBLIC",
  "base" : "(not set)",
  "desc" : "Complex API Example",
  "env"  : "production",
  "ver"  : "0.0.3",
  "date" : "2024-04-11T07:20:02.629Z",
  "vars" : {
    "HOME"            : "/home/node",
    "HOSTNAME"        : "d2525c513cbe",
    "NODE_ALIAS"      : "PUBLIC",
    "NODE_ENV"        : "production",
    "NODE_VERSION"    : "18.19.1",
    "PATH"            : "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
    "PWD"             : "/home/node/app",
    "UPSTREAM_PRIVATE": "complexapi-private:3000",
    "YARN_VERSION"    : "1.22.19"
  },
  "tests": {
    "PRIVATE": {
      "name" : "complex-api-example",
      "alias": "PRIVATE",
      "base" : "(not set)",
      "desc" : "Complex API Example",
      "env"  : "production",
      "ver"  : "0.0.3",
      "date" : "2024-04-11T07:20:02.670Z"
    }
  }
}

Additional Samples

Other samples exist within the examples directory. Please feel free to explore them. They are too numerous to list here.

Contact Iinfo

Please feel free to reach out if I can help in any way.

Fred Lackey
[email protected]
https://fredlackey.com

Tag summary

Content type

Image

Digest

sha256:b718bd4f9

Size

85.2 MB

Last updated

over 2 years ago

docker pull fredlackey/complexapi