Skip to content

Commit

Permalink
feat: bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksii-honchar committed Jul 14, 2024
1 parent 73ae101 commit 9241baf
Show file tree
Hide file tree
Showing 35 changed files with 5,615 additions and 1 deletion.
10 changes: 10 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
node_modules
.github/
.git/
.gitignore
Dockerfile
Makefile
README*
*.code-workspace
.gitlab-ci.yml
ops/
1 change: 1 addition & 0 deletions .env.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NODE_ENV=development
27 changes: 27 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.git
.editorconfig
.idea
.npmrc
*.crt
*.key
*.pem
npm-debug.log
.env
backup/*
coverage
dist/*
devops/version/
!dist/.gitkeep
logs/*
node_modules/
devops/version/
tmp

cdktf.out
cdktf.log
*terraform.*.tfstate*
.gen
.terraform
tsconfig.tsbuildinfo
!jest.config.js
!setup.js
2 changes: 2 additions & 0 deletions .ncurc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v22.3
Empty file added .prettierignore
Empty file.
13 changes: 13 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
arrowParens: 'avoid',
bracketSpacing: true,
plugins: [
'prettier-plugin-organize-imports',
],
printWidth: 90,
singleQuote: true,
semi: true,
tabWidth: 2,
trailingComma: 'all',
useTabs: false,
};
18 changes: 18 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"editor.codeActionsOnSave": {
"source.sortImports": "explicit",
"source.fixAll": "explicit"
},
"eslint.enable": true,
"eslint.format.enable": true,
"eslint.options": {
"overrideConfigFile": "./eslint.config.js"
},
"eslint.run": "onType",
"eslint.useFlatConfig": true,
"eslint.validate": ["typescript"],
"prettier.configPath": "./.prettierrc.js",
"prettier.requireConfig": true,
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
30 changes: 30 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#--- BASE ---
FROM tuiteraz/jaba-node:22.3-0 AS base
# default workdir = /usr/src/app
ENV TZ=Europe/Madrid
COPY ./scripts/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
CMD ["node", "--enable-source-maps", "dist/src/index.js"]

#--- BUILD ---
FROM tuiteraz/jaba-build:22.3-0 AS build
WORKDIR /tmp

COPY . .
RUN corepack enable pnpm && corepack install -g pnpm
RUN pnpm install
RUN pnpm build

#--- FINAL---
FROM base AS final
ARG IMAGE_VERSION
ARG IMAGE_NAME
ENV IMAGE_VERSION=$IMAGE_VERSION
ENV IMAGE_NAME=$IMAGE_NAME

COPY --from=build /tmp/dist ./dist
RUN corepack enable pnpm && corepack install -g pnpm
COPY package.json .
COPY pnpm-lock.yaml .
RUN pnpm i --prod
44 changes: 44 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
SHELL=/bin/bash
RED=\033[0;31m
GREEN=\033[0;32m
BG_GREY=\033[48;5;237m
YELLOW=\033[38;5;202m
BOLD_ON=\033[1m
BOLD_OFF=\033[21m
NC=\033[0m # No Color

ifneq (,$(wildcard ./.env))
include ./.env
endif

IMAGE_VERSION := $(shell jq -r '.version' package.json)
IMAGE_NAME := $(shell jq -r '.name' package.json)

.PHONY: help

help:
@echo Automation commands:
@echo
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(firstword $(MAKEFILE_LIST)) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

check-node-env:
ifndef NODE_ENV
@printf "${YELLOW}NODE_ENV not provided. Using ${BOLD_ON}'NODE_ENV=development'${BOLD_OFF} as default${NC}\n"
export NODE_ENV = development
endif

docker-build: check-node-env ## build docker image
docker build --load -f ./Dockerfile --build-arg IMAGE_VERSION=$(IMAGE_VERSION) --build-arg IMAGE_NAME=$(IMAGE_NAME) -t $(IMAGE_NAME):$(IMAGE_VERSION) .

docker-run: check-node-env ## build docker image
docker run --rm -p 9000:9000 $(IMAGE_NAME):$(IMAGE_VERSION)

logs-restart: ## restart logs stack
@docker compose -f ./ops/grafana-logs/docker-compose.logs.yaml down
@docker compose -f ./ops/grafana-logs/docker-compose.logs.yaml up -d

logs-stop: ## stop logs stack
@docker compose -f ./ops/grafana-logs/docker-compose.logs.yaml down

logs-logs: ## display logs from logs stack
@docker compose -f ./ops/grafana-logs/docker-compose.logs.yaml logs -f
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# fastify-tmpl
# fastify-tmpl

Fastify API service barebone template
5 changes: 5 additions & 0 deletions config/custom-environment-variables.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"runtime": {
"environment": "NODE_ENV"
}
}
11 changes: 11 additions & 0 deletions config/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"deployment": {
"containerName": "fastify-tmpl",
"imageName": "tuiteraz/fastify-tmpl"
},
"runtime": {
"environment": "development",
"logLevel": "debug",
"port": 9000
}
}
5 changes: 5 additions & 0 deletions config/development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"runtime": {
"logLevel": "info"
}
}
5 changes: 5 additions & 0 deletions config/production.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"runtime": {
"logLevel": "error"
}
}
49 changes: 49 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const globals = require('globals');
const eslint = require('@eslint/js');
const tseslint = require('typescript-eslint');
const eslintPrettier = require('eslint-plugin-prettier');

module.exports = tseslint.config(
{
ignores: [
'**/dist/**',
'**/node_modules/**',
'**/coverage/**',
'.prettierrc.js',
'eslint.config.js',
],
},
eslint.configs.recommended,
...tseslint.configs.recommended,
...tseslint.configs.stylistic,
require('eslint-config-prettier'),
{
plugins: {
'typescript-eslint':
tseslint.plugin,
prettier: eslintPrettier,
},
languageOptions: {
ecmaVersion: 'latest',
parser: tseslint.parser,
parserOptions: {
project: true,
},
sourceType: 'module',
globals: {
...globals.node,
...globals.jest,
},
},
rules: {
'prettier/prettier': 'error',
},
},
{
files: ['**/*.md'],
plugins: {
markdown: require('eslint-plugin-markdown'),
},
processor: 'markdown/markdown',
},
);
12 changes: 12 additions & 0 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"ignore": [
".git",
"node_modules",
"test"
],
"verbose": true,
"execMap": {
"ts": "node -r ts-node/register -r tsconfig-paths/register"
},
"ext": "js json ts"
}
17 changes: 17 additions & 0 deletions ops/grafana-logs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Package logs exploring using local Grafana+Loki+Promtail stack

