From 1569c59c69a7138865aa99a5f8901c1da8787647 Mon Sep 17 00:00:00 2001
From: "patrick.tafoya" <patricktafoya@seed-innovations.com>
Date: Thu, 10 Oct 2024 10:51:29 -0600
Subject: [PATCH] Initial templates.

---
 .gitignore                         | 26 ++++++++
 README.md                          | 97 +++---------------------------
 config/healthcheck.js              | 31 ++++++++++
 config/healthcheck.sh              | 22 +++++++
 config/ingress.conf                | 45 ++++++++++++++
 config/postgres-entrypoint.sh      | 19 ++++++
 docker/Dockerfile.ui.dev           |  7 +++
 docker/docker-compose-cosmtek.yml  | 33 ++++++++++
 docker/docker-compose-postgres.yml | 28 +++++++++
 docker/docker-compose-prefix.yml   | 33 ++++++++++
 docker/docker-compose-sufix.yml    |  7 +++
 docker/docker-compose-vue.yml      | 25 ++++++++
 root/.gitignore                    | 34 +++++++++++
 root/README.md                     | 27 +++++++++
 root/readme-golang.md              | 22 +++++++
 15 files changed, 367 insertions(+), 89 deletions(-)
 create mode 100644 .gitignore
 create mode 100755 config/healthcheck.js
 create mode 100755 config/healthcheck.sh
 create mode 100644 config/ingress.conf
 create mode 100755 config/postgres-entrypoint.sh
 create mode 100644 docker/Dockerfile.ui.dev
 create mode 100644 docker/docker-compose-cosmtek.yml
 create mode 100644 docker/docker-compose-postgres.yml
 create mode 100644 docker/docker-compose-prefix.yml
 create mode 100644 docker/docker-compose-sufix.yml
 create mode 100644 docker/docker-compose-vue.yml
 create mode 100644 root/.gitignore
 create mode 100644 root/README.md
 create mode 100644 root/readme-golang.md

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..9b63429
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,26 @@
+.DS_Store
+node_modules
+/dist
+
+# local env files
+.env.local
+.env.*.local
+
+# Log files
+npm-debug.log*
+yarn-debug.log*
+yarn-error.log*
+pnpm-debug.log*
+
+# Editor directories and files
+.idea
+.vscode
+*.suo
+*.ntvs*
+*.njsproj
+*.sln
+*.sw?
+*.iml
+
+# Other
+/tmp/
\ No newline at end of file
diff --git a/README.md b/README.md
index 5ce1114..0de4c0e 100644
--- a/README.md
+++ b/README.md
@@ -1,93 +1,12 @@
 # fiesta-wagon-templates
 
+This project provides templates used by the fiesta-wagon-bootstrap project to build a fiesta wagon development environment.
 
+# template placeholder variables to be replaced
 
