This commit is contained in:
b1ek 2023-02-13 11:16:46 +10:00
commit 040895a036
16 changed files with 246 additions and 0 deletions

11
.gitignore vendored Normal file
View File

@ -0,0 +1,11 @@
# package management
node_modules
package-lock.json
pnpm-lock.yaml
yarn.lock
# env
.env
# code
!*.js

18
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,18 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach to docker",
"restart": true,
"localRoot": ".",
"remoteRoot": "/opt/code",
"address": "localhost",
"port": 9229
}
]
}

11
Dockerfile Normal file
View File

@ -0,0 +1,11 @@
FROM node:19
WORKDIR /opt/code
COPY . /opt/code
RUN cat .gitignore | xargs rm -rf && \
npm i && \
./install
CMD [ "bash", "-c", "if [[ $APP_DEBUG == 'true' ]]; then npm run dev; else npm run prod; fi" ]

14
docker-compose.yml Normal file
View File

@ -0,0 +1,14 @@
version: '3'
services:
server:
build:
context: .
dockerfile: Dockerfile
volumes:
- './:/opt/code'
ports:
- '${APP_PORT}:${APP_PORT}'
- '9229:9229'
env_file: ./.env
environment:
APP_PORT: '${APP_PORT}'

40
gulpfile.js Normal file
View File

@ -0,0 +1,40 @@
const gulp = require('gulp');
const spawn = require('child_process').spawn;
/*function clean(cb) {
cb();
}
function js(cb) {
cb();
}
function css(cb) {
cb();
}*/
gulp.task("serve_dev", (cb) => {
console.log('Launching node...');
let node = spawn('node', ['--inspect=0.0.0.0', 'index.js'], {stdio: 'inherit'})
function files(cb) {
console.log('Restarting node...');
node.kill('SIGTERM');
node = spawn('node', ['--inspect=0.0.0.0', 'index.js'], {stdio: 'inherit'})
cb();
}
gulp.watch("**", { events: 'all' }, files);
cb();
});
gulp.task("serve", (cb) => {
console.log('Launching node...');
spawn('node', [, 'index.js'], {stdio: 'inherit'})
cb();
});
exports.default = function() {
gulp.watch("**", files);
}

3
helpers/index.js Normal file
View File

@ -0,0 +1,3 @@
module.exports = {
ViewLoader: require('./view_loader')
}

34
helpers/view_loader.js Normal file
View File

@ -0,0 +1,34 @@
const Eta = require('eta');
const fs = require('fs/promises');
const glob = require('glob');
const cwd = process.cwd();
const layoutdir = cwd + '/view';
let compiled = {};
async function loadFile(file) {
if (compiled[file]) return compiled[file];
compiled[file] = Eta.compile(await fs.readFile(file, 'utf8'));
return compiled[file];
}
async function preload() {
glob(layoutdir + '/*.eta', (err, files) => {
files.filter(file => {return !file.startsWith('.');})
.forEach(file => {
loadFile(file);
});
});
}
async function load(name, data) {
return (await loadFile(cwd + '/view/' + name))(data, Eta.config);
}
module.exports = { load, loadFile, preload }

14
index.js Normal file
View File

@ -0,0 +1,14 @@
// do startup jobs
require('./startup');
const express = require('express');
const app = express();
const { APP_PORT } = process.env;
app.use(require('./routes'));
const server = app.listen(APP_PORT, () => {
console.log("Listening on port " + APP_PORT);
});

17
install Executable file
View File

@ -0,0 +1,17 @@
#!/bin/bash
PACKAGES="gulp-cli"
if [[ $APP_DEBUG == 'true' ]]; then
npm i
else
npm i --prod
fi
echo -e "Installing \033[32m$PACKAGES\033[0m"
if [[ APP_DEBUG == 'true' ]]; then
npm i -g $PACKAGES
else
npm i -g --prod $PACKAGES
fi
echo "All done."

4
nodemon.json Normal file
View File

@ -0,0 +1,4 @@
{
"watch": ["."],
"ext": "js, json, eta,"
}

23
package.json Normal file
View File

@ -0,0 +1,23 @@
{
"name": "homepage.js",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "gulp serve_dev",
"prod": "node index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"dotenv": "^16.0.3",
"eta": "^2.0.0",
"express": "^4.18.2",
"glob": "^8.1.0"
},
"devDependencies": {
"gulp": "^4.0.2"
}
}

9
routes/homepage.js Normal file
View File

@ -0,0 +1,9 @@
const Helpers = require('../helpers');
async function handler(req, res) {
res.send(await Helpers.ViewLoader.load('welcome.eta'));
}
module.exports = (router) => {
router.get('/', handler);
}

19
routes/index.js Normal file
View File

@ -0,0 +1,19 @@
const express = require('express');
const router = express.Router();
const glob = require('glob');
glob(__dirname + "/**/*.js", {}, (er, data) => {
if (er) {
console.error(er);
process.exit(-1);
}
data
.filter(file => {
return !file.endsWith('index.js')
})
.forEach(file => {
require(file)(router);
});
});
module.exports = router;

27
startup.js Normal file
View File

@ -0,0 +1,27 @@
console.log('Executing startup jobs...');
const fs = require('fs');
const hrt = () => {
let hr = process.hrtime();
return hr[0] + hr[1] / 1000000;
}
async function startup() {
let t1 = hrt();
let dotpath = (process.env.APP_DEBUG == 'true') ? '.env.debug' : '.env.prod';
if (!fs.existsSync(dotpath)) dotpath = '.env';
require('dotenv').config({
path: dotpath
});
await require('./helpers').ViewLoader.preload();
console.log('Views compiled in ' + (hrt() - t1) + ' ms');
console.log('Finished in ' + (hrt() - t1) + " ms");
}
startup();

0
view/layout/main.eta Normal file
View File

2
view/welcome.eta Normal file
View File

@ -0,0 +1,2 @@
hi<br/>
<%~ Date.now() %>