Skip to content

Commit d2a8767

Browse files
authored
App setup with versioned API, html route and linter (#1)
Initial implementation of app with versioned API endpoints and HTML routes that consume the internal API running on express and nunjucks template engine. Made use of dotenv module to deal with environment variables. Running linters in GitHub Actions.
1 parent 9205ded commit d2a8767

19 files changed

+1753
-0
lines changed

.cspell.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"ignorePaths": [
3+
"**/node_modules/**",
4+
"**/vscode-extension/**",
5+
"**/.git/**",
6+
"**/.pnpm-lock.json",
7+
".vscode",
8+
"megalinter",
9+
"package-lock.json",
10+
"report",
11+
".env",
12+
".github"
13+
],
14+
"language": "en",
15+
"noConfigSearch": true,
16+
"words": [
17+
"megalinter",
18+
"oxsecurity",
19+
"autoescape",
20+
"Bame",
21+
"venv",
22+
"stefanzweifel",
23+
"SHELLCHECK",
24+
"DEVSKIM",
25+
"gitmessage"
26+
],
27+
"version": "0.2"
28+
}

.env.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
API_BASE_HOST=http://127.0.0.1:3000
2+
PORT=3000

.github/workflows/mega-linter.yml

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
---
2+
# MegaLinter GitHub Action configuration file
3+
# More info at https://megalinter.io
4+
name: MegaLinter
5+
6+
on:
7+
# Trigger mega-linter at every push. Action will also be visible from Pull Requests to master
8+
push: # Comment this line to trigger action only on pull-requests (not recommended if you don't pay for GH Actions)
9+
pull_request:
10+
branches: [master, main]
11+
12+
env: # Comment env block if you do not want to apply fixes
13+
# Apply linter fixes configuration
14+
APPLY_FIXES: all # When active, APPLY_FIXES must also be defined as environment variable (in github/workflows/mega-linter.yml or other CI tool)
15+
APPLY_FIXES_EVENT: pull_request # Decide which event triggers application of fixes in a commit or a PR (pull_request, push, all)
16+
APPLY_FIXES_MODE: commit # If APPLY_FIXES is used, defines if the fixes are directly committed (commit) or posted in a PR (pull_request)
17+
18+
concurrency:
19+
group: ${{ github.ref }}-${{ github.workflow }}
20+
cancel-in-progress: true
21+
22+
jobs:
23+
build:
24+
name: MegaLinter
25+
runs-on: ubuntu-latest
26+
permissions:
27+
# Give the default GITHUB_TOKEN write permission to commit and push, comment issues & post new PR
28+
# Remove the ones you do not need
29+
contents: write
30+
issues: write
31+
pull-requests: write
32+
steps:
33+
# Git Checkout
34+
- name: Checkout Code
35+
uses: actions/checkout@v3
36+
with:
37+
token: ${{ secrets.PAT || secrets.GITHUB_TOKEN }}
38+
fetch-depth: 0 # If you use VALIDATE_ALL_CODEBASE = true, you can remove this line to improve performances
39+
40+
# MegaLinter
41+
- name: MegaLinter
42+
id: ml
43+
# You can override MegaLinter flavor used to have faster performances
44+
# More info at https://megalinter.io/flavors/
45+
uses: oxsecurity/megalinter/flavors/javascript@v7
46+
env:
47+
# All available variables are described in documentation
48+
# https://megalinter.io/configuration/
49+
VALIDATE_ALL_CODEBASE: true # Set ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }} to validate only diff with main branch
50+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
51+
# ADD YOUR CUSTOM ENV VARIABLES HERE TO OVERRIDE VALUES OF .mega-linter.yml AT THE ROOT OF YOUR REPOSITORY
52+
53+
# Upload MegaLinter artifacts
54+
- name: Archive production artifacts
55+
if: ${{ success() }} || ${{ failure() }}
56+
uses: actions/upload-artifact@v3
57+
with:
58+
name: MegaLinter reports
59+
path: |
60+
megalinter-reports
61+
mega-linter.log
62+
63+
# Create pull request if applicable (for now works only on PR from same repository, not from forks)
64+
- name: Create Pull Request with applied fixes
65+
id: cpr
66+
if: steps.ml.outputs.has_updated_sources == 1 && (env.APPLY_FIXES_EVENT == 'all' || env.APPLY_FIXES_EVENT == github.event_name) && env.APPLY_FIXES_MODE == 'pull_request' && (github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository) && !contains(github.event.head_commit.message, 'skip fix')
67+
uses: peter-evans/create-pull-request@v5
68+
with:
69+
token: ${{ secrets.PAT || secrets.GITHUB_TOKEN }}
70+
commit-message: '[MegaLinter] Apply linters automatic fixes'
71+
title: '[MegaLinter] Apply linters automatic fixes'
72+
labels: bot
73+
- name: Create PR output
74+
if: steps.ml.outputs.has_updated_sources == 1 && (env.APPLY_FIXES_EVENT == 'all' || env.APPLY_FIXES_EVENT == github.event_name) && env.APPLY_FIXES_MODE == 'pull_request' && (github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository) && !contains(github.event.head_commit.message, 'skip fix')
75+
run: |
76+
echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}"
77+
echo "Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}"
78+
79+
# Push new commit if applicable (for now works only on PR from same repository, not from forks)
80+
- name: Prepare commit
81+
if: steps.ml.outputs.has_updated_sources == 1 && (env.APPLY_FIXES_EVENT == 'all' || env.APPLY_FIXES_EVENT == github.event_name) && env.APPLY_FIXES_MODE == 'commit' && github.ref != 'refs/heads/main' && (github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository) && !contains(github.event.head_commit.message, 'skip fix')
82+
run: sudo chown -Rc $UID .git/
83+
- name: Commit and push applied linter fixes
84+
if: steps.ml.outputs.has_updated_sources == 1 && (env.APPLY_FIXES_EVENT == 'all' || env.APPLY_FIXES_EVENT == github.event_name) && env.APPLY_FIXES_MODE == 'commit' && github.ref != 'refs/heads/main' && (github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository) && !contains(github.event.head_commit.message, 'skip fix')
85+
uses: stefanzweifel/git-auto-commit-action@v4
86+
with:
87+
branch: ${{ github.event.pull_request.head.ref || github.head_ref || github.ref }}
88+
commit_message: '[MegaLinter] Apply linters fixes'
89+
commit_user_name: megalinter-bot
90+
commit_user_email: nicolas.vuillamy@ox.security

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
.env
3+
megalinter-reports/