-## Getting started
-
-To make it easy for you to get started with GitLab, here's a list of recommended next steps.
-
-Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
-
-## Add your files
-
-- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
-- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
-
-```
-cd existing_repo
-git remote add origin https://repo1.dso.mil/platform-one/party-bus/fiesta-wagon/fiesta-wagon-templates.git
-git branch -M main
-git push -uf origin main
-```
-
-## Integrate with your tools
-
-- [ ] [Set up project integrations](https://repo1.dso.mil/platform-one/party-bus/fiesta-wagon/fiesta-wagon-templates/-/settings/integrations)
-
-## Collaborate with your team
-
-- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
-- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
-- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
-- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
-- [ ] [Set auto-merge](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
-
-## Test and Deploy
-
-Use the built-in continuous integration in GitLab.
-
-- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
-- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
-- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
-- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
-- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
-
-***
-
-# Editing this README
-
-When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template.
-
-## Suggestions for a good README
-
-Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
-
-## Name
-Choose a self-explaining name for your project.
-
-## Description
-Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
-
-## Badges
-On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
-
-## Visuals
-Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
-
-## Installation
-Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
-
-## Usage
-Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
-
-## Support
-Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
-
-## Roadmap
-If you have ideas for releases in the future, it is a good idea to list them in the README.
-
-## Contributing
-State if you are open to contributions and what your requirements are for accepting them.
-
-For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
-
-You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
-
-## Authors and acknowledgment
-Show your appreciation to those who have contributed to the project.
-
-## License
-For open source projects, say how it is licensed.
-
-## Project status
-If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
+- `<<projectName>>`: the name of the project. Should be all lower case and contain no whitespace.
+- `<<dbNameKey>>`: the name of the environment variable used to convey the database name depending on the chosen db. For example, postgres would use `PG_DATABASE`
+- `<<dbUserKey>>`: the name of the environment variable used to convey the database username depending on the chosen db. For example, postgres would use `PG_USER`
+- `<<dbPassword>>`: the name of the environment variable used to convey the database password depending on the chosen db. For example, postgres would use `PG_PASSWORD`
+- `<<dbHost>>`: the name of the environment variable used to convey the database host depending on the chosen db. For example, postgres would use `PGHOST`
+- `<<ignoreEnv>>`: static value of `.env`. We want git to ignore .env files, but not in the project where the templates are maintained.
diff --git a/config/healthcheck.js b/config/healthcheck.js
new file mode 100755
index 0000000..0eeefe1
--- /dev/null
+++ b/config/healthcheck.js
@@ -0,0 +1,31 @@
+#!/usr/local/bin/node
+
+// nodejs-slim container healthcheck (where there is no curl, only node)
+//
+// Usage:
+// ./healthcheck.js url [expectedStatusCode:200]
+//
+// Example in docker compose expecting http://localhost:9999 to respond with a 401:
+// ```
+//   healthcheck:
+//     test: ./healthcheck.js http://localhost:9999
+// ```
+
+const DEFAULT_STATUS_CODE = 200;
+
+(async () => {
+  const url = process.argv?.[2];
+  if (!url) {
+    console.error("url parameter not specified")
+    process.exit(1)
+  }
+
+  const expectedStatusCode = process.argv?.[3] || DEFAULT_STATUS_CODE;
+
+  // check expected vs actual status code
+  const response = await fetch(url);
+  if (parseInt(expectedStatusCode) !== response.status) {
+    console.error(`expected status code ${expectedStatusCode} but received actual ${response.status}`)
+    process.exit(1);
+  }
+})();
diff --git a/config/healthcheck.sh b/config/healthcheck.sh
new file mode 100755
index 0000000..e6ca4d0
--- /dev/null
+++ b/config/healthcheck.sh
@@ -0,0 +1,22 @@
+#!/bin/bash
+
+# Helper script for providing a docker compose healthcheck to containers that are healthy with
+# a non-200 status code.
+#
+# Usage:
+#   ./healthcheck.sh url expected_response_code
+#
+# Example in docker compose expecting http://localhost:9999 to respond with a 401:
+# ```
+#   healthcheck:
+#     test: ./healthcheck.sh http://localhost:9999 401
+# ```
+
+url=$1
+expected_status_code=$2
+
+status_code=$(curl --write-out %{http_code} --silent --output /dev/null $url)
+if [[ "$status_code" -eq $expected_status_code ]]; then
+  exit 0
+fi
+exit 1
\ No newline at end of file
diff --git a/config/ingress.conf b/config/ingress.conf
new file mode 100644
index 0000000..ced9011
--- /dev/null
+++ b/config/ingress.conf
@@ -0,0 +1,45 @@
+# setup for fiesta-wagon-ingress
+server {
+  listen 443 ssl;
+  server_name {projectName}.dev.bigbang.mil;
+
+  location = /_auth {
+    internal;
+    proxy_pass              http://users:8080/auth;
+    proxy_pass_request_body off;
+    proxy_set_header        Content-Length "";
+    proxy_set_header        X-Original-URI $request_uri;
+  }
+
+  location / {
+    auth_request        /_auth;
+    auth_request_set    $token $upstream_http_authorization;
+    proxy_set_header    Authorization $token;
+
+    include             snippets/proxy-params.conf;
+    proxy_intercept_errors on;
+    # if the dev build server is down, try to serve the production build
+    error_page          502 = @fallback;
+
+    set $ui_proxy {projectName}-ui:8080;
+    proxy_pass http://$ui_proxy;
+  }
+
+  location @fallback {
+    include             snippets/proxy-params.conf;
+
+    set $ui_proxy {projectName}-ui:8080;
+    proxy_pass http://$ui_proxy;
+  }
+
+  location ~ ^/api(.*) {
+    auth_request        /_auth;
+    auth_request_set    $token $upstream_http_authorization;
+    proxy_set_header    Authorization $token;
+
+    include             snippets/proxy-params.conf;
+
+    set $api_proxy {projectName}-api:8000;
+    proxy_pass http://$api_proxy/api$1$is_args$args;
+  }
+}
diff --git a/config/postgres-entrypoint.sh b/config/postgres-entrypoint.sh
new file mode 100755
index 0000000..17c2f83
--- /dev/null
+++ b/config/postgres-entrypoint.sh
@@ -0,0 +1,19 @@
+#!/bin/bash
+set -e
+
+CERT_DIR=/var/lib/postgresql/ssl
+mkdir -p ${CERT_DIR}
+
+# get the latest bigbang.dev cert
+wget -q -O /tmp/ingress-certs.yaml https://repo1.dso.mil/big-bang/bigbang/-/raw/master/chart/ingress-certs.yaml
+# extract private key
+cat /tmp/ingress-certs.yaml | awk '/-----BEGIN PRIVATE KEY-----/{p=1} p; /-----END PRIVATE KEY-----/{exit}' | awk '{ sub(/^[ \t]+/, ""); print }' > ${CERT_DIR}/server.key
+# extract certs
+cat /tmp/ingress-certs.yaml | awk '/-----BEGIN CERTIFICATE-----/{p=1} p; /-----END CERTIFICATE-----^\s*$/{exit}' | awk '{ sub(/^[ \t]+/, ""); print }' > ${CERT_DIR}/server.crt
+
+# cert permissions for postgres checks
+chown postgres:postgres ${CERT_DIR}/*
+chmod 0600 ${CERT_DIR}/*
+
+# call original entrypoint in the postgres image
+/usr/local/bin/docker-entrypoint.sh "$@"
diff --git a/docker/Dockerfile.ui.dev b/docker/Dockerfile.ui.dev
new file mode 100644
index 0000000..0705f4d
--- /dev/null
+++ b/docker/Dockerfile.ui.dev
@@ -0,0 +1,7 @@
+FROM registry1.dso.mil/ironbank/opensource/nodejs/nodejs20:20.11
+
+# set up dependencies
+COPY --chown=node:node ./<<projectName>>-ui/package.json ./<<projectName>>-ui/package-lock.json /home/node/
+
+# install dependencies
+RUN npm ci
diff --git a/docker/docker-compose-cosmtek.yml b/docker/docker-compose-cosmtek.yml
new file mode 100644
index 0000000..9864272
--- /dev/null
+++ b/docker/docker-compose-cosmtek.yml
@@ -0,0 +1,33 @@
+version: "3.6"
+services:
+
+  <<projectName>>-api:
+    image: cosmtrek/air:v1.49.0
+    container_name: <<projectName>>-api
+    working_dir: /app
+    env_file:
+      - .env
+    environment:
+      - air_wd=/app
+      - <<dbNameKey>>=<<projectName>>
+      - <<dbUserKey>>=databaseUser
+      - <<dbPassword>>=databasePw1!
+      - <<dbHost>>=db
+      - DEBUGGER_ARGS=${DEBUGGER_ARGS}
+    ports:
+      # debugger port
+      - "40000:40000"
+    volumes:
+      - ./<<projectName>>-api:/app
+      - ./config/healthcheck.sh:/opt/healthcheck/healthcheck.sh
+    networks:
+      - <<projectName>>-net
+    depends_on:
+      db:
+        condition: service_healthy
+    healthcheck:
+      # due to the way the api authorizes requests, we expect a 401
+      test: /opt/healthcheck/healthcheck.sh http://localhost:8000/api/docs 401
+      timeout: 30s
+      retries: 10
+      start_period: 30s
diff --git a/docker/docker-compose-postgres.yml b/docker/docker-compose-postgres.yml
new file mode 100644
index 0000000..3eeda48
--- /dev/null
+++ b/docker/docker-compose-postgres.yml
@@ -0,0 +1,28 @@
+version: "3.6"
+services:
+
+  db:
+    image: postgres:11-alpine
+    container_name: <<projectName>>-db
+    entrypoint: /postgres-entrypoint.sh
+    command: >
+      -c ssl=on
+      -c ssl_cert_file=/var/lib/postgresql/ssl/server.crt
+      -c ssl_key_file=/var/lib/postgresql/ssl/server.key
+    environment:
+      - POSTGRES_DB=<<projectName>>
+      - POSTGRES_USER=postgres
+      - POSTGRES_PASSWORD=postgres
+    volumes:
+      - <<projectName>>-psql:/var/lib/postgresql/data/
+      - ./config/postgres-entrypoint.sh:/postgres-entrypoint.sh
+    networks:
+      - <<projectName>>-net
+    ports:
+      - "5432:5432"
+    healthcheck:
+      test: >
+        pg_isready -d $$POSTGRES_DB -U $$POSTGRES_USER
+      interval: 10s
+      timeout: 5s
+      retries: 10
diff --git a/docker/docker-compose-prefix.yml b/docker/docker-compose-prefix.yml
new file mode 100644
index 0000000..994b8fa
--- /dev/null
+++ b/docker/docker-compose-prefix.yml
@@ -0,0 +1,33 @@
+version: "3.6"
+services:
+
+  ingress:
+    image: registry.dso.mil/platform-one/party-bus/fiesta-wagon/fiesta-wagon-ingress:20240328
+    container_name: <<projectName>>-ingress
+    volumes:
+      - ./config/ingress.conf:/etc/nginx/fiesta-wagon-ingress.conf
+    ports:
+      - 443:443
+    networks:
+      - <<projectName>>-net
+    extra_hosts:
+      - "healthcheck.dev.bigbang.mil:127.0.0.1"
+    healthcheck:
+      test: curl --fail https://healthcheck.dev.bigbang.mil
+      timeout: 10s
+      start_period: 30s
+      retries: 10
+
+  users:
+    image: registry.dso.mil/platform-one/party-bus/fiesta-wagon/fiesta-wagon-users:0.2.0
+    container_name: <<projectName>>-users
+    volumes:
+      - ./users-db:/srv/users/api/db
+      - ./config/healthcheck.js:/opt/healthcheck/healthcheck.js
+    networks:
+      - <<projectName>>-net
+    healthcheck:
+      test: /opt/healthcheck/healthcheck.js http://localhost:8080
+      timeout: 10s
+      start_period: 10s
+      retries: 10
\ No newline at end of file
diff --git a/docker/docker-compose-sufix.yml b/docker/docker-compose-sufix.yml
new file mode 100644
index 0000000..d9a6a39
--- /dev/null
+++ b/docker/docker-compose-sufix.yml
@@ -0,0 +1,7 @@
+
+
+volumes:
+  <<projectName>>-psql: {}
+
+networks:
+  <<projectName>>-net: {}
\ No newline at end of file
diff --git a/docker/docker-compose-vue.yml b/docker/docker-compose-vue.yml
new file mode 100644
index 0000000..412d1e2
--- /dev/null
+++ b/docker/docker-compose-vue.yml
@@ -0,0 +1,25 @@
+version: "3.6"
+services:
+
+  <<projectName>>-ui:
+    build:
+      context: .
+      dockerfile: Dockerfile.ui.dev
+    container_name: <<projectName>>-ui
+    user: "${UID}:${GID}"
+    volumes:
+      - ./<<projectName>>-ui:/home/node
+      - /home/node/node_modules
+    command: npx vite --host
+    environment:
+      # this is the url the browser uses, so it needs to be the external url (going through ingress to hit the backend)
+      - VITE_APP_API_BASE_URL=https://<<projectName>>.dev.bigbang.mil/api
+    stdin_open: true
+    tty: true
+    networks:
+      - <<projectName>>-net
+    healthcheck:
+      test: curl -f http://localhost:8080
+      timeout: 10s
+      retries: 5
+      start_period: 20s
\ No newline at end of file
diff --git a/root/.gitignore b/root/.gitignore
new file mode 100644
index 0000000..003206a
--- /dev/null
+++ b/root/.gitignore
@@ -0,0 +1,34 @@
+.DS_Store
+node_modules
+/dist
+
+# fiesta-wagon specific ignores
+<<projectName>>-ui/
+<<projectName>>-api/
+<<projectName>>-docs/
+users-db/
+
+
+# local env files
+<<ignoreEnv>>
+.env.local
+.env.*.local
+
+# Log files
+npm-debug.log*
+yarn-debug.log*
+yarn-error.log*
+pnpm-debug.log*
+
+# Editor directories and files
+.idea
+.vscode
+*.suo
+*.ntvs*
+*.njsproj
+*.sln
+*.sw?
+*.iml
+
+# Other
+/tmp/
\ No newline at end of file
diff --git a/root/README.md b/root/README.md
new file mode 100644
index 0000000..b8ccde0
--- /dev/null
+++ b/root/README.md
@@ -0,0 +1,27 @@
+# <<projectName>>-local-dev
+
+## Quickstart
+
+```sh
+# start all the <<projectName>> components
+docker compose up
+```
+
+Access the <<projectName>> UI at https://<<projectName>>.dev.bigbang.mil
+
+Access the <<projectName>> API Docs at https://<<projectName>>.dev.bigbang.mil/api/docs
+
+### Create a user
+
+`<<projectName>>-local-dev` uses Fiesta Wagon Ingress, meaning that you can configure local user JWTs from https://users.dev.bigbang.mil/
+
+To grant a local user admin permissions, modify the user's JWT to include the following `group-full` attribute:
+
+```json
+{
+  ...,
+  "group-full": [
+    "/Platform One/Products/<<projectName>>/IL2/admin"
+  ]
+}
+```
diff --git a/root/readme-golang.md b/root/readme-golang.md
new file mode 100644
index 0000000..d9c01db
--- /dev/null
+++ b/root/readme-golang.md
@@ -0,0 +1,22 @@
+
+## Development Environment Setup for golang
+
+- Ensure a `.env` file exists at the root of `<<projectName-local-dev>>` and has the required properties. Example:
+
+  ```
+  DEBUGGER_ARGS=--continue
+  # (Add other variables as needed)
+  ```
+
+- Run locally with hot reloading via Air: `docker compose up`
+
+### Debugging
+
+If running <<projectName>> via the steps above, debugging is already enabled via Delve. If the env var `DEBUGGER_ARGS`
+includes `--continue`, the application will start up and then allow you to attach a debugger. However, if you want to
+debug any of the initialization/startup steps (e.g. `main()`, etc.), you will have
+to remove `--continue` from `DEBUGGER_ARGS` and restart the `<<projectName>>-api` container. This will halt startup until there is a
+debugger attached, allowing you to debug initialization steps in the code.
+
+To connect your IDE to the running instance, set up your debugger to connect to a Go "remote" or "server"
+via `port: 40000`.
-- 
GitLab