123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- const express = require('express')
- const app = express()
- app.use(express.json())
- const port = 3103
- var chalk = require("chalk");
- var uniqid = require('uniqid');
- var db = require("kairoscope-db-models");
- var helpers = require('./helpers');
- helpers.check_env_variables();
- db.start().then(() => {
- app.get('/', (req, res) => {
- res.send("Mail Delivery Service");
- })
- app.post('/send', (req, res) => {
- if(req.body == undefined){
- console.log(`Received delivery request for message with no data. Aborting`);
- res.status(400).json({ error: 'No data'}).end();
- return;
- }
- if(req.body.from == "" || req.body.from == undefined){
- console.log(`Received delivery request for message with no from email. Aborting`);
- res.status(400).json({ error: 'Missing \'from\' field'}).end();
- return;
- }
- if(req.body.to == "" || req.body.to == undefined){
- console.log(`Received delivery request for message with no to email. Aborting`);
- res.status(400).json({ error: 'Missing \'to\' field'}).end();
- return;
- }
- if(req.body.message == "" || req.body.message == undefined){
- console.log(`Received delivery request for message with no to message. Aborting`);
- res.status(400).json({ error: 'Missing \'message\' field'}).end();
- return;
- }
- if(req.body.subject == "" || req.body.subject == undefined){
- console.log(`Received delivery request for message with empty subject. Aborting`);
- res.status(400).json({ error: 'Missing \'subject\' field'}).end();
- return;
- }
- var subject = chalk.italic(`\'${req.body.subject}\'`);
- var to = chalk.italic(`\'${req.body.to}\'`);
- var from = chalk.italic(`\'${req.body.from}\'`);
- var id = uniqid();
- var id_text = chalk.italic(`\'${id}\'`);
- console.log(`Delivering email ${subject} (${id_text}) to ${to} from ${from}`);
- //console.log(req.body);
- res.status(200).end();
- helpers.send_mail(req.body, id);
- })
- app.listen(port, () => {
- console.log(`Mail Delivery Service listening at ${chalk.cyan(`http://localhost:${port}`)}`);
- })
- });
|