This commit is contained in:
Leandro Facchinetti 2020-03-19 10:17:38 -04:00
parent 4131c21645
commit 6dcd96ab06
2 changed files with 40 additions and 44 deletions

View File

@ -8,10 +8,22 @@ import { Builder, Parser } from "xml2js";
import fs from "fs"; import fs from "fs";
import cryptoRandomString from "crypto-random-string"; import cryptoRandomString from "crypto-random-string";
const webApp = express() const app = express();
.use(express.static("static")) if (process.env.NODE_ENV === "production")
.use(express.urlencoded({ extended: true })) app.use((req, res, next) => {
.get("/", (req, res) => if (
req.protocol !== "https" ||
req.hostname !== "www.kill-the-newsletter.com"
)
return res.redirect(
301,
`https://www.kill-the-newsletter.com${req.originalUrl}`
);
next();
});
app.use(express.static("static"));
app.use(express.urlencoded({ extended: true }));
app.get("/", (req, res) =>
res.send( res.send(
renderHTML( renderHTML(
<Layout> <Layout>
@ -19,8 +31,8 @@ const webApp = express()
</Layout> </Layout>
) )
) )
) );
.post("/", (req, res) => { app.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(
@ -65,29 +77,13 @@ export const emailServer = new SMTPServer({
} }
}); });
export let developmentWebServer: Server; export const webServer = app.listen(
process.env.NODE_ENV === "production" ? 80 : 8000
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(); if (process.env.NODE_ENV === "production") {
}) app.listen(443);
.use(webApp);
productionWebApp.listen(80);
productionWebApp.listen(443);
emailServer.listen(25);
} else {
developmentWebServer = webApp.listen(8000);
emailServer.listen(2525);
} }
emailServer.listen(process.env.NODE_ENV === "production" ? 25 : 2525);
type Inbox = { type Inbox = {
name: string; name: string;

View File

@ -1,4 +1,4 @@
import { developmentWebServer, emailServer, feedPath, feedEmail } from "."; import { webServer, emailServer, feedPath, feedEmail } from ".";
import { createTransport } from "nodemailer"; import { createTransport } from "nodemailer";
import fetch from "node-fetch"; import fetch from "node-fetch";
import fs from "fs"; import fs from "fs";
@ -58,7 +58,7 @@ describe("receive email", () => {
}); });
afterAll(() => { afterAll(() => {
developmentWebServer.close(); webServer.close();
emailServer.close(() => {}); emailServer.close(() => {});
}); });