holy-cow-nodejs v1.1.7
Holycow node.js example project
Note: Cluster bindings are missing for the holy cow group in gitlab so gitlab ci will fail to deploy, until this is resolved. test
great contribution
Status of project:
- Build
- Use Kaniko
- One process per container
- Small images
- CI/CD
- Gitlab CI
- Healthchecks
- Format
- Readiness /ready
- Liveness /healthy
- Format
- Logging
- Format
- JSON
- Fields
- Format
- Monitoring
- Expose a
/metrics
endpoint - Use existing libraries to expose your metrics
- Use your application port for your metrics endpoint if possible
- Follow the default prometheus naming conventions for your metrics
- Expose a
- API Docs
- Use OpenAPI
- Provide endpoint for swagger.json
- Provide swagger ui at /api-docs
- Requests & Limits
- Scaling
- Security
- Use Helmet
- Startup and Shutdown
- Statelessness
- Versioning
Steps used to create this project
Create Repo in Gitlab & Clone
git clone git@gitlab.open-xchange.com:holycow/node.js.git
Initialize package.json
yarn init
Add .gitignore and .editorconfig.
Add ESlint for Linting
yarn add eslint eslint-config-standard@"^17.0.0-1" eslint-plugin-promise eslint-plugin-import eslint-plugin-n --dev
Define style guide for eslint by creating .eslintrc.json. We are using standard to save time by avoiding bikeshedding about code style.
{ "root": true, "env": { "node": true, "es2022": true }, "extends": [ "standard" ] }
Add lint-staged and husky for linting in pre-commit hooks
npx mrm@2 lint-staged
Add following dependencies:
- express as our web framework
- @open-xchange/logging for logging
- @open-xchange/metrics for prometheus based metrics
- helmet for security
- dotenv-defaults for easy env handling
- cloud health connect for readiness/liveness checks
- swagger-ui-express for swagger ui
yarn add express helmet pino-http dotenv-defaults @cloudnative/health-connect prometheus-api-metrics prom-client swagger-ui-express js-yaml
Create .env.defaults file. Note: Only add safe to commit env defaults.
Write the actual code in index.js. See comments in index.js to follow along.
Kubernetes
- Create own namespace (replace yournamespace with your namespace name)
kubectl create namespace yournamespace
- Set current namespace
kubectl config set-context --current --namespace=yournamespace
- Create registry secret
kubectl create secret docker-registry gitlab-registry-credentials --docker-server=registry.gitlab.open-xchange.com --docker-username=$GITLAB_USERNAME --docker-password=$GITLAB_TOKEN --dry-run=client -o yaml | kubectl apply -f -
- Create helm chart and amend templates
mkdir helm
helm create helm/nodejs
Gitlab CI
See comments in .gitlab-ci.yml.
3 years ago