.gitmessage

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Title: Summary, imperative, start upper case, don't end with a period
2+
# No more than 50 chars. #### 50 chars is here: #
3+
4+
# Remember blank line between title and body.
5+
6+
# Body: Explain *what* and *why* (not *how*). Include task ID (Jira issue).
7+
# Wrap at 72 chars. ################################## which is here: #
8+
9+
10+
# At the end: Include Co-authored-by for all contributors.
11+
# Include at least one empty line before it. Format:
12+
# Co-authored-by: name <user@users.noreply.github.com>
13+
#
14+
# How to Write a Git Commit Message:
15+
# https://chris.beams.io/posts/git-commit/
16+
#
17+
# 1. Separate subject from body with a blank line
18+
# 2. Limit the subject line to 50 characters
19+
# 3. Capitalize the subject line
20+
# 4. Do not end the subject line with a period
21+
# 5. Use the imperative mood in the subject line
22+
# 6. Wrap the body at 72 characters
23+
# 7. Use the body to explain what and why vs. how

.jscpd.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"threshold": 0,
3+
"reporters": ["html", "markdown"],
4+
"ignore": [
5+
"**/node_modules/**",
6+
"**/.git/**",
7+
"**/.rbenv/**",
8+
"**/.venv/**",
9+
"**/*cache*/**",
10+
"**/.github/**",
11+
"**/.idea/**",
12+
"**/report/**",
13+
"**/*.svg",
14+
"megalinter-reports"
15+
]
16+
}

