Skip to content

Viorel1989/fastify-hello-world

Repository files navigation

Known Vulnerabilities Coverage Status Conventional Commits

Simple REST API based on Fastify framework.

Run locally

Clone the project

  git clone https://github.com/Viorel1989/fastify-hello-world.git

Go to the project directory

  cd fastify-hello-world

Run npm install to install required depdendencies

  npm install

Start server

  npm start

Requests sent to localhost:3000 will respond with the following json:

{
  "hello": "world"
}

Running Tests

To run tests, run the following command

  npm run test:unit

Install pre-commit hooks

Before you start, make sure you have the following installed on your machine:

  pip install pre-commit
  pre-commit install --hook-type commit-msg

Build Azure Image

Create resource group

az group create -n fastifyResourceGroup --location westeurope

Create service principal

# !!! Save the content of $AZURE_SECRETS in safe place for long-term storage
AZURE_SECRETS=$(az ad sp create-for-rbac -n "$USER@$(hostname -f)" --role Contributor --scopes /subscriptions/$(az account show --query "{ subscription_id: id }" | jq -r ".subscription_id") --query "{ client_id: appId, client_secret: password, tenant_id: tenant }")

Build image

packer init fastifyVM.pkr.hcl

packer build \
  -var "subscription_id=$(az account show --query "{ subscription_id: id }" | jq -r ".subscription_id")" \
  -var "tenant_id=$(echo $AZURE_SECRETS | jq -r ".tenant_id")" \
  -var "client_id=$(echo $AZURE_SECRETS | jq -r ".client_id")" \
  -var "client_secret=$(echo $AZURE_SECRETS | jq -r ".client_secret")" \
  -var "version=sha-$(git rev-parse --short HEAD)" \
  fastifyVM.pkr.hcl

Create VM

az vm create \
    --resource-group fastifyResourceGroup \
    --name fastify-hello-world \
    --image fastifyVM-sha-$(git rev-parse --short HEAD) \
    --size Standard_B2ats_v2 \
    --admin-username $USER \
    --ssh-key-values ~/.ssh/id_ed25519.pub

az vm open-port \
    --resource-group fastifyResourceGroup \
    --name fastify-hello-world \
    --port 3000

curl $(az vm list-ip-addresses --name fastify-hello-world --resource-group fastifyResourceGroup --query "[0].virtualMachine.network.publicIpAddresses[0].ipAddress" | jq -r):3000

az resource delete --ids $(az resource list -g fastifyResourceGroup --query "[].id" -o tsv)

Optimizations

  • Installed Commitizen as pre-commit hook to ensure Semantic Versioning and Conventional Commits specifications
  • Added editorconfig, prettier and eslint for formatting consistentcy

Acknowledgements

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published