## Motivation

When developing locally, API logs are either too lengthy when using pino-pretty or too hard to visually parse when in JSON format. To make multiple log steps easier to read, one can utilise local Grafana+Loki+Promtail stack. It also allows one to collapse and expand log lines.

## How to use

1. Start environment using `pnpm dev:json-logs`. This command will pipe all the logs to the `./logs/fastify.log` file
2. Navigate to Grafana `http://locahost:3000`
3. Check dashboard "Loki: Logs/App"

Notes:

- Use default `admin:admin` to log into Grafana for customization.

<image src="./assets/log-in.jpg" width=400/>
Binary file added ops/grafana-logs/assets/log-in.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 44 additions & 0 deletions ops/grafana-logs/docker-compose.logs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
version: '3'

services:
loki:
image: grafana/loki:latest
container_name: loki
ports:
- '3100:3100'
volumes:
- loki_storage:/loki
networks:
- monitoring

promtail:
image: grafana/promtail:latest
container_name: promtail
volumes:
- ./promtail-config.yaml:/etc/promtail/config.yaml
- ../../logs:/var/log/fastify
command: -config.file=/etc/promtail/config.yaml
networks:
- monitoring

grafana:
image: grafana/grafana:latest
container_name: grafana
ports:
- '3000:3000'
volumes:
- grafana-storage:/var/lib/grafana
- ./provisioning/:/etc/grafana/provisioning
environment:
- GF_USERS_DEFAULT_THEME=light
- GF_AUTH_ANONYMOUS_ENABLED=true
networks:
- monitoring

volumes:
grafana-storage:
loki_storage:

networks:
monitoring:
driver: bridge
27 changes: 27 additions & 0 deletions ops/grafana-logs/promtail-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
server:
http_listen_port: 9080
grpc_listen_port: 0

positions:
filename: /tmp/positions.yaml

clients:
- url: http://loki:3100/loki/api/v1/push

scrape_configs:
- job_name: system
static_configs:
- targets:
- localhost
labels:
job: fastify-logs
__path__: /var/log/fastify/*.log
pipeline_stages:
- json:
expressions:
name: name
severity: severity
- labels:
name:
severity:
message:
10 changes: 10 additions & 0 deletions ops/grafana-logs/provisioning/dashboards/dashboard.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: 1
providers:
- name: 'default'
orgId: 1
folder: ''
type: file
disableDeletion: false
editable: true
options:
path: /etc/grafana/provisioning/dashboards
Loading

0 comments on commit 9241baf

Please sign in to comment.