From fd985e0b1da234d3e5aae71b8b7a7643648c7585 Mon Sep 17 00:00:00 2001 From: Leandro Facchinetti Date: Fri, 20 Mar 2020 17:31:46 -0400 Subject: [PATCH] . --- package.json | 6 ++--- src/index.tsx | 62 ++++++++++++++++----------------------------------- src/test.ts | 6 ++--- 3 files changed, 25 insertions(+), 49 deletions(-) diff --git a/package.json b/package.json index 397004e..bc23bf8 100644 --- a/package.json +++ b/package.json @@ -51,14 +51,14 @@ "ref": "origin/master", "repo": "git@github.com:leafac/www.kill-the-newsletter.com.git", "path": "/root/www.kill-the-newsletter.com", - "pre-setup": "(ssh -o StrictHostKeyChecking=no git@github.com || true) && apt-get update && apt-get install -y build-essential software-properties-common && (curl -sL https://deb.nodesource.com/setup_13.x | bash -) && add-apt-repository universe && add-apt-repository ppa:certbot/certbot && apt-get install -y nodejs certbot && certbot certonly --standalone --non-interactive --agree-tos --email kill-the-newsletter@leafac.com --domain kill-the-newsletter.com --domain www.kill-the-newsletter.com", + "pre-setup": "(curl -sL https://deb.nodesource.com/setup_13.x | bash -) && sudo apt-get install -y nodejs build-essential && (curl https://getcaddy.com | bash -s personal) && (ssh -o StrictHostKeyChecking=no git@github.com || true)", "ssh_options": [ "ForwardAgent=yes", "StrictHostKeyChecking=no" ], "TODO": [ - "https://certbot.eff.org/docs/using.html", - "--pre-hook '/root/www.kill-the-newsletter.com/current/node_modules/.bin/pm2 stop all' --post-hook '/root/www.kill-the-newsletter.com/current/node_modules/.bin/pm2 start all'", + "Caddyfile", + "/root/www.kill-the-newsletter.com/current/node_modules/.bin/pm2 startOrRestart all", "env NODE_ENV=production npm ci", "rsync", "pm2 startup OR pm2 save", diff --git a/src/index.tsx b/src/index.tsx index f511ac8..540ce0d 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,7 +1,5 @@ import express from "express"; -import http from "http"; -import https from "https"; -import { SMTPServer, SMTPServerOptions } from "smtp-server"; +import { SMTPServer } from "smtp-server"; import mailparser from "mailparser"; import React from "react"; import ReactDOMServer from "react-dom/server"; @@ -9,7 +7,7 @@ import xml2js from "xml2js"; import fs from "fs"; import cryptoRandomString from "crypto-random-string"; -const webApp = express() +export const webServer = express() .use(express.static("static")) .use(express.urlencoded({ extended: true })) .get("/", (req, res) => @@ -36,9 +34,10 @@ const webApp = express() ) ); - }); + }) + .listen(8000, "localhost"); -const emailApp: SMTPServerOptions = { +export const emailServer = new SMTPServer({ authOptional: true, async onData(stream, session, callback) { const paths = session.envelope.rcptTo.flatMap(({ address }) => { @@ -69,43 +68,20 @@ const emailApp: SMTPServerOptions = { fs.writeFileSync(path, renderXML(xml)); } callback(); - } -}; - -export const developmentWebServer = http.createServer(webApp); -export const developmentEmailServer = new SMTPServer(emailApp); - -if (process.env.NODE_ENV === "production") { - const productionWebApp = express() - .use((req, res, next) => { - if ( - req.protocol !== "https" || - req.hostname !== "www.kill-the-newsletter.com" - ) - return res.redirect( - 301, - `https://www.kill-the-newsletter.com${req.originalUrl}` - ); - next(); - }) - .use(webApp); - const credentials = { - key: fs.readFileSync( - "/etc/letsencrypt/live/kill-the-newsletter.com/privkey.pem", - "utf8" - ), - cert: fs.readFileSync( - "/etc/letsencrypt/live/kill-the-newsletter.com/fullchain.pem", - "utf8" - ) - }; - http.createServer(productionWebApp).listen(80); - https.createServer(credentials, productionWebApp).listen(443); - new SMTPServer({ ...credentials, ...emailApp }).listen(25); -} else { - developmentWebServer.listen(8000); - developmentEmailServer.listen(2525); -} + }, + ...(process.env.NODE_ENV === "production" + ? { + key: fs.readFileSync( + "/etc/letsencrypt/live/kill-the-newsletter.com/privkey.pem", + "utf8" + ), + cert: fs.readFileSync( + "/etc/letsencrypt/live/kill-the-newsletter.com/fullchain.pem", + "utf8" + ) + } + : {}) +}).listen(process.env.NODE_ENV === "production" ? 25 : 2525); function Layout({ children }: { children: React.ReactNode }) { return ( diff --git a/src/test.ts b/src/test.ts index 9288746..9cdd39b 100644 --- a/src/test.ts +++ b/src/test.ts @@ -1,4 +1,4 @@ -import { developmentWebServer, developmentEmailServer, feedEmail } from "."; +import { webServer, emailServer, feedEmail } from "."; import nodemailer from "nodemailer"; import axios from "axios"; import qs from "qs"; @@ -58,9 +58,9 @@ describe("receive email", () => { }); afterAll(() => { - developmentWebServer.close(); + webServer.close(); // FIXME: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/43268 - developmentEmailServer.close(() => {}); + emailServer.close(() => {}); }); async function createFeed(): Promise {