From 040895a036484768ffabeaddf845b94fd04685bb Mon Sep 17 00:00:00 2001 From: b1ek Date: Mon, 13 Feb 2023 11:16:46 +1000 Subject: [PATCH] base --- .gitignore | 11 +++++++++++ .vscode/launch.json | 18 ++++++++++++++++++ Dockerfile | 11 +++++++++++ docker-compose.yml | 14 ++++++++++++++ gulpfile.js | 40 ++++++++++++++++++++++++++++++++++++++++ helpers/index.js | 3 +++ helpers/view_loader.js | 34 ++++++++++++++++++++++++++++++++++ index.js | 14 ++++++++++++++ install | 17 +++++++++++++++++ nodemon.json | 4 ++++ package.json | 23 +++++++++++++++++++++++ routes/homepage.js | 9 +++++++++ routes/index.js | 19 +++++++++++++++++++ startup.js | 27 +++++++++++++++++++++++++++ view/layout/main.eta | 0 view/welcome.eta | 2 ++ 16 files changed, 246 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/launch.json create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 gulpfile.js create mode 100644 helpers/index.js create mode 100644 helpers/view_loader.js create mode 100644 index.js create mode 100755 install create mode 100644 nodemon.json create mode 100644 package.json create mode 100644 routes/homepage.js create mode 100644 routes/index.js create mode 100644 startup.js create mode 100644 view/layout/main.eta create mode 100644 view/welcome.eta diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..28ef80a --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +# package management +node_modules +package-lock.json +pnpm-lock.yaml +yarn.lock + +# env +.env + +# code +!*.js \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..23a0677 --- /dev/null +++ b/.vscode/launch.json @@ -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 + } + ] +} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..5a875e1 --- /dev/null +++ b/Dockerfile @@ -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" ] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..daeb419 --- /dev/null +++ b/docker-compose.yml @@ -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}' diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..64d5e1f --- /dev/null +++ b/gulpfile.js @@ -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); +} \ No newline at end of file diff --git a/helpers/index.js b/helpers/index.js new file mode 100644 index 0000000..3b2d98c --- /dev/null +++ b/helpers/index.js @@ -0,0 +1,3 @@ +module.exports = { + ViewLoader: require('./view_loader') +} \ No newline at end of file diff --git a/helpers/view_loader.js b/helpers/view_loader.js new file mode 100644 index 0000000..0cd2f2d --- /dev/null +++ b/helpers/view_loader.js @@ -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 } \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..d4ef49a --- /dev/null +++ b/index.js @@ -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); +}); diff --git a/install b/install new file mode 100755 index 0000000..61e6333 --- /dev/null +++ b/install @@ -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." \ No newline at end of file diff --git a/nodemon.json b/nodemon.json new file mode 100644 index 0000000..4252efc --- /dev/null +++ b/nodemon.json @@ -0,0 +1,4 @@ +{ + "watch": ["."], + "ext": "js, json, eta," +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..96b3fcc --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/routes/homepage.js b/routes/homepage.js new file mode 100644 index 0000000..e047893 --- /dev/null +++ b/routes/homepage.js @@ -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); +} \ No newline at end of file diff --git a/routes/index.js b/routes/index.js new file mode 100644 index 0000000..8e289f3 --- /dev/null +++ b/routes/index.js @@ -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; \ No newline at end of file diff --git a/startup.js b/startup.js new file mode 100644 index 0000000..ad58bd6 --- /dev/null +++ b/startup.js @@ -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(); diff --git a/view/layout/main.eta b/view/layout/main.eta new file mode 100644 index 0000000..e69de29 diff --git a/view/welcome.eta b/view/welcome.eta new file mode 100644 index 0000000..739a35a --- /dev/null +++ b/view/welcome.eta @@ -0,0 +1,2 @@ +hi
+<%~ Date.now() %> \ No newline at end of file