Skip to content
This repository has been archived by the owner on Oct 7, 2022. It is now read-only.

Commit

Permalink
feat: add nodejs sample
Browse files Browse the repository at this point in the history
  • Loading branch information
upsetbit committed May 12, 2022
1 parent 697ab5e commit 32eeb0c
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 0 deletions.
1 change: 1 addition & 0 deletions samples/node/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
1 change: 1 addition & 0 deletions samples/node/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
28 changes: 28 additions & 0 deletions samples/node/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM caian/sample-devcontainer:latest AS devcontainer

ARG DOCKER_DEFAULT_USER=turing
ENV DOCKER_DEFAULT_USER $DOCKER_DEFAULT_USER

USER ${DOCKER_DEFAULT_USER}
WORKDIR /home/${DOCKER_DEFAULT_USER}

COPY entrypoint.sh .
RUN sudo chmod +x entrypoint.sh


FROM devcontainer AS dependencies
USER root
COPY package.json .
COPY package-lock.json .

RUN eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)" \
&& npm install --only=production \
&& rm package.json package-lock.json


FROM dependencies AS run
COPY index.js .
RUN chown -R "${DOCKER_DEFAULT_USER}:${DOCKER_DEFAULT_USER}" "/home/${DOCKER_DEFAULT_USER}"

USER ${DOCKER_DEFAULT_USER}
ENTRYPOINT ["./entrypoint.sh"]
5 changes: 5 additions & 0 deletions samples/node/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"

node index.js "$@"
32 changes: 32 additions & 0 deletions samples/node/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const axios = require('axios')
const dayjs = require('dayjs')

const capitalize = s => s && s[0].toUpperCase() + s.slice(1)

const getDate = (daysToAdd = 0) => dayjs().add(daysToAdd, 'days').format('YYYY-MM-DD')

function getCityArg() {
const fallback = 'manhattan'

const city = (process.argv.length < 3 ? fallback : process.argv[2]).trim()
return city.length > 0 ? city : fallback
}

async function getWeatherInfoOf(city) {
const { data } = await axios.get(`https://goweather.herokuapp.com/weather/${city}`)
const forecast = [{ day: '0', temperature: data.temperature, wind: data.wind }, ...data.forecast]

return forecast.map((f) => ({ day: parseInt(f.day), temp: f.temperature, wind: f.wind }))
}

async function main() {
const city = getCityArg()
const forecast = await getWeatherInfoOf(city)

console.log(`Weather forecast for ${capitalize(city)} city\n`)
for (const weather of forecast) {
console.log(` | ${getDate(weather.day)} ~ ${weather.temp}, ${weather.wind}`)
}
}

main().catch((e) => console.error(e))
168 changes: 168 additions & 0 deletions samples/node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions samples/node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "node-weather-app",
"version": "0.1.0",
"main": "index.js",
"author": "Caian Ertl <[email protected]>",
"license": "CC0-1.0",
"dependencies": {
"axios": "^0.27.2",
"dayjs": "^1.11.2"
}
}

0 comments on commit 32eeb0c

Please sign in to comment.