Added basic CDN file serving

This commit is contained in:
Jonathan Barrow 2021-08-22 18:24:32 -04:00
commit 3d9a93385c
11 changed files with 1343 additions and 0 deletions

1
.eslintignore Normal file
View File

@ -0,0 +1 @@
node_modules

43
.eslintrc Normal file
View File

@ -0,0 +1,43 @@
{
"env": {
"node": true,
"commonjs": true,
"es6": true
},
"parserOptions": {
"ecmaVersion": 2020
},
"globals": {
"BigInt": true
},
"extends": "eslint:recommended",
"rules": {
"require-atomic-updates": "warn",
"no-case-declarations": "off",
"no-empty": "off",
"no-console": "off",
"linebreak-style": "off",
"no-global-assign": "off",
"prefer-const": "error",
"no-var": "error",
"one-var": [
"error",
"never"
],
"indent": [
"error",
"tab",
{
"SwitchCase": 1
}
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
}
}

61
.gitignore vendored Normal file
View File

@ -0,0 +1,61 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Typescript v1 declaration files
typings/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
# custom
/cdn

5
README.md Normal file
View File

@ -0,0 +1,5 @@
# BOSS
### Pretendo BOSS server implementation
## About
Handles all BOSS (SpotPass on 3DS) related tasks for the WiiU and 3DS

23
config.json Normal file
View File

@ -0,0 +1,23 @@
{
"http": {
"port": 8001
},
"mongoose": {
"uri": "mongodb://localhost:27017",
"database": "pretendo",
"options": {
"useNewUrlParser": true
}
},
"email": {
"service": "gmail",
"auth": {
"user": "pretendonetwork@gmail.com",
"pass": "pretendopassword"
}
},
"recaptcha": {
"siteKey": "key",
"secretKey": "secret"
}
}

5
example.config.json Normal file
View File

@ -0,0 +1,5 @@
{
"http": {
"port": 8080
}
}

52
logger.js Normal file
View File

@ -0,0 +1,52 @@
const fs = require('fs-extra');
require('colors');
const root = __dirname;
fs.ensureDirSync(`${root}/logs`);
const streams = {
latest: fs.createWriteStream(`${root}/logs/latest.log`),
success: fs.createWriteStream(`${root}/logs/success.log`),
error: fs.createWriteStream(`${root}/logs/error.log`),
warn: fs.createWriteStream(`${root}/logs/warn.log`),
info: fs.createWriteStream(`${root}/logs/info.log`)
};
function success(input) {
const time = new Date();
input = `[${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}] [SUCCESS]: ${input}`;
streams.success.write(`${input}\n`);
console.log(`${input}`.green.bold);
}
function error(input) {
const time = new Date();
input = `[${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}] [ERROR]: ${input}`;
streams.error.write(`${input}\n`);
console.log(`${input}`.red.bold);
}
function warn(input) {
const time = new Date();
input = `[${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}] [WARN]: ${input}`;
streams.warn.write(`${input}\n`);
console.log(`${input}`.yellow.bold);
}
function info(input) {
const time = new Date();
input = `[${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}] [INFO]: ${input}`;
streams.info.write(`${input}\n`);
console.log(`${input}`.cyan.bold);
}
module.exports = {
success,
error,
warn,
info
};

1049
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

21
package.json Normal file
View File

@ -0,0 +1,21 @@
{
"name": "BOSS",
"version": "1.0.0",
"description": "",
"main": "./src/server.js",
"scripts": {
"start": "node ."
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"boss-js": "github:PretendoNetwork/boss-js",
"colors": "^1.4.0",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"express-subdomain": "^1.0.5",
"fs-extra": "^10.0.0",
"morgan": "^1.10.0"
}
}

56
src/server.js Normal file
View File

@ -0,0 +1,56 @@
process.title = 'Pretendo - BOSS';
const express = require('express');
const morgan = require('morgan');
const logger = require('../logger');
const config = require('../config.json');
const { http: { port } } = config;
const app = express();
const nptsService = require('./services/npts');
// START APPLICATION
app.set('etag', false);
app.disable('x-powered-by');
// Create router
logger.info('Setting up Middleware');
app.use(morgan('dev'));
app.use(express.json());
app.use(express.urlencoded({
extended: true
}));
app.use(nptsService);
// 404 handler
logger.info('Creating 404 status handler');
app.use((request, response) => {
response.status(404);
response.json({
app: 'api',
status: 404,
error: 'Route not found'
});
});
// non-404 error handler
logger.info('Creating non-404 status handler');
app.use((error, request, response) => {
const status = error.status || 500;
response.status(status);
response.json({
app: 'api',
status,
error: error.message
});
});
// Starts the server
logger.info('Starting server');
app.listen(port, () => {
logger.success(`Server started on port ${port}`);
});

27
src/services/npts.js Normal file
View File

@ -0,0 +1,27 @@
const path = require('path');
const fs = require('fs-extra');
const express = require('express');
const subdomain = require('express-subdomain');
// Router to handle the subdomain restriction
const npts = express.Router();
// Setup routes
npts.get('/p01/tasksheet/1/:id/:fileName', (request, response) => {
const { id, fileName } = request.params;
const tasksheetPath = path.normalize(`${__dirname}/../../cdn/tasksheet/${id}/${fileName}`);
if (fs.existsSync(tasksheetPath)) {
response.sendFile(tasksheetPath);
} else {
response.sendStatus(404);
}
});
// Main router for endpoints
const router = express.Router();
// Create subdomain
router.use(subdomain('npts.app', npts));
module.exports = router;