Skip to content

Commit

Permalink
Add Dev Container
Browse files Browse the repository at this point in the history
  • Loading branch information
myrotvorets-team committed Sep 26, 2023
1 parent 6b1ab25 commit 041ec06
Show file tree
Hide file tree
Showing 14 changed files with 234 additions and 1 deletion.
1 change: 1 addition & 0 deletions .devcontainer/.docker/identigraf-uploader/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.gitkeep
11 changes: 11 additions & 0 deletions .devcontainer/.docker/identigraf-uploader/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM ghcr.io/sjinks/codespaces/nodejs:latest

RUN \
apk add --no-cache nginx && \
sed -i "s/user nginx;/user vscode;/" /etc/nginx/nginx.conf && \
chown -R vscode:vscode /run/nginx /var/log/nginx /var/lib/nginx

COPY rootfs /
WORKDIR /usr/src/service

RUN chown -R vscode:vscode /usr/src/service
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
server {
listen 80;
server_name localhost;

client_body_buffer_size 10m;

proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header Accept-Encoding "";
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host:$server_port;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 300s;

location / {
proxy_set_header traceparent "";
proxy_set_header tracestate "";
proxy_pass http://localhost:3000;
}

location /identigraf-upload/v1/ {
proxy_set_header traceparent "";
proxy_set_header tracestate "";
proxy_pass http://localhost:3000/;
}

location /swagger {
proxy_pass http://swagger:8080;
}
}

