Finally finished the massive refactor... transitions still broke but it's fine for now.
This commit is contained in:
@ -21,21 +21,18 @@
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
//Imports
|
||||
const
|
||||
http = require('http'),
|
||||
https = require('https'),
|
||||
express = require('express'),
|
||||
bodyParser = require('body-parser'),
|
||||
fs = require('fs'),
|
||||
path = require('path'),
|
||||
webpack = require('webpack'),
|
||||
CompilerOptions = require('./WebpackCompilerOptions'),
|
||||
API = require('./../api/API')
|
||||
WebpackCompiler = require('./../webpack/WebpackCompiler'),
|
||||
API = require('./api/API')
|
||||
;
|
||||
|
||||
//Constants
|
||||
const LANDING_FILE = 'index.html';
|
||||
const SERVE_FOLDER = path.resolve(`${__dirname}/../../dist`);
|
||||
|
||||
class Server {
|
||||
constructor(app) {
|
||||
@ -43,48 +40,22 @@ class Server {
|
||||
|
||||
//Server settings
|
||||
this.ip =
|
||||
app.getConfig().getValueOf("IP") ||
|
||||
app.getConfig().getValueOf("ip") ||
|
||||
app.getConfig().getValueOf("server.ip") ||
|
||||
app.getConfig().ip ||
|
||||
app.getConfig().get("IP") ||
|
||||
app.getConfig().get("server.ip") ||
|
||||
process.env.ip ||
|
||||
process.env.IP ||
|
||||
null
|
||||
;
|
||||
|
||||
this.port =
|
||||
app.getConfig().getValueOf("PORT") ||
|
||||
app.getConfig().getValueOf("port") ||
|
||||
app.getConfig().getValueOf("server.port") ||
|
||||
app.getConfig().port ||
|
||||
app.getConfig().get("PORT") ||
|
||||
app.getConfig().get("port") ||
|
||||
app.getConfig().get("server.port") ||
|
||||
process.env.port ||
|
||||
process.env.PORT ||
|
||||
80
|
||||
;
|
||||
|
||||
this.useHTTPS = app.getConfig().getValueOf("ssl") && app.getConfig().getValueOf("ssl.enable")
|
||||
if(this.useHTTPS) {
|
||||
this.portHTTPS = this.config.ssl.port || 443;
|
||||
if(!this.config.ssl.key) {
|
||||
throw new Error("Invalid SSL Key in Server Configuration");
|
||||
}
|
||||
if(!this.config.ssl.cert) {
|
||||
throw new Error("Invalid SSL Cert in Server Configuration");
|
||||
}
|
||||
|
||||
//TODO: Clean this up, don't use static files (use path.join etc) and should these be flat files?
|
||||
let keyFile = __dirname+'./../'+this.config.ssl.key;
|
||||
let certFile = __dirname+'./../'+this.config.ssl.cert;
|
||||
if(!fs.existsSync(keyFile)) {
|
||||
throw new Error("Key file \"" + keyFile + "\" doesn't exist!");
|
||||
}
|
||||
if(!fs.existsSync(certFile)) {
|
||||
throw new Error("Key file \"" + certFile + "\" doesn't exist!");
|
||||
}
|
||||
|
||||
this.key = fs.readFileSync(keyFile, 'utf8');
|
||||
this.cert = fs.readFileSync(certFile, 'utf8');
|
||||
}
|
||||
|
||||
//Setup the express wrapper.
|
||||
this.express = express();
|
||||
|
||||
@ -92,160 +63,65 @@ class Server {
|
||||
this.express.use(bodyParser.json({
|
||||
type:'application/json' // to support JSON-encoded bodies
|
||||
}));
|
||||
|
||||
this.express.use(bodyParser.urlencoded({
|
||||
extended: true
|
||||
}));
|
||||
|
||||
//Serve Static Files
|
||||
this.express.use(express.static('./dist'));
|
||||
this.express.use(express.static(SERVE_FOLDER));
|
||||
|
||||
//API Handler
|
||||
//Register API Handlers
|
||||
this.api = new API(this);
|
||||
this.api.loadHandlers();
|
||||
|
||||
//Finally our catcher for all other enquiries
|
||||
this.express.get('*', this.onGetRequest.bind(this));
|
||||
//Setup fallback GET request
|
||||
this.express.get('*', (req,res) => this.onGetRequest(req,res));
|
||||
|
||||
//Setup our webpack compiler
|
||||
this.compiler = webpack(WebpackCompiler());
|
||||
}
|
||||
|
||||
getExpress() {return this.express;}
|
||||
getApp() {return this.app;}
|
||||
getHTTP() {return this.http;}
|
||||
|
||||
async init() {
|
||||
//Create our HTTP and (if needed HTTPS) server(s)
|
||||
this.http = http.createServer(this.express);
|
||||
this.http.on('error', this.onServerError.bind(this));
|
||||
this.http.on('error', e => this.onServerError(e));
|
||||
|
||||
if(this.isHTTPS()) {
|
||||
if(!this.key) throw new Error("Can't start server, missing SSL Key");
|
||||
if(!this.cert) throw new Error("Can't start server, missing SSL Cert");
|
||||
//Start the compiler watching
|
||||
this.watcher = this.compiler.watch({}, (e,s) => this.onWatchChange(e,s));
|
||||
|
||||
this.https = https.createServer({
|
||||
key: this.key,
|
||||
cert: this.cert
|
||||
}, this.express);
|
||||
this.https.on('error', this.onServerError.bind(this));
|
||||
}
|
||||
|
||||
//Create our bundler
|
||||
this.compiler = webpack(CompilerOptions(this, this.app));
|
||||
}
|
||||
|
||||
getConfig() {return this.config;}
|
||||
getIP() {return this.ip; }
|
||||
getPort() {return this.port;}
|
||||
isHTTPS() {return this.useHTTPS;}
|
||||
getHTTPSPort() {return this.portHTTPS;}
|
||||
getKey() {return this.key;}
|
||||
getCertificate() {return this.cert;}
|
||||
getLandingFile() {return path.join(this.app.getPublicDirectory(), LANDING_FILE);}
|
||||
getExpress() {return this.express;}
|
||||
getAPI() {return this.api;}
|
||||
getApp() {return this.app;}
|
||||
|
||||
isRunning() {
|
||||
if(typeof this.http !== typeof undefined) {
|
||||
return this.http.listening;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
async start() {
|
||||
if(typeof this.startPromise !== typeof undefined) {
|
||||
await this.startPromise();
|
||||
return;
|
||||
}
|
||||
this.startPromise = new Promise(this.startServerPromise.bind(this));//Lazy Programming FTW
|
||||
await this.startPromise;
|
||||
}
|
||||
|
||||
startServerPromise(resolve, reject) {
|
||||
this.startResolve = resolve;
|
||||
this.startReject = reject;
|
||||
|
||||
let options = {
|
||||
//Start Listening
|
||||
this.http.listen({
|
||||
host: this.ip,
|
||||
port: this.port
|
||||
};
|
||||
|
||||
//Create our webpack watcher
|
||||
this.watcher = this.compiler.watch({
|
||||
|
||||
}, this.onWatchChange.bind(this));
|
||||
|
||||
//Start the HTTP Server
|
||||
this.http.listen(options, this.onServerStart.bind(this));
|
||||
|
||||
//HTTPS?
|
||||
if(this.https) {
|
||||
this.https.listen(options, this.portHTTPS);
|
||||
}
|
||||
}, e => this.onServerStart(e));
|
||||
}
|
||||
|
||||
onServerStart() {
|
||||
this.bound = this.http.address();
|
||||
this.startResolve(this);
|
||||
//Events
|
||||
onServerStart(e) {
|
||||
this.boundAddress = this.http.address();
|
||||
}
|
||||
|
||||
onServerError(e) {
|
||||
console.log("A Server Error occured!");
|
||||
this.startReject(e);
|
||||
this.stop();
|
||||
throw new Error(e);
|
||||
}
|
||||
|
||||
|
||||
async stop() {
|
||||
if(typeof this.stopPromise !== typeof undefined) {
|
||||
await this.stopPromise;
|
||||
return;
|
||||
}
|
||||
this.stopPromise = new Promse(this.stopPromise.bind(this));
|
||||
await this.stopPromise;
|
||||
delete this.http;
|
||||
delete this.https;
|
||||
delete this.stopPromise;
|
||||
delete this.watcher;
|
||||
}
|
||||
|
||||
stopPromise(resolve, reject) {
|
||||
this.stopResolve = resolve;
|
||||
this.stopReject = reject;
|
||||
|
||||
if(typeof this.watcher !== typeof undefined) {
|
||||
this.watcher.close(() => {
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
this.http.close(this.onHTTPClosed.bind(this));
|
||||
} catch(e) {
|
||||
this.stopReject(e);
|
||||
}
|
||||
}
|
||||
|
||||
onHTTPClosed() {
|
||||
if(typeof this.https === typeof undefined) {
|
||||
this.resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
this.https.close(this.onHTTPSClosed.bind(this));
|
||||
} catch(e) {
|
||||
this.stopReject(e);
|
||||
}
|
||||
}
|
||||
|
||||
onHTTPSClosed() {
|
||||
this.resolve();
|
||||
}
|
||||
|
||||
onWatchChange(error, stats) {
|
||||
if(error || (stats.compilation.errors && stats.compilation.errors.length)) {
|
||||
console.error(error || stats.compilation.errors);
|
||||
} else {
|
||||
console.log("Server compiled!");
|
||||
}
|
||||
console.error('Error');
|
||||
console.error(e);
|
||||
}
|
||||
|
||||
onGetRequest(req, res) {
|
||||
//Used as our "catch all get requests"
|
||||
res.sendFile(this.getLandingFile());
|
||||
let file = path.resolve(`${SERVE_FOLDER}/index.html`);
|
||||
res.sendFile(file);
|
||||
}
|
||||
|
||||
onWatchChange(error,stats) {
|
||||
if(error || (stats.compilation.errors && stats.compilation.errors.length)) {
|
||||
return this.app.error(error || stats.compilation.errors);
|
||||
}
|
||||
|
||||
this.app.log("Server Compiled!");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,151 +0,0 @@
|
||||
// Copyright (c) 2018 Dominic Masters
|
||||
//
|
||||
// MIT License
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
//Includes
|
||||
const
|
||||
path = require('path'),
|
||||
webpack = require('webpack'),
|
||||
HtmlWebpackPlugin = require('html-webpack-plugin'),
|
||||
CompressionPlugin = require("compression-webpack-plugin"),
|
||||
UglifyJsPlugin = require('uglifyjs-webpack-plugin'),
|
||||
MiniCssExtractPlugin = require("mini-css-extract-plugin"),
|
||||
OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin"),
|
||||
SharpLoader = require('responsive-loader/sharp')
|
||||
;
|
||||
|
||||
//Constants
|
||||
const SOURCE_DIRECTORY = './public';
|
||||
const ENTRY_FILE = 'index.jsx';
|
||||
const ENTRY_WRAPPER = 'index.html';
|
||||
|
||||
module.exports = function(server, app) {
|
||||
//Create our dirs
|
||||
let entryDir = path.join(app.getPublicDirectory(), '..', 'public');
|
||||
|
||||
//Create our output
|
||||
let output = {};
|
||||
|
||||
//Set the entry point
|
||||
output.entry = path.join(entryDir, ENTRY_FILE);
|
||||
|
||||
//Set the output
|
||||
output.output = {
|
||||
path: app.getPublicDirectory(),
|
||||
filename: "app.js"
|
||||
}
|
||||
|
||||
//Set Resolves
|
||||
output.resolve = {
|
||||
modules: ['node_modules', SOURCE_DIRECTORY],
|
||||
extensions: ['.js', '.jsx', '.css', '.scss' ]
|
||||
};
|
||||
|
||||
//Setup Modules
|
||||
output.module = {
|
||||
rules: [
|
||||
{
|
||||
test: /\.jsx?$|\.js?$/,
|
||||
exclude: /node_modules/,
|
||||
use: {
|
||||
loader: 'babel-loader'
|
||||
}
|
||||
},
|
||||
{
|
||||
test: /\.scss$|\.css$/i,
|
||||
use: [
|
||||
MiniCssExtractPlugin.loader,
|
||||
"css-loader",
|
||||
'sass-loader',
|
||||
]
|
||||
},
|
||||
|
||||
{
|
||||
test: /\.svg|\.webm|\.mp4$/i,
|
||||
use: [{
|
||||
loader: "file-loader",
|
||||
options: {
|
||||
name: "[path][name].[ext]",
|
||||
context: 'public'
|
||||
}
|
||||
}]
|
||||
},
|
||||
|
||||
{
|
||||
test: /\.jpe?g$|\.gif$|\.png$/i,
|
||||
use: [{
|
||||
loader: "responsive-loader",
|
||||
options: {
|
||||
adapter: SharpLoader,
|
||||
sizes: [250, 500, 1000, 1500, 2000, 2500],
|
||||
name: "[path][name]_[width]x.[ext]",
|
||||
context: 'public'
|
||||
}
|
||||
}]
|
||||
},
|
||||
|
||||
{
|
||||
test: /\.(eot|ttf|woff(2)?)(\?v=\d+\.\d+\.\d+)?/,
|
||||
loader: 'url-loader'
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
//Setup the Plugins
|
||||
let HTMLWebpackPluginConfig = new HtmlWebpackPlugin({
|
||||
template: path.join(SOURCE_DIRECTORY, ENTRY_WRAPPER),
|
||||
filename: ENTRY_WRAPPER,
|
||||
inject: true
|
||||
});
|
||||
|
||||
let UglifyPluginConfig = new UglifyJsPlugin({
|
||||
test: /\.js($|\?)/i
|
||||
});
|
||||
|
||||
let MiniCssExtractConfig = new MiniCssExtractPlugin({
|
||||
filename: "[name].css",
|
||||
chunkFilename: "[id].css"
|
||||
})
|
||||
|
||||
//Set the plugins
|
||||
output.plugins = [
|
||||
new webpack.DefinePlugin({
|
||||
'process.env.NODE_ENV': JSON.stringify('production')
|
||||
}),
|
||||
MiniCssExtractConfig,
|
||||
HTMLWebpackPluginConfig
|
||||
];
|
||||
|
||||
//Minimization
|
||||
output.optimization = {
|
||||
minimize: true,
|
||||
minimizer: [
|
||||
UglifyPluginConfig,
|
||||
new OptimizeCSSAssetsPlugin({})
|
||||
]
|
||||
};
|
||||
|
||||
//Now setup the production values
|
||||
output.devtool = 'source-map';
|
||||
|
||||
return output;
|
||||
}
|
86
private/server/api/API.js
Normal file
86
private/server/api/API.js
Normal file
@ -0,0 +1,86 @@
|
||||
// Copyright (c) 2018 Dominic Masters
|
||||
//
|
||||
// MIT License
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
const
|
||||
path = require('path'),
|
||||
fs = require('fs')
|
||||
;
|
||||
|
||||
const API_BASE = path.resolve(__dirname, 'methods');
|
||||
const API_URL_BASE = '/api';
|
||||
|
||||
module.exports = class API {
|
||||
constructor(server) {
|
||||
this.server = server;
|
||||
this.handlers = [];
|
||||
}
|
||||
|
||||
getHandlers() {return this.handlers;}
|
||||
getServer() {return this.server;}
|
||||
getApp() {return this.getServer().getApp();}
|
||||
getExpress() {return this.getServer().getExpress();}
|
||||
getConfig() {return this.getApp().getConfig();}
|
||||
getEmail() {return this.getApp().getEmail();}
|
||||
|
||||
addHandler(handler) {this.handlers.push(handler);}
|
||||
|
||||
registerHandlers() {
|
||||
this.handlers.forEach(handler => {
|
||||
handler.getMethods().forEach(method => {
|
||||
method = method.toLowerCase();
|
||||
|
||||
//For each method, there's perhaps multiple paths (e.g. post /test, get /test, post /ayy, get /ayy)
|
||||
handler.getPaths().forEach(path => {
|
||||
let url = API_URL_BASE;
|
||||
if(!path.startsWith('/')) url += '/';
|
||||
url += path;
|
||||
|
||||
this.getExpress()[method](url, handler.onMethod.bind(handler));
|
||||
console.log('Registering ' + url + '...');
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
loadHandlers() {
|
||||
this.loadHandlersInDirectory(API_BASE);
|
||||
this.registerHandlers();
|
||||
}
|
||||
|
||||
loadHandlersInDirectory(dir) {
|
||||
let assets = fs.readdirSync(dir);
|
||||
assets.forEach(asset => {
|
||||
let assetPath = path.join(dir, asset);
|
||||
let stats = fs.statSync(assetPath);
|
||||
if(stats.isDirectory()) {
|
||||
this.loadHandlersInDirectory(assetPath );
|
||||
return;
|
||||
}
|
||||
|
||||
let method = require(assetPath);
|
||||
let instance = new method(this);
|
||||
|
||||
this.addHandler(instance);
|
||||
});
|
||||
}
|
||||
}
|
54
private/server/api/APIHandler.js
Normal file
54
private/server/api/APIHandler.js
Normal file
@ -0,0 +1,54 @@
|
||||
// Copyright (c) 2018 Dominic Masters
|
||||
//
|
||||
// MIT License
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
const
|
||||
API = require('./API'),
|
||||
APIRequest = require('./APIRequest')
|
||||
;
|
||||
|
||||
class APIHandler {
|
||||
constructor(api, methods, paths) {
|
||||
if(!(api instanceof API)) throw new Error('Invalid API Supplied!');
|
||||
if(typeof methods === typeof undefined) methods = ['GET'];
|
||||
if(typeof paths === typeof undefined) paths = [];
|
||||
if(typeof methods === "string") methods = [ methods ];
|
||||
if(typeof paths === "string") paths = [ paths ];
|
||||
this.api = api;
|
||||
this.methods = methods;
|
||||
this.paths = paths;
|
||||
}
|
||||
|
||||
getAPI() {return this.api;}
|
||||
getMethods() {return this.methods;}
|
||||
getPaths() {return this.paths;}
|
||||
|
||||
onMethod(req, res) {
|
||||
//Now that we have a request we need to create a nice wrapper for it, pass
|
||||
//it to our method, and then expect a nice JSON object to send back to the
|
||||
//client.
|
||||
let request = new APIRequest(this, req, res);
|
||||
request.process();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = APIHandler;
|
232
private/server/api/APIRequest.js
Normal file
232
private/server/api/APIRequest.js
Normal file
@ -0,0 +1,232 @@
|
||||
// Copyright (c) 2018 Dominic Masters
|
||||
//
|
||||
// MIT License
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
const Forms = require('./../../../common/Forms.js');
|
||||
|
||||
class APIRequest {
|
||||
constructor(handler, req, res) {
|
||||
this.handler = handler;
|
||||
this.req = req;
|
||||
this.res = res;
|
||||
}
|
||||
|
||||
getHandler() {return this.handler;}
|
||||
getRequest() {return this.req;}
|
||||
getResponse() {return this.res;}
|
||||
getHandleFunction() {return this.getHandler().handle;}
|
||||
getFormData(name) {return Forms[name];}
|
||||
|
||||
//Some nice shorthands
|
||||
getAPI() {return this.getHandler().getAPI();}
|
||||
getConfig() {return this.getAPI().getConfig();}
|
||||
getServer() {return this.getAPI().getServer();}
|
||||
getExpress() {return this.getAPI().getExpress();}
|
||||
getEmail() {return this.getAPI().getEmail();}
|
||||
getApp() {return this.getAPI().getApp();}
|
||||
|
||||
//Our process method
|
||||
process() {
|
||||
if(typeof this.processPromise !== typeof undefined) return;//Woops, already processing?
|
||||
this.processPromise = this.processHandler();
|
||||
this.processPromise.then(this.onResult.bind(this)).catch(this.onError.bind(this));
|
||||
}
|
||||
|
||||
async processHandler() {
|
||||
//Awesome, now we have a nice async function!
|
||||
let response = { ok: false };
|
||||
if(typeof this.getHandleFunction() === "function") {
|
||||
try {
|
||||
response = await this.getHandleFunction()(this);
|
||||
} catch(e) {
|
||||
console.error(e);
|
||||
response = { ok: 500, data: "An unknown error occured" };
|
||||
}
|
||||
}
|
||||
|
||||
if(typeof response.data === typeof undefined || typeof response.ok === typeof undefined) {
|
||||
throw new Error("Invalid response object.");
|
||||
}
|
||||
|
||||
if(response.ok !== true) {
|
||||
response.code = typeof response.ok === "number" ? response.ok : 400;
|
||||
}
|
||||
|
||||
this.res.status(response.code || 200).json(response.data);
|
||||
}
|
||||
|
||||
onResult(result) {
|
||||
}
|
||||
|
||||
onError(error) {
|
||||
this.res.status(500).json("An unexpected error occured");
|
||||
}
|
||||
|
||||
//Some really nice API handlers
|
||||
get(key) {
|
||||
if(typeof this.req === typeof undefined) return null;
|
||||
let data = this.req.body || {};
|
||||
if(this.req.method == "GET") {
|
||||
data = this.req.query || {};
|
||||
}
|
||||
if(typeof data === typeof undefined) return null;
|
||||
if(typeof key === typeof undefined) return data;
|
||||
|
||||
return this.getRecursive(key.split("."), data);
|
||||
}
|
||||
|
||||
getRecursive(key_array, data_obj) {
|
||||
if(typeof data_obj === typeof undefined) return null;
|
||||
|
||||
let k = key_array[0];
|
||||
let o = data_obj[k];
|
||||
if(typeof o === typeof undefined) return null;
|
||||
|
||||
//Awesome
|
||||
if(key_array.length > 1) {
|
||||
if(typeof o !== typeof {}) return null;
|
||||
key_array.shift();
|
||||
return this.getRecursive(key_array, o);
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
getInteger(key) {
|
||||
if(!this.hasInteger(key)) throw new Error("Invalid Data Type!");
|
||||
return parseInt(this.get(key));
|
||||
}
|
||||
|
||||
getDouble(key) {
|
||||
if(!this.hasDouble(key)) throw new Error("Invalid Data Type!");
|
||||
return parseFloat(this.get(key));
|
||||
}
|
||||
|
||||
getBool(key) {
|
||||
if(!this.hasBool(key)) throw new Error("Invalid Type");
|
||||
let t = this.get(key);
|
||||
if(t === true || t === "true" || t === 1 || t === "1") return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
getString(key, maxLength, allowBlank) {
|
||||
if(typeof allowBlank === typeof undefined) allowBlank = true;
|
||||
if(!this.hasString(key, maxLength, allowBlank)) throw new Error("Missing Data");
|
||||
return this.get(key)+"";
|
||||
}
|
||||
|
||||
has(key) {
|
||||
if(typeof this.req === typeof undefined) return false;
|
||||
if(typeof this.req === typeof undefined) return false;
|
||||
let data = this.req.body || {};
|
||||
if(this.req.method == "GET") {
|
||||
data = this.req.query || {};
|
||||
}
|
||||
if(typeof data === typeof undefined) return false;
|
||||
if(typeof key === typeof undefined) return data;
|
||||
return this.hasRecursive(key.split("."), data);
|
||||
}
|
||||
|
||||
hasRecursive(key_array, data_obj) {
|
||||
if(typeof data_obj === typeof undefined) return false;
|
||||
|
||||
let k = key_array[0];
|
||||
let o = data_obj[k];
|
||||
if(typeof o === typeof undefined) return false;
|
||||
//Awesome
|
||||
if(key_array.length > 1) {
|
||||
if(typeof o !== typeof {}) return false;
|
||||
key_array.shift();
|
||||
return this.hasRecursive(key_array, o);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
hasInteger(key) {
|
||||
if(!this.has(key)) return false;
|
||||
let t = parseInt(this.get(key));
|
||||
if(typeof t !== "number" || isNaN(t) || !isFinite(t)) return false;
|
||||
let tf = parseFloat(this.get(key));
|
||||
if(tf !== t) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
hasDouble(key) {
|
||||
if(!this.has(key)) return false;
|
||||
let t = parseFloat(this.get(key));
|
||||
return typeof t === typeof 1.00 && !isNaN(t) && isFinite(t);
|
||||
}
|
||||
|
||||
hasBool(bool) {
|
||||
if(!this.has(bool)) return false;
|
||||
let t = this.get(bool);
|
||||
return (
|
||||
t === true || t === false ||
|
||||
t === "true" || t === "false" ||
|
||||
t === 1 || t === 0 ||
|
||||
t === "1" || t === "0"
|
||||
);
|
||||
}
|
||||
|
||||
hasString(str, maxLength, allowBlank) {
|
||||
if(typeof maxLength === typeof undefined) throw new Error("MaxLength check missing.");
|
||||
if(typeof allowBlank === typeof undefined) allowBlank = false;
|
||||
if(!this.has(str)) return false;
|
||||
let t = this.get(str);
|
||||
let v = typeof t === typeof "" && t.length <= maxLength;
|
||||
if(!v) return false;
|
||||
if(!allowBlank) {
|
||||
t = t.replace(/\s/g, "");
|
||||
if(!t.length) return false;
|
||||
}
|
||||
return typeof t === typeof "" && (t.length <= maxLength ? true : false);
|
||||
}
|
||||
|
||||
//Files (if supported)
|
||||
hasFiles() {
|
||||
if(typeof this.req === typeof undefined) return false;
|
||||
if(typeof this.req.files === typeof undefined) return false;
|
||||
if(!this.req || !this.req.files) return false;
|
||||
return Object.keys(this.req.files).length ? true : false;
|
||||
}
|
||||
|
||||
hasFile(name) {
|
||||
if(!this.hasFiles()) return false;
|
||||
if(typeof this.req.files[name] === typeof undefined) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
getFile(name) {
|
||||
if(!this.hasFile(name)) return false;
|
||||
return this.req.files[name];
|
||||
}
|
||||
|
||||
//Headers
|
||||
hasHeader(header) {
|
||||
return this.req && typeof this.req.get(header) !== typeof undefined
|
||||
}
|
||||
|
||||
getHeader(header) {
|
||||
return this.req.get(header);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = APIRequest;
|
101
private/server/api/methods/contact/send.js
Normal file
101
private/server/api/methods/contact/send.js
Normal file
@ -0,0 +1,101 @@
|
||||
// Copyright (c) 2018 Dominic Masters
|
||||
//
|
||||
// MIT License
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
const
|
||||
APIHandler = require('./../../APIHandler'),
|
||||
sanitizeHtml = require('sanitize-html')
|
||||
;
|
||||
|
||||
const ERRORS = {
|
||||
name: "Invalid name",
|
||||
email: "Invalid email",
|
||||
message: "Invalid message",
|
||||
sending: "An unknown error occured"
|
||||
};
|
||||
|
||||
module.exports = class Send extends APIHandler {
|
||||
constructor(api) {
|
||||
super(api, ['GET', 'POST'], '/contact/send');
|
||||
}
|
||||
|
||||
async handle(request) {
|
||||
let form = request.getFormData("contact");
|
||||
let name,email,message;
|
||||
|
||||
if(form.name.required && !request.hasString('name', form.name.maxLength)) return {ok: false, data: ERRORS.name};
|
||||
name = request.getString('name', form.name.maxLength);
|
||||
|
||||
if(form.email.required) {
|
||||
if(!request.hasString('email', form.email.maxLength)) return { ok: false, data: ERRORS.email };
|
||||
email = request.getString('email', form.email.maxLength);
|
||||
if(!form.email.regex.test(email)) return { ok: false, data: ERRORS.email };
|
||||
}
|
||||
|
||||
if(form.message.required && !request.hasString('message', form.message.maxLength)) return {ok: false, data: ERRORS.message};
|
||||
message = request.getString('message', form.name.maxLength);
|
||||
|
||||
//Now let's create our message, we're gonna have to do some rudementry HTML...
|
||||
let textMessage = '';
|
||||
let htmlMessage = '';
|
||||
|
||||
//First the name
|
||||
textMessage += 'Name: ' + name;
|
||||
htmlMessage += '<p><strong>Name: </strong>' + sanitizeHtml(name) + '</p>';
|
||||
|
||||
//Now the response Email
|
||||
textMessage += '\nEmail: ' + email;
|
||||
htmlMessage += '<p><strong>Email: </strong>' + sanitizeHtml(email) + '</p>';
|
||||
|
||||
//Message!
|
||||
textMessage += '\nMessage: ' + message;
|
||||
htmlMessage += '<p><strong>Message:</strong></p>';
|
||||
htmlMessage += '<p>' + sanitizeHtml(message) + '</p>';
|
||||
|
||||
htmlMessage += '<hr />';
|
||||
htmlMessage += '<p>Reply: <a href="mailto:'+email+'">'+email+'</a></p>';
|
||||
|
||||
//Now we can send it!
|
||||
try {
|
||||
await request.getEmail().sendMailClean(
|
||||
request.getEmail().getDestinationEmail(),
|
||||
request.getEmail().getSourceName(),
|
||||
request.getEmail().getSourceEmail(),
|
||||
"domsPlace Contact Enquiry",
|
||||
htmlMessage,
|
||||
textMessage
|
||||
);
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
data: true
|
||||
};
|
||||
} catch(e) {
|
||||
console.error('Failed to send contact message');
|
||||
console.error(e);
|
||||
return {
|
||||
ok: false,
|
||||
data: ERRORS.sending
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user