feat: Split v1 and v2 apis
This commit is contained in:
68
v2/server/src/SysS/Connections/index.js
Normal file
68
v2/server/src/SysS/Connections/index.js
Normal file
@@ -0,0 +1,68 @@
|
||||
'use strict';
|
||||
const apiConfig = require( '../../../config/apiConfig.json' );
|
||||
const Knex = require('knex');
|
||||
const { Model } = require('objection');
|
||||
|
||||
const UNINIT = 0;
|
||||
const INIT = 1;
|
||||
const ONLINE = 2;
|
||||
const OFFLINE = 3;
|
||||
|
||||
class SystemServices {
|
||||
constructor(){
|
||||
this.SystemServiceState = UNINIT;
|
||||
}
|
||||
|
||||
async setup(){
|
||||
this.SystemServiceState = UNINIT;
|
||||
}
|
||||
|
||||
async init(){
|
||||
this.SystemServiceState = INIT;
|
||||
}
|
||||
|
||||
async connect(){
|
||||
const knex = Knex({
|
||||
client: 'mysql',
|
||||
useNullAsDefault: true,
|
||||
connection: {
|
||||
host: apiConfig.sql.host,
|
||||
port: apiConfig.sql.port,
|
||||
user: apiConfig.sql.user,
|
||||
password: apiConfig.sql.password,
|
||||
database: apiConfig.sql.database,
|
||||
}
|
||||
});
|
||||
Model.knex(knex);
|
||||
this.knex = knex;
|
||||
console.log("Connected to SQL");
|
||||
this.SystemServiceState = ONLINE;
|
||||
}
|
||||
|
||||
async disconnect(){
|
||||
this.knex.destroy();
|
||||
|
||||
this.SystemServiceState = OFFLINE;
|
||||
}
|
||||
|
||||
async deinit(){
|
||||
this.SystemServiceState = UNINIT;
|
||||
}
|
||||
|
||||
async getState(){
|
||||
switch( this.SystemServiceState ){
|
||||
case UNINIT:
|
||||
return "UNINIT";
|
||||
case INIT:
|
||||
return "INIT";
|
||||
case ONLINE:
|
||||
return "ONLINE";
|
||||
case OFFLINE:
|
||||
return "OFFLINE";
|
||||
default:
|
||||
return "UNINIT";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new SystemServices();
|
||||
132
v2/server/src/SysS/Controller/index.js
Normal file
132
v2/server/src/SysS/Controller/index.js
Normal file
@@ -0,0 +1,132 @@
|
||||
'use strict';
|
||||
/**
|
||||
* ExpressJS Controller
|
||||
*/
|
||||
require('dotenv').config();
|
||||
const express = require('express');
|
||||
const cors = require('cors');
|
||||
const compression = require('compression');
|
||||
const morgan = require('morgan');
|
||||
const helmet = require('helmet');
|
||||
const bodyParser = require('body-parser');
|
||||
const fileUpload = require('express-fileupload');
|
||||
|
||||
/// Import Applications to serve
|
||||
const AppsController = require('../../Controller');
|
||||
const middlewares = require('./middlewares');
|
||||
|
||||
const UNINIT = 0;
|
||||
const INIT = 1;
|
||||
const ONLINE = 2;
|
||||
const OFFLINE = 3;
|
||||
|
||||
class ExpressJSServices {
|
||||
constructor(){
|
||||
this.SystemServiceState = UNINIT;
|
||||
this.serverPort = process.env.SERVER_PORT || 3000;
|
||||
this.app = express();
|
||||
}
|
||||
|
||||
async setup(){
|
||||
const app = this.app;
|
||||
|
||||
app.use( middlewares.Auth );
|
||||
|
||||
app.use(
|
||||
fileUpload({
|
||||
limits: { fileSize: 4 * 1024 * 1024 },
|
||||
abortOnLimit: true,
|
||||
limitHandler: (req,res,next) => {
|
||||
req.limitSize = true;
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
app.use((req, res, next) => {
|
||||
if (req.limitSize) {
|
||||
res.status(413).send({message:"File size limit has been reached",status:"PAYLOAD_TOO_LARGE"});
|
||||
}else{
|
||||
next()
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
app.use(bodyParser.urlencoded({ extended: true, limit: '50mb' }));
|
||||
app.use(bodyParser.json({ limit: '50mb' }));
|
||||
app.use(morgan('dev'));
|
||||
|
||||
app.use(helmet({
|
||||
crossOriginResourcePolicy: false
|
||||
}));
|
||||
|
||||
app.use(compression());
|
||||
|
||||
app.use(cors({
|
||||
origin: '*',
|
||||
methods: [
|
||||
'GET',
|
||||
'POST',
|
||||
'PATCH',
|
||||
'PUT',
|
||||
'DELETE'
|
||||
],
|
||||
allowedHeaders: ['Content-Type', 'Authorization']
|
||||
}));
|
||||
|
||||
this.SystemServiceState = UNINIT;
|
||||
}
|
||||
|
||||
async init(){
|
||||
const app = this.app;
|
||||
|
||||
app.use( middlewares.errorJSON );
|
||||
app.use( AppsController );
|
||||
app.use( middlewares.error404 );
|
||||
|
||||
this.SystemServiceState = INIT;
|
||||
}
|
||||
|
||||
async connect(){
|
||||
const app = this.app;
|
||||
const serverPort = this.serverPort;
|
||||
|
||||
const server = app.listen( serverPort , function(err){
|
||||
if( !err ){
|
||||
console.log('API listen on port', serverPort );
|
||||
}else{
|
||||
console.log( err );
|
||||
}
|
||||
});
|
||||
|
||||
this.server = server;
|
||||
|
||||
this.SystemServiceState = ONLINE;
|
||||
}
|
||||
|
||||
async disconnect(){
|
||||
this.server.close();
|
||||
|
||||
this.SystemServiceState = OFFLINE;
|
||||
}
|
||||
|
||||
async deinit(){
|
||||
this.SystemServiceState = UNINIT;
|
||||
}
|
||||
|
||||
async getState(){
|
||||
switch( this.SystemServiceState ){
|
||||
case UNINIT:
|
||||
return "UNINIT";
|
||||
case INIT:
|
||||
return "INIT";
|
||||
case ONLINE:
|
||||
return "ONLINE";
|
||||
case OFFLINE:
|
||||
return "OFFLINE";
|
||||
default:
|
||||
return "UNINIT";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new ExpressJSServices();
|
||||
87
v2/server/src/SysS/Controller/middlewares.js
Normal file
87
v2/server/src/SysS/Controller/middlewares.js
Normal file
@@ -0,0 +1,87 @@
|
||||
'use strict';
|
||||
/**
|
||||
* HASH
|
||||
*****************************************************
|
||||
* DEPENDENCIES
|
||||
*****************************************************
|
||||
* Based on Express Framework
|
||||
* System
|
||||
*****************************************************
|
||||
* PUBLIC METHODS
|
||||
*****************************************************
|
||||
* Auth( req, res, next)
|
||||
* Extract JWT or BasicAuth data
|
||||
* errorJSON( error , request , response , next )
|
||||
* Generate error response on bad JSON format
|
||||
* error404( request , response , next )
|
||||
* Generate error 404 response
|
||||
* apiKey( request , response , next )
|
||||
* Generate error on invalid apikey
|
||||
**/
|
||||
|
||||
/// Extract JWT or BasicAuth
|
||||
function Auth( req, res , next ){
|
||||
///
|
||||
/// Try to extract the authorization data from headers
|
||||
///
|
||||
let auth;
|
||||
if( req.headers.hasOwnProperty( "authorization" ) ){
|
||||
auth = req.headers.authorization;
|
||||
auth = auth.split(" ")[1];
|
||||
if( !auth ){ console.log( "NO HEADER AUTH available" ); return next(); }
|
||||
//console.log( auth );
|
||||
/// Try BasicAuth {
|
||||
try{
|
||||
let ba = Buffer.from( auth , 'base64' ).toString()
|
||||
//const [user,pass] = ba.split(':');
|
||||
ba = ba.split(':');
|
||||
if( ba.length == 2 ){
|
||||
req.basicAuth = { user : ba[0] , password : ba[1] };
|
||||
}
|
||||
}catch(error){
|
||||
console.log("MIDDLEWARE_AUTH_ERR_BA",error);
|
||||
}
|
||||
/// Try BasicAuth }
|
||||
}else if( req.query.access_token ){
|
||||
auth = req.query.access_token;
|
||||
if( !auth ){ console.log( "NO QUERY AUTH available" ); return next(); }
|
||||
}
|
||||
if( auth ){
|
||||
/// Try JWT {
|
||||
try{
|
||||
let jwt = auth.split(".");
|
||||
if( jwt.length == 3 ){
|
||||
req.JWT = {};
|
||||
req.JWT.raw = auth;
|
||||
}
|
||||
}catch( error ){
|
||||
console.log("MIDDLEWARE_AUTH_ERR_JWT",error);
|
||||
}
|
||||
/// Try JWT }
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
function errorJSON( error , request , response , next ){
|
||||
console.log(error);
|
||||
if( error !== null ){
|
||||
/// For body-parser errors
|
||||
if( error instanceof SyntaxError && error.status === 400 && 'body' in error ){
|
||||
return response.status(400).json({ error : 'Invalid json' , code : 400 });
|
||||
}
|
||||
/// For any error
|
||||
return response.status(500).send( { error: "Internal server error" , code : 500 } );
|
||||
}else{
|
||||
return next();
|
||||
}
|
||||
}
|
||||
|
||||
function error404( request , response , next ){
|
||||
return response.status(404).send( { error : "Page not found", code : 404 } );
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
Auth,
|
||||
errorJSON,
|
||||
error404,
|
||||
};
|
||||
@@ -0,0 +1,84 @@
|
||||
'user strict';
|
||||
const apiConfig = require( '../../../../config/apiConfig.json' );
|
||||
const nodemailer = require("nodemailer");
|
||||
const SendGrid = require("nodemailer-sendgrid");
|
||||
|
||||
const SiteName = "ETA Viaporte";
|
||||
|
||||
const sendgridConfig = apiConfig.sendgrid;
|
||||
|
||||
const transporter = nodemailer.createTransport(
|
||||
SendGrid({
|
||||
host: sendgridConfig.HOST,
|
||||
apiKey: sendgridConfig.API_KEY
|
||||
})
|
||||
);
|
||||
|
||||
async function sendMailTemplate( templateId, receiver, subject, content ){
|
||||
/**TODO: Remove in production */
|
||||
const default_mail_list = [
|
||||
{pattern:"testing@etaviaporte.com",redirect:"testing@etaviaporte.com"},
|
||||
{pattern:"alex@etaviaporte.com",redirect:"alexandro_uribe@outlook.com"},
|
||||
{pattern:"pablo@etaviaporte.com",redirect:"josepablo134@gmail.com"}
|
||||
];
|
||||
for( let i=0; i< default_mail_list.length; i++ ){
|
||||
if( receiver.indexOf( default_mail_list[i].pattern ) >= 0 ){
|
||||
receiver = default_mail_list[i].redirect;
|
||||
break;/** Set only the first match */
|
||||
}
|
||||
}
|
||||
return await transporter.sendMail({
|
||||
from: sendgridConfig.FROM,
|
||||
to: receiver,
|
||||
subject: subject,
|
||||
templateId: templateId,
|
||||
dynamic_template_data: content
|
||||
});
|
||||
}
|
||||
|
||||
async function AccountVerifyEmail( receiver , content ){
|
||||
const templateId = "d-e9b7966303694964a64b6e4954e9715d";
|
||||
const subject = "[ETA] Account Verification";
|
||||
const content_to_send = {
|
||||
project_name: SiteName,
|
||||
user_name: content.user_name,
|
||||
user_email: receiver,
|
||||
OTP : content.OTP
|
||||
};
|
||||
return await sendMailTemplate( templateId, receiver, subject, content_to_send );
|
||||
}
|
||||
|
||||
async function AccountConfirmed( receiver, content ){
|
||||
const templateId = "d-4daaab1b85d443ceba38826f606e9931";
|
||||
const subject = "[ETA] Welcome to ETA";
|
||||
const content_to_send = {
|
||||
user_name: content.user_name,
|
||||
};
|
||||
return await sendMailTemplate( templateId, receiver, subject, content_to_send );
|
||||
}
|
||||
|
||||
async function AccountPwdResetEmail( receiver, content ){
|
||||
const templateId = "d-e9b7966303694964a64b6e4954e9715d";
|
||||
const subject = "[ETA] Password Reset";
|
||||
const content_to_send = {
|
||||
project_name: SiteName,
|
||||
user_name: content.user_name,
|
||||
user_email: receiver,
|
||||
OTP : content.OTP
|
||||
};
|
||||
return await sendMailTemplate( templateId, receiver, subject, content_to_send );
|
||||
}
|
||||
|
||||
async function ContactEmail( receiver, content ){
|
||||
const templateId = "d-1090dda1091442f3a75ee8ab39ad0f10";
|
||||
const subject = "[ETA] Contact Email";
|
||||
const content_to_send = {
|
||||
project_name: SiteName,
|
||||
user_name: content.name,
|
||||
user_email: receiver
|
||||
};
|
||||
return await sendMailTemplate( templateId, receiver, subject, content_to_send );
|
||||
}
|
||||
//ContactEmail( "josepablo134@gmail.com", { email : "josepablo134@gmail.com", name:"Josepablo C.", message: "This is an example" } ).then().catch();
|
||||
|
||||
module.exports = { AccountVerifyEmail, AccountConfirmed, AccountPwdResetEmail, ContactEmail };
|
||||
@@ -0,0 +1,22 @@
|
||||
'user strict';
|
||||
const nodemailer = require("nodemailer");
|
||||
const apiConfig = require( '../../../../config/apiConfig.json' );
|
||||
|
||||
const transporter = nodemailer.createTransport(
|
||||
apiConfig.email_standalone
|
||||
);
|
||||
|
||||
async function StandAloneContactEmail( content ){
|
||||
const default_from = apiConfig.email_standalone.auth.user;
|
||||
const receiver = "support@etaviaporte.com";
|
||||
const {name, email, message } = content;
|
||||
return await transporter.sendMail({
|
||||
from: `${name} <${default_from}>`,
|
||||
to: receiver,
|
||||
subject: "Contact Email From Landing Page",
|
||||
text: `\n\n The following is an email from : ${email}\n\n\n` + message
|
||||
});
|
||||
}
|
||||
//StandAloneContactEmail( { email : "josepablo134@gmail.com", name:"Josepablo C.", message: "This is an example" } ).then().catch();
|
||||
|
||||
module.exports = { StandAloneContactEmail };
|
||||
36
v2/server/src/SysS/EventManager/EmailEvents/index.js
Normal file
36
v2/server/src/SysS/EventManager/EmailEvents/index.js
Normal file
@@ -0,0 +1,36 @@
|
||||
'use strict';
|
||||
|
||||
const { StandAloneContactEmail } = require('./StandAlone.handler');
|
||||
const { AccountVerifyEmail, AccountConfirmed, AccountPwdResetEmail, ContactEmail } = require('./SendGrid.handler');
|
||||
|
||||
async function onChecksumGeneration( data ){
|
||||
console.log( data );
|
||||
const receiver = data.email;
|
||||
await AccountVerifyEmail( receiver, data );
|
||||
}
|
||||
|
||||
async function onAccountConfirmed( data ){
|
||||
const receiver = data.email;
|
||||
await AccountConfirmed( receiver, data );
|
||||
}
|
||||
|
||||
async function onPasswordReset( data ){
|
||||
const receiver = data.email;
|
||||
await AccountPwdResetEmail( receiver, data );
|
||||
}
|
||||
|
||||
async function onContactFromWebPage( data ){
|
||||
const receiver = data.email;
|
||||
await StandAloneContactEmail( data );
|
||||
await ContactEmail( receiver, data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Dictionary of event ids and handlers
|
||||
*/
|
||||
module.exports = {
|
||||
"App:Account:getchecksum:signup" : onChecksumGeneration,
|
||||
"App:Account:signupconfirmed":onAccountConfirmed,
|
||||
"App:Account:getchecksum:recover":onPasswordReset,
|
||||
"App:ContactEmail:contact":onContactFromWebPage,
|
||||
};
|
||||
71
v2/server/src/SysS/EventManager/index.js
Normal file
71
v2/server/src/SysS/EventManager/index.js
Normal file
@@ -0,0 +1,71 @@
|
||||
'use strict';
|
||||
const events = require('events');
|
||||
const SharedResources = require('../../Shared/Resources');
|
||||
|
||||
const resources_list = require('./resources');
|
||||
|
||||
const UNINIT = 0;
|
||||
const INIT = 1;
|
||||
const ONLINE = 2;
|
||||
const OFFLINE = 3;
|
||||
|
||||
class SystemServices {
|
||||
constructor(){
|
||||
this.resources = resources_list;
|
||||
this.eventEmitter = new events.EventEmitter();
|
||||
|
||||
this.SystemServiceState = UNINIT;
|
||||
}
|
||||
|
||||
addEvent( event_id , callback ){
|
||||
this.eventEmitter.on( event_id , callback );
|
||||
}
|
||||
|
||||
publishEvent( event_id , data ){
|
||||
this.eventEmitter.emit( event_id , data );
|
||||
}
|
||||
|
||||
async setup(){
|
||||
for ( const resource of this.resources ){
|
||||
for ( const [key, value] of Object.entries( resource ) ) {
|
||||
this.eventEmitter.on( key , value );
|
||||
}
|
||||
}
|
||||
|
||||
this.SystemServiceState = UNINIT;
|
||||
}
|
||||
|
||||
async init(){
|
||||
SharedResources.set( "SysS:EventManager" , this );
|
||||
this.SystemServiceState = INIT;
|
||||
}
|
||||
|
||||
async connect(){
|
||||
this.SystemServiceState = ONLINE;
|
||||
}
|
||||
|
||||
async disconnect(){
|
||||
this.SystemServiceState = OFFLINE;
|
||||
}
|
||||
|
||||
async deinit(){
|
||||
this.SystemServiceState = UNINIT;
|
||||
}
|
||||
|
||||
async getState(){
|
||||
switch( this.SystemServiceState ){
|
||||
case UNINIT:
|
||||
return "UNINIT";
|
||||
case INIT:
|
||||
return "INIT";
|
||||
case ONLINE:
|
||||
return "ONLINE";
|
||||
case OFFLINE:
|
||||
return "OFFLINE";
|
||||
default:
|
||||
return "UNINIT";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new SystemServices();
|
||||
7
v2/server/src/SysS/EventManager/resources.js
Normal file
7
v2/server/src/SysS/EventManager/resources.js
Normal file
@@ -0,0 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const EmailEvents = require('./EmailEvents');
|
||||
|
||||
module.exports = [
|
||||
EmailEvents,
|
||||
];
|
||||
33
v2/server/src/SysS/Template/index.js
Normal file
33
v2/server/src/SysS/Template/index.js
Normal file
@@ -0,0 +1,33 @@
|
||||
'use strict';
|
||||
const UNINIT = 0;
|
||||
const INIT = 1;
|
||||
const ONLINE = 2;
|
||||
const OFFLINE = 3;
|
||||
|
||||
class SystemServices {
|
||||
constructor(){
|
||||
this.SystemServiceState = UNINIT;
|
||||
}
|
||||
|
||||
async setup(){
|
||||
this.SystemServiceState = UNINIT;
|
||||
}
|
||||
|
||||
async init(){
|
||||
this.SystemServiceState = INIT;
|
||||
}
|
||||
|
||||
async connect(){
|
||||
this.SystemServiceState = ONLINE;
|
||||
}
|
||||
|
||||
async disconnect(){
|
||||
this.SystemServiceState = OFFLINE;
|
||||
}
|
||||
|
||||
async deinit(){
|
||||
this.SystemServiceState = UNINIT;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new SystemServices();
|
||||
78
v2/server/src/SysS/index.js
Normal file
78
v2/server/src/SysS/index.js
Normal file
@@ -0,0 +1,78 @@
|
||||
'use strict';
|
||||
const EventManager = require('./EventManager');
|
||||
const Connections = require('./Connections');
|
||||
const Controller = require('./Controller');
|
||||
|
||||
const UNINIT = 0;
|
||||
const INIT = 1;
|
||||
const ONLINE = 2;
|
||||
const OFFLINE = 3;
|
||||
|
||||
class SystemServices {
|
||||
constructor(){
|
||||
this.resources = [
|
||||
EventManager,
|
||||
Connections,
|
||||
Controller,
|
||||
];
|
||||
|
||||
this.SystemServiceState = UNINIT;
|
||||
}
|
||||
|
||||
async setup(){
|
||||
for ( const service of this.resources ){
|
||||
await service.setup();
|
||||
}
|
||||
|
||||
this.SystemServiceState = UNINIT;
|
||||
}
|
||||
|
||||
async init(){
|
||||
for ( const service of this.resources ){
|
||||
await service.init();
|
||||
}
|
||||
|
||||
this.SystemServiceState = INIT;
|
||||
}
|
||||
|
||||
async connect(){
|
||||
for ( const service of this.resources ){
|
||||
await service.connect();
|
||||
}
|
||||
|
||||
this.SystemServiceState = ONLINE;
|
||||
}
|
||||
|
||||
async disconnect(){
|
||||
for ( const service of this.resources ){
|
||||
await service.disconnect();
|
||||
}
|
||||
|
||||
this.SystemServiceState = OFFLINE;
|
||||
}
|
||||
|
||||
async deinit(){
|
||||
for ( const service of this.resources ){
|
||||
await service.deinit();
|
||||
}
|
||||
|
||||
this.SystemServiceState = UNINIT;
|
||||
}
|
||||
|
||||
async getState(){
|
||||
switch( this.SystemServiceState ){
|
||||
case UNINIT:
|
||||
return "UNINIT";
|
||||
case INIT:
|
||||
return "INIT";
|
||||
case ONLINE:
|
||||
return "ONLINE";
|
||||
case OFFLINE:
|
||||
return "OFFLINE";
|
||||
default:
|
||||
return "UNINIT";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new SystemServices();
|
||||
Reference in New Issue
Block a user