server {
listen 9411;
server_name localhost;

location / {
proxy_pass http://zipkin:9411;
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh

set -e
exec 2>&1

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
SERVICE=identigraf-uploader

install -d -o vscode -g vscode -m 0755 "/var/log/${SERVICE}"
exec sudo -u vscode -H /usr/local/bin/start-service.sh > "/var/log/${SERVICE}/start.log" 2>&1
16 changes: 16 additions & 0 deletions .devcontainer/.docker/identigraf-uploader/rootfs/etc/sv/nginx/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh

set -eu

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

exec 2>&1

NGINX_USER="${CONTAINER_USER:-vscode}"

COMMAND=/usr/sbin/nginx
PID_FILE=/run/nginx/nginx.pid

/usr/bin/install -d -o "${NGINX_USER}" -g "${NGINX_USER}" "${PID_FILE%/*}" /var/log/nginx
/usr/bin/install -d -o "${NGINX_USER}" -g "${NGINX_USER}" -m 0750 /var/lib/nginx
exec "${COMMAND}" -c /etc/nginx/nginx.conf -g "pid ${PID_FILE}; daemon off;"
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/sh

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

cd /usr/src/service || exit 1

if [ ! -f .npmrc.local ] || ! grep -q '//npm.pkg.github.com/:_authToken=' .npmrc.local; then
if [ -n "${GITHUB_TOKEN}" ]; then
echo "Using GITHUB_TOKEN to authenticate to npm.pkg.github.com"
echo "//npm.pkg.github.com/:_authToken=${GITHUB_TOKEN}" >> .npmrc.local
elif [ -n "${READ_PACKAGES_TOKEN}" ]; then
echo "Using READ_PACKAGES_TOKEN to authenticate to npm.pkg.github.com"
echo "//npm.pkg.github.com/:_authToken=${READ_PACKAGES_TOKEN}" >> .npmrc.local
else
echo "WARNING: Neither GITHUB_TOKEN nor READ_PACKAGES_TOKEN is available; package download may fail."
fi
else
echo ".npmrc.local already exists and has an authentication token for npm.pkg.github.com"
fi

exec sudo sv start identigraf-uploader
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/sh

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
SERVICE=identigraf-uploader

cd /usr/src/service || exit 1

if [ ! -f src/specs/identigraf-uploader.yaml ] && [ -d .git ]; then
git submodule update --init --recursive
fi

export NPM_CONFIG_AUDIT=0

if [ ! -d node_modules ]; then
npm ci --ignore-scripts --userconfig .npmrc.local && npm rebuild && npm run prepare --if-present
else
npm install --ignore-scripts --userconfig .npmrc.local && npm rebuild && npm run prepare --if-present
fi

if [ ! -d node_modules ]; then
echo "FATAL: Failed to install dependencies"
exit 1
fi

npm run start:dev 2>&1 | tee -a "/var/log/${SERVICE}/${SERVICE}.log"
Empty file.
54 changes: 54 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "IDentigraF Uploader",
"dockerComposeFile": "docker-compose.yml",
"service": "identigraf-uploader",
"workspaceFolder": "/usr/src/service",
"forwardPorts": [80, 9411],
"portsAttributes": {
"80": {
"label": "Application (Frontend)",
"onAutoForward": "notify"
},
"3000": {
"label": "Application (Backend)",
"onAutoForward": "ignore"
},
"9411": {
"label": "Zipkin",
"onAutoForward": "notify"
}
},
"remoteUser": "vscode",
"postCreateCommand": "/usr/local/bin/post-create.sh",
"secrets": {
"READ_PACKAGES_TOKEN": {
"description": "Personal access token to install packages from ghcr.io"
}
},
"customizations": {
"codespaces": {
"repositories": {
"myrotvorets/psb-api-identigraf-uploader": {
"permissions": {
"packages": "read"
}
}
}
},
"vscode": {
"extensions": [
"dlech.chmod",
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
"timonwong.shellcheck",
"ms-azuretools.vscode-docker",
"zhiayang.tabindentspacealign",
"42Crunch.vscode-openapi",
"natqe.reload"
],
"settings": {
"files.autoSave": "onWindowChange"
}
}
}
}
36 changes: 36 additions & 0 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
services:
identigraf-uploader:
build:
context: .docker/identigraf-uploader
dockerfile: Dockerfile
environment:
- NODE_ENV=development
- NO_UPDATE_NOTIFIER=true
- NPM_CONFIG_FUND=0
- SUPPRESS_SUPPORT=1
- HTTPS=0
- PORT=3000
- ENABLE_TRACING=1
- OTEL_EXPORTER_ZIPKIN_ENDPOINT=http://zipkin:9411/api/v2/spans
- IDENTIGRAF_UPLOAD_FOLDER=/var/lib/identigraf-uploader/uploads
- IDENTIGRAF_MAX_FILE_SIZE=5242880
- HAVE_SWAGGER=true
- npm_config_userconfig=/usr/src/service/.npmrc.local
restart: always
volumes:
- "../:/usr/src/service"
- uploads:/var/lib/identigraf-uploader/uploads
working_dir: /usr/src/service

zipkin:
image: openzipkin/zipkin:latest@sha256:5fd55e6a109233b36d419d7fd2449588d17a6e4da7ed7a3fd0d09c86f1c75a15
restart: always

swagger:
image: swaggerapi/swagger-ui:latest@sha256:3b4f51470fed56b1ce5a064f57ce7a0d585f50192731f458e7b376713b286d57
environment:
- SWAGGER_JSON_URL=/specs/identigraf-uploader.yaml
- BASE_URL=/swagger

volumes:
uploads:
18 changes: 17 additions & 1 deletion src/server.mts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import express, { type Express, json } from 'express';
import express, { type Express, json, static as staticMiddleware } from 'express';
import { installOpenApiValidator } from '@myrotvorets/oav-installer';
import { errorMiddleware, notFoundMiddleware } from '@myrotvorets/express-microservice-middlewares';
import { cleanUploadedFilesMiddleware } from '@myrotvorets/clean-up-after-multer';
Expand All @@ -17,6 +17,22 @@ import { uploadErrorHandlerMiddleware } from './middleware/upload.mjs';
export async function configureApp(app: express.Express): Promise<void> {
const env = environment();

/* c8 ignore start */
if (env.NODE_ENV !== 'production') {
app.use(
'/specs/',
staticMiddleware(join(dirname(fileURLToPath(import.meta.url)), 'specs'), {
acceptRanges: false,
index: false,
}),
);

if (process.env.HAVE_SWAGGER === 'true') {
app.get('/', (_req, res) => res.redirect('/swagger/'));
}
}
/* c8 ignore end */

app.use(json());

await installOpenApiValidator(
Expand Down

0 comments on commit 041ec06

Please sign in to comment.