This commit is contained in:
Leandro Facchinetti 2020-03-19 11:47:37 -04:00
parent b10b3af881
commit f192ef56ca
1 changed files with 12 additions and 10 deletions

View File

@ -7,9 +7,9 @@ import xml2js from "xml2js";
import fs from "fs"; import fs from "fs";
import cryptoRandomString from "crypto-random-string"; import cryptoRandomString from "crypto-random-string";
const app = express(); const webApp = express();
if (process.env.NODE_ENV === "production") if (process.env.NODE_ENV === "production")
app.use((req, res, next) => { webApp.use((req, res, next) => {
if ( if (
req.protocol !== "https" || req.protocol !== "https" ||
req.hostname !== "www.kill-the-newsletter.com" req.hostname !== "www.kill-the-newsletter.com"
@ -20,9 +20,9 @@ if (process.env.NODE_ENV === "production")
); );
next(); next();
}); });
app.use(express.static("static")); webApp.use(express.static("static"));
app.use(express.urlencoded({ extended: true })); webApp.use(express.urlencoded({ extended: true }));
app.get("/", (req, res) => webApp.get("/", (req, res) =>
res.send( res.send(
renderHTML( renderHTML(
<Layout> <Layout>
@ -31,7 +31,7 @@ app.get("/", (req, res) =>
) )
) )
); );
app.post("/", (req, res) => { webApp.post("/", (req, res) => {
const inbox: Inbox = { name: req.body.name, token: newToken() }; const inbox: Inbox = { name: req.body.name, token: newToken() };
fs.writeFileSync(feedPath(inbox.token), renderXML(Feed(inbox))); fs.writeFileSync(feedPath(inbox.token), renderXML(Feed(inbox)));
res.send( res.send(
@ -43,7 +43,7 @@ app.post("/", (req, res) => {
); );
}); });
export const emailServer = new SMTPServer({ const emailApp = new SMTPServer({
authOptional: true, authOptional: true,
async onData(stream, session, callback) { async onData(stream, session, callback) {
const paths = session.envelope.rcptTo.flatMap(({ address }) => { const paths = session.envelope.rcptTo.flatMap(({ address }) => {
@ -77,13 +77,15 @@ export const emailServer = new SMTPServer({
} }
}); });
export const webServer = app.listen( export const webServer = webApp.listen(
process.env.NODE_ENV === "production" ? 80 : 8000 process.env.NODE_ENV === "production" ? 80 : 8000
); );
if (process.env.NODE_ENV === "production") { if (process.env.NODE_ENV === "production") {
app.listen(443); webApp.listen(443);
} }
emailServer.listen(process.env.NODE_ENV === "production" ? 25 : 2525); export const emailServer = emailApp.listen(
process.env.NODE_ENV === "production" ? 25 : 2525
);
type Inbox = { type Inbox = {
name: string; name: string;