.mega-linter.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
APPLY_FIXES: all # all, none, or list of linter keys
2+
SHOW_ELAPSED_TIME: true
3+
FILEIO_REPORTER: false
4+
DISABLE_LINTERS:
5+
- BASH_SHELLCHECK
6+
- REPOSITORY_CHECKOV
7+
- REPOSITORY_DEVSKIM
8+
- ACTION_ACTIONLINT

.prettierrc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"trailingComma": "es5",
3+
"tabWidth": 4,
4+
"semi": false,
5+
"singleQuote": true
6+
}

package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "bitly",
3+
"version": "1.0.0",
4+
"description": "Simple API demo application",
5+
"main": "index.js",
6+
"author": "Albion Bame",
7+
"license": "MIT",
8+
"private": true,
9+
"scripts": {
10+
"start:dev": "nodemon src/app.ts"
11+
},
12+
"dependencies": {
13+
"dotenv": "16.3.1",
14+
"express": "4.18.2",
15+
"nunjucks": "3.2.4"
16+
},
17+
"devDependencies": {
18+
"@types/express": "4.17.17",
19+
"@types/nunjucks": "3.2.3",
20+
"eslint": "8.44.0",
21+
"nodemon": "2.0.22",
22+
"prettier": "2.8.8",
23+
"ts-node": "10.9.1",
24+
"typescript": "5.1.6"
25+
}
26+
}

src/app.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import dotenv from 'dotenv'
2+
3+
import express from 'express'
4+
import nunjucks from 'nunjucks'
5+
import routes from './routes'
6+
7+
dotenv.config()
8+
9+
const port = process.env.PORT ?? 3000
10+
const app = express()
11+
12+
nunjucks.configure('src/views', {
13+
autoescape: true,
14+
express: app
15+
})
16+
17+
app.set('view engine', 'html')
18+
19+
routes(app)
20+
21+
app.listen(port, () => {
22+
console.log(`App listening on port ${port}`)
23+
})

src/routes/api/v1/hello-world.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type { Request, Response } from 'express'
2+
import express from 'express'
3+
4+
const router = express.Router()
5+
6+
router.get('/', (_request: Request, response: Response) => {
7+
response.json({
8+
status: 'success',
9+
message: 'Hello World API!'
10+
})
11+
})
12+
export default router

src/routes/api/v1/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Express } from 'express'
2+
import helloWorldRoute from './hello-world'
3+
4+
export default (app: Express) => {
5+
app.use('/api/v1/hello-world', helloWorldRoute)
6+
}

src/routes/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Express } from 'express'
2+
import v1 from './api/v1'
3+
import render from './render'
4+
5+
export default (app: Express) => {
6+
v1(app)
7+
render(app)
8+
}

src/routes/render/hello-world.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { Request, Response } from 'express'
2+
import express from 'express'
3+
4+
const router = express.Router()
5+
6+
router.get('/', async (_request: Request, response: Response) => {
7+
const result = await fetch(`${process.env.API_BASE_HOST}/api/v1/hello-world`)
8+
const data = await result.json()
9+
10+
response.render('hello-world.html', {
11+
message: data.message
12+
})
13+
})
14+
export default router

src/routes/render/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Express } from 'express'
2+
import helloWorldRoute from './hello-world'
3+
4+
export default (app: Express) => {
5+
app.use('/hello-world', helloWorldRoute)
6+
}

src/views/base.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<title>{{ title }}</title>
7+
<meta name="description" content="">
8+
<meta name="viewport" content="width=device-width, initial-scale=1">
9+
<meta name="keywords" content="NodeJs, Terraform, DemoApp">
10+
</head>
11+
<body>
12+
{% block content %}{% endblock %}
13+
</body>
14+
</html>

src/views/hello-world.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!DOCTYPE html>
2+
{% extends 'base.html' %}
3+
4+
{% set title = 'Hello World' %}
5+
6+
{% block content %}
7+
<h1>{{ message }}</h1>
8+
{% endblock %}

tsconfig.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"esModuleInterop": true,
5+
"sourceMap": false
6+
},
7+
"exclude": ["node_modules"]
8+
}

0 commit comments

Comments
 (0)