mirror of
https://github.com/responsively-org/responsively-app
synced 2024-11-14 16:37:27 +00:00
lambda updates
This commit is contained in:
parent
bc9e1878d6
commit
0d6e21c417
15 changed files with 176 additions and 117 deletions
|
@ -1,9 +0,0 @@
|
|||
SERVER_PORT=
|
||||
|
||||
MONGO_USERNAME =
|
||||
MONGO_PASSWORD =
|
||||
MONGO_HOSTNAME =
|
||||
MONGO_PORT =
|
||||
MONGO_DB =
|
||||
|
||||
SENDGRID_API_KEY=
|
|
@ -1,8 +0,0 @@
|
|||
var express=require('express')
|
||||
var db=require('./src/db/db')
|
||||
var subscriptionRouter=require('./src/app/router/subscription.router')
|
||||
const app=express()
|
||||
|
||||
app.use('/subscription', subscriptionRouter)
|
||||
|
||||
const server=app.listen(process.env.SERVER_PORT,function() {});
|
6
server/environment.yml
Normal file
6
server/environment.yml
Normal file
|
@ -0,0 +1,6 @@
|
|||
MONGO_USERNAME:
|
||||
MONGO_PASSWORD:
|
||||
MONGO_HOSTNAME:
|
||||
MONGO_PORT:
|
||||
MONGO_DB:
|
||||
SENDGRID_API_KEY:
|
|
@ -11,8 +11,6 @@
|
|||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@sendgrid/mail": "^6.4.0",
|
||||
"dotenv": "^8.1.0",
|
||||
"express": "^4.17.1",
|
||||
"mongoose": "^5.6.13",
|
||||
"serverless": "^1.51.0",
|
||||
"serverless-http": "^2.3.0"
|
||||
|
@ -24,4 +22,4 @@
|
|||
"serverless-webpack": "^5.3.1",
|
||||
"webpack": "^4.39.3"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,5 +1,3 @@
|
|||
org: manojvivek
|
||||
app: responsively
|
||||
service: responsively-server
|
||||
|
||||
custom:
|
||||
|
@ -10,11 +8,28 @@ provider:
|
|||
name: aws
|
||||
runtime: nodejs10.x
|
||||
websocketsApiName: responsively-websocket
|
||||
apiName: dev-responsively-server
|
||||
websocketsApiRouteSelectionExpression: $request.body.action # custom routes are selected by the value of the action property in the body
|
||||
environment: ${file(environment.yml)}
|
||||
|
||||
functions:
|
||||
activateTrial:
|
||||
handler: src/http/lambda.activateTrial
|
||||
timeout: 15
|
||||
events:
|
||||
- http:
|
||||
path: activate-trial
|
||||
method: get
|
||||
validateLicenseHttp:
|
||||
handler: src/http/lambda.validateLicense
|
||||
timeout: 15
|
||||
events:
|
||||
- http:
|
||||
path: validate-license
|
||||
method: get
|
||||
websocket-connection-manager:
|
||||
handler: src/websocket/lambda.connectionHandler
|
||||
timeout: 15
|
||||
events:
|
||||
- websocket:
|
||||
route: $connect
|
||||
|
@ -22,13 +37,21 @@ functions:
|
|||
route: $disconnect
|
||||
defaultHandler:
|
||||
handler: src/websocket/lambda.defaultHandler
|
||||
timeout: 15
|
||||
events:
|
||||
- websocket: $default #simple event definition without extra route property
|
||||
pingHandler:
|
||||
handler: src/websocket/lambda.pingHandler
|
||||
timeout: 15
|
||||
events:
|
||||
- websocket:
|
||||
route: ping # will trigger if $request.body.action === "ping"
|
||||
validateLicense:
|
||||
handler: src/websocket/lambda.validateLicense
|
||||
timeout: 15
|
||||
events:
|
||||
- websocket:
|
||||
route: validate # will trigger if $request.body.action === "validate"
|
||||
|
||||
plugins:
|
||||
- serverless-webpack
|
||||
- serverless-webpack
|
|
@ -3,7 +3,7 @@ const webpack = require('webpack');
|
|||
|
||||
process.env.NODE_ENV = slsw.lib.options.stage || 'development';
|
||||
const mode =
|
||||
process.env.NODE_ENV === 'development' ? 'development' : 'production';
|
||||
process.env.NODE_ENV === 'development' ? 'development' : 'production';
|
||||
|
||||
module.exports = {
|
||||
entry: slsw.lib.entries,
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
var http = require('http');
|
||||
var url= require('url');
|
||||
|
||||
|
||||
http.createServer(function (req, res) {
|
||||
var queryParams=url.parse(req.url,true).query;
|
||||
res.write('Hello '+queryParams.email); //write a response to the client
|
||||
res.end(); //end the response
|
||||
}).listen(8080);
|
||||
|
||||
function createLicenseKey(userEmail){
|
||||
|
||||
|
||||
}
|
||||
|
||||
checkUserExists(String email){
|
||||
|
||||
}
|
|
@ -1,53 +0,0 @@
|
|||
const express = require('express');
|
||||
const userService = require('../service/user.service')
|
||||
const UserExistsError=require('../exception/user-exists-error.exception')
|
||||
const InvalidEmailError=require('../exception/invalid-email-error.exception')
|
||||
const InvalidLicenseError=require('../exception/invalid-license-error.exception')
|
||||
|
||||
const router = express.Router();
|
||||
router.get('/activate-trial', async function (req, res) {
|
||||
const email=req.query.email
|
||||
let responseBody={}
|
||||
try{
|
||||
await userService.createUserAndEnableTrial(email)
|
||||
responseBody.status=true
|
||||
responseBody.message='success'
|
||||
}catch(err){
|
||||
console.log(err)
|
||||
responseBody.status=false
|
||||
if(err instanceof UserExistsError){
|
||||
responseBody.errorType=409
|
||||
responseBody.message='user already activated'
|
||||
}else if(err instanceof InvalidEmailError){
|
||||
responseBody.errorType=400
|
||||
responseBody.message='invalid email'
|
||||
}else{
|
||||
responseBody.errorType=500
|
||||
responseBody.message='Internal Server Error'
|
||||
}
|
||||
}
|
||||
res.send(responseBody)
|
||||
})
|
||||
|
||||
router.get('/verify-license', async function (req, res) {
|
||||
const licenseKey=req.query.licenseKey
|
||||
let responseBody={}
|
||||
try{
|
||||
await userService.verifyLicenseKey(licenseKey)
|
||||
responseBody.status=true
|
||||
responseBody.message='success'
|
||||
}catch(err){
|
||||
console.log(err)
|
||||
responseBody.status=false
|
||||
if(err instanceof InvalidLicenseError){
|
||||
responseBody.errorType=403
|
||||
responseBody.message='Invalid License'
|
||||
}else{
|
||||
responseBody.errorType=500
|
||||
responseBody.message='Internal Server Error'
|
||||
}
|
||||
}
|
||||
res.send(responseBody)
|
||||
})
|
||||
|
||||
module.exports = router;
|
|
@ -3,7 +3,7 @@ const emailUtils=require('../utils/email.utils')
|
|||
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
|
||||
|
||||
const SYSTEM_MAIL='noreply@responsively.app'
|
||||
function sendLicenseKeyMail(email,licenseKey){
|
||||
async function sendLicenseKeyMail(email,licenseKey){
|
||||
console.log('sending license key to email:'+email)
|
||||
const msg = {
|
||||
to: email,
|
||||
|
@ -11,7 +11,7 @@ function sendLicenseKeyMail(email,licenseKey){
|
|||
subject: 'Responsively - License Key',
|
||||
html: 'Please find your license key - <strong>'+licenseKey+'</strong>',
|
||||
};
|
||||
sgMail.send(msg);
|
||||
await sgMail.send(msg);
|
||||
}
|
||||
|
||||
function validateEmailExistence(email){
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
const User=require('../model/user.model')
|
||||
const db=require('../../db/db')
|
||||
const subscriptionUtils=require('../utils/subscription.utils')
|
||||
const constants=require('../constants/constants')
|
||||
const crypto=require('crypto')
|
||||
|
@ -7,6 +6,7 @@ const emailService=require('../service/email.service')
|
|||
const InvalidEmailError=require('../exception/invalid-email-error.exception')
|
||||
const UserExistsError=require('../exception/user-exists-error.exception')
|
||||
const InvalidLicenseError=require('../exception/invalid-license-error.exception')
|
||||
|
||||
async function createUserAndEnableTrial(email){
|
||||
|
||||
if(!emailService.validateEmailExistence(email)){
|
||||
|
@ -19,7 +19,7 @@ async function createUserAndEnableTrial(email){
|
|||
})
|
||||
|
||||
await insertUser(newUser)
|
||||
emailService.sendLicenseKeyMail(email,newUser.license_key)
|
||||
await emailService.sendLicenseKeyMail(email,newUser.license_key)
|
||||
}
|
||||
|
||||
async function insertUser(newUser){
|
||||
|
@ -34,25 +34,31 @@ async function insertUser(newUser){
|
|||
}
|
||||
throw err
|
||||
}
|
||||
console.log('userInserted')
|
||||
return newUser
|
||||
}
|
||||
|
||||
async function verifyLicenseKey(licenseKey){
|
||||
async function validateLicenseKey(licenseKey){
|
||||
|
||||
if(!licenseKey){
|
||||
throw new InvalidLicenseError('Invalid License :'+licenseKey)
|
||||
}
|
||||
let user=await User.findOne({license_key: licenseKey}).exec()
|
||||
if(!user){
|
||||
throw new InvalidLicenseError('Invalid License:'+licenseKey)
|
||||
try{
|
||||
let user=await User.findOne({license_key: licenseKey}).exec()
|
||||
if(!user){
|
||||
throw new InvalidLicenseError('Invalid License:'+licenseKey)
|
||||
}
|
||||
let licenseValid=user.subscription.valid_till.getTime()-new Date().getTime()
|
||||
if(licenseValid<0){
|
||||
console.log('license expired!');
|
||||
return false
|
||||
}
|
||||
console.log('license active!');
|
||||
return true
|
||||
}catch(err){
|
||||
throw err
|
||||
}
|
||||
console.log(user)
|
||||
let licenseValid=user.subscription.valid_till.getTime()-new Date().getTime()
|
||||
if(licenseValid<0){
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
function getLicenseKey(email){
|
||||
const key=crypto.randomBytes(20).toString('hex');
|
||||
|
@ -61,5 +67,5 @@ function getLicenseKey(email){
|
|||
|
||||
module.exports={
|
||||
createUserAndEnableTrial,
|
||||
verifyLicenseKey
|
||||
validateLicenseKey
|
||||
}
|
|
@ -1,15 +1,12 @@
|
|||
const mongoose = require('mongoose');
|
||||
|
||||
const MONGO_USERNAME = process.env.MONGO_USERNAME
|
||||
const MONGO_PASSWORD = process.env.MONGO_PASSWORD
|
||||
const MONGO_HOSTNAME = process.env.MONGO_HOSTNAME
|
||||
const MONGO_PORT = process.env.MONGO_PORT
|
||||
const MONGO_DB = process.env.MONGO_DB
|
||||
const url = `mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_HOSTNAME}:${MONGO_PORT}/${MONGO_DB}`
|
||||
|
||||
const url = `mongodb+srv://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_HOSTNAME}/${MONGO_DB}`
|
||||
console.log('connecting to db');
|
||||
mongoose.connect(url,{ useNewUrlParser: true }, function(error){
|
||||
if(error) console.log(error);
|
||||
console.log("connection successful");
|
||||
if(error){console.log(error)}
|
||||
else{console.log("connection successful")}
|
||||
})
|
||||
console.log('connected');
|
||||
|
|
76
server/src/http/lambda.js
Normal file
76
server/src/http/lambda.js
Normal file
|
@ -0,0 +1,76 @@
|
|||
const db=require('../db/db')
|
||||
const userService = require('../app/service/user.service')
|
||||
const UserExistsError=require('../app/exception/user-exists-error.exception')
|
||||
const InvalidLicenseError=require('../app/exception/invalid-license-error.exception')
|
||||
const InvalidEmailError=require('../app/exception/invalid-email-error.exception')
|
||||
|
||||
export async function activateTrial(event, context, callback) {
|
||||
let responseBody={}
|
||||
let statusCode=0
|
||||
context.callbackWaitsForEmptyEventLoop = false;
|
||||
try{
|
||||
const email=event["queryStringParameters"]['email']
|
||||
if(!email){
|
||||
throw new InvalidEmailError('email is empty')
|
||||
}
|
||||
await userService.createUserAndEnableTrial(email)
|
||||
responseBody.status=true
|
||||
responseBody.statusCode=200;
|
||||
responseBody.message='success'
|
||||
statusCode=200
|
||||
}catch(err){
|
||||
console.log(err)
|
||||
responseBody.status=false
|
||||
if(err instanceof UserExistsError){
|
||||
responseBody.statusCode=409
|
||||
responseBody.message='user already activated'
|
||||
}else if(err instanceof InvalidEmailError){
|
||||
responseBody.statusCode=400
|
||||
responseBody.message='invalid email'
|
||||
}else{
|
||||
responseBody.statusCode=500
|
||||
responseBody.message='Internal Server Error'
|
||||
}
|
||||
}
|
||||
let response= {
|
||||
statusCode: 200,
|
||||
body: JSON.stringify(responseBody)
|
||||
}
|
||||
callback(null, response)
|
||||
}
|
||||
|
||||
export async function validateLicense(event, context, callback) {
|
||||
let responseBody={}
|
||||
let statusCode=0
|
||||
context.callbackWaitsForEmptyEventLoop = false;
|
||||
console.log(event)
|
||||
try{
|
||||
const licenseKey=event["queryStringParameters"]['licenseKey']
|
||||
if(!licenseKey){
|
||||
throw new InvalidLicenseError('licenseKey is empty')
|
||||
}
|
||||
responseBody.status=await userService.validateLicenseKey(licenseKey)
|
||||
responseBody.statusCode=200
|
||||
if(responseBody.status){
|
||||
responseBody.message='success'
|
||||
}else{
|
||||
responseBody.message='license expired'
|
||||
}
|
||||
statusCode=200
|
||||
}catch(err){
|
||||
console.log(err)
|
||||
responseBody.status=false
|
||||
if(err instanceof InvalidLicenseError){
|
||||
responseBody.statusCode=403
|
||||
responseBody.message='Invalid License'
|
||||
}else{
|
||||
responseBody.statusCode=500
|
||||
responseBody.message='Internal Server Error'
|
||||
}
|
||||
}
|
||||
let response= {
|
||||
statusCode: 200,
|
||||
body: JSON.stringify(responseBody)
|
||||
}
|
||||
callback(null, response)
|
||||
}
|
|
@ -1,3 +1,7 @@
|
|||
var db=require('../db/db')
|
||||
const userService = require('../app/service/user.service')
|
||||
const InvalidLicenseError=require('../app/exception/invalid-license-error.exception')
|
||||
|
||||
export async function connectionHandler(event, context, callback) {
|
||||
return {
|
||||
statusCode: 200,
|
||||
|
@ -18,3 +22,40 @@ export async function pingHandler(event, context, callback) {
|
|||
body: 'Message recieved',
|
||||
};
|
||||
}
|
||||
|
||||
export async function validateLicense(event, context, callback) {
|
||||
let responseBody={}
|
||||
let statusCode=0
|
||||
context.callbackWaitsForEmptyEventLoop = false;
|
||||
console.log(event)
|
||||
try{
|
||||
const body=JSON.parse(event.body)
|
||||
if(!body['data'] || !body['data']['licenseKey']){
|
||||
throw new InvalidLicenseError('licenseKey is empty')
|
||||
}
|
||||
const licenseKey=body['data']['licenseKey']
|
||||
responseBody.status=await userService.validateLicenseKey(licenseKey)
|
||||
responseBody.statusCode=200
|
||||
if(responseBody.status){
|
||||
responseBody.message='success'
|
||||
}else{
|
||||
responseBody.message='license expired'
|
||||
}
|
||||
statusCode=200
|
||||
}catch(err){
|
||||
console.log(err)
|
||||
responseBody.status=false
|
||||
if(err instanceof InvalidLicenseError){
|
||||
responseBody.statusCode=403
|
||||
responseBody.message='Invalid License'
|
||||
}else{
|
||||
responseBody.statusCode=500
|
||||
responseBody.message='Internal Server Error'
|
||||
}
|
||||
}
|
||||
let response= {
|
||||
statusCode: 200,
|
||||
body: JSON.stringify(responseBody)
|
||||
}
|
||||
callback(null, response)
|
||||
}
|
Loading…
Reference in a new issue