Skip to content

Commit 105c27c

Browse files
example nodejs project
1 parent f820594 commit 105c27c

File tree

14 files changed

+270
-0
lines changed

14 files changed

+270
-0
lines changed

examples/nodeproj/.eslintrc.json

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"env": {
3+
"node": true,
4+
"es6": true
5+
},
6+
"settings": {
7+
"import/resolver": {
8+
"node": {
9+
"extensions": [".js", ".mjs", ".ts", ".cjs"]
10+
}
11+
}
12+
},
13+
"parser": "@typescript-eslint/parser",
14+
"parserOptions": {
15+
"ecmaVersion": 2020,
16+
"sourceType": "module",
17+
"allowImportExportEverywhere": true
18+
},
19+
"extends": [
20+
"eslint:recommended",
21+
"plugin:import/errors",
22+
"plugin:import/warnings",
23+
"plugin:import/typescript",
24+
"plugin:promise/recommended",
25+
"google",
26+
"plugin:security/recommended"
27+
],
28+
"plugins": ["promise", "security", "import"],
29+
"overrides": [
30+
{
31+
"files": "public/**/*.min.js",
32+
"env": {
33+
"browser": true,
34+
"node": false,
35+
"es6": false
36+
},
37+
"parserOptions": {
38+
"sourceType": "script"
39+
},
40+
"extends": ["plugin:compat/recommended"],
41+
"plugins": [],
42+
"rules": {
43+
"no-var": ["off"]
44+
}
45+
}
46+
],
47+
"rules": {
48+
"security/detect-non-literal-fs-filename":["off"],
49+
"security/detect-object-injection":["off"],
50+
"camelcase": ["off"],
51+
"no-console": ["off"],
52+
"require-jsdoc": ["off"],
53+
"one-var": ["off"],
54+
"guard-for-in": ["off"],
55+
"max-len": [
56+
"warn",
57+
{
58+
"ignoreComments": true,
59+
"ignoreTrailingComments": true,
60+
"ignoreUrls": true,
61+
"code": 200
62+
}
63+
],
64+
"indent": ["warn", 4],
65+
"no-unused-vars": ["warn"],
66+
"no-extra-semi": ["warn"],
67+
"linebreak-style": ["error", "unix"],
68+
"quotes": ["warn", "double"],
69+
"semi": ["error", "always"]
70+
}
71+
}

examples/nodeproj/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
charmander-local.rc
2+
.DS_Store
3+
*~
4+
*.seed
5+
*.log
6+
*.pid
7+
node_modules

examples/nodeproj/.home/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*

examples/nodeproj/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# How to run example
2+
3+
4+
5+
```
6+
cp example.env local.env
7+
podman-compose build
8+
podman-compose run --rm --no-deps init
9+
podman-compose up
10+
```
11+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM registry.fedoraproject.org/fedora-minimal:35
2+
ARG NODE_VER=16
3+
# microdnf -y module enable nodejs:${NODE_VER}
4+
RUN \
5+
echo -e "[nodejs]\nname=nodejs\nstream=${NODE_VER}\nprofiles=\nstate=enabled\n" > /etc/dnf/modules.d/nodejs.module && \
6+
microdnf -y install shadow-utils nodejs zopfli findutils busybox && \
7+
microdnf clean all
8+
RUN adduser -d /app app && mkdir -p /app/code/.home && chown app:app -R /app/code && chmod 711 /app /app/code/.home && usermod -d /app/code/.home app
9+
ENV XDG_CONFIG_HOME=/app/code/.home
10+
ENV HOME=/app/code/.home
11+
WORKDIR /app/code
12+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
version: '3'
2+
volumes:
3+
redis:
4+
services:
5+
redis:
6+
read_only: true
7+
image: docker.io/redis:alpine
8+
command: ["redis-server", "--appendonly", "yes", "--notify-keyspace-events", "Ex"]
9+
volumes:
10+
- redis:/data
11+
tmpfs:
12+
- /tmp
13+
- /var/run
14+
- /run
15+
init:
16+
read_only: true
17+
userns_mode: keep-id
18+
build:
19+
context: ./containers/${NODE_IMG:-node16-runtime}
20+
image: ${NODE_IMG:-node16-runtime}
21+
env_file:
22+
- local.env
23+
volumes:
24+
- .:/app/code
25+
command: ["/bin/sh", "-c", "mkdir -p ~/; [ -d ./node_modules ] && echo '** node_modules exists' || npm install"]
26+
tmpfs:
27+
- /tmp
28+
- /run
29+
task:
30+
extends:
31+
service: init
32+
command: ["npm", "run", "cli", "--", "task"]
33+
links:
34+
- redis
35+
depends_on:
36+
- redis
37+
web:
38+
extends:
39+
service: init
40+
command: ["npm", "run", "cli", "--", "web"]
41+
ports:
42+
- ${WEB_LISTEN_PORT:-3000}:3000
43+
depends_on:
44+
- redis
45+
links:
46+
- mongo
47+

examples/nodeproj/example.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
REDIS_HOST=redis
2+

examples/nodeproj/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#! /usr/bin/env node
2+
"use strict";
3+
import {start} from "./lib";
4+
5+
start();
6+

examples/nodeproj/jsconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2020",
4+
"module": "es2020",
5+
"moduleResolution": "node",
6+
"allowSyntheticDefaultImports": true
7+
},
8+
"files": [
9+
"index.js"
10+
],
11+
"include": [
12+
"lib/**/*.js"
13+
]
14+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"use strict";
2+
import {proj} from "../proj";
3+
4+
async function loop() {
5+
const poped = await proj.predis.blpop("queue", 5);
6+
const task_desc_s = poped[1];
7+
let task_desc;
8+
try {
9+
task_desc = JSON.parse(task_desc_s);
10+
} catch (e) {
11+
proj.logger.exception(e);
12+
}
13+
proj.logger.info("got task "+task_desc.func);
14+
const func = task_desc.func;
15+
const args = task_desc.args;
16+
if (typeof(proj.tasks[func])!="function") {
17+
console.log(`task ${func} not found`);
18+
process.exit(-1)
19+
}
20+
try {
21+
await ((this.tasks[func])(...args));
22+
} catch (e) {
23+
console.exception(e);
24+
}
25+
}
26+
27+
export async function start() {
28+
while(true) {
29+
loop();
30+
}
31+
}

0 commit comments

Comments
 (0)