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,29 +8,41 @@ 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 (
res.send( req.protocol !== "https" ||
renderHTML( req.hostname !== "www.kill-the-newsletter.com"
<Layout> )
<Form></Form> return res.redirect(
</Layout> 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(
renderHTML(
<Layout>
<Form></Form>
</Layout>
) )
) )
.post("/", (req, res) => { );
const inbox: Inbox = { name: req.body.name, token: newToken() }; app.post("/", (req, res) => {
fs.writeFileSync(feedPath(inbox.token), renderXML(Feed(inbox))); const inbox: Inbox = { name: req.body.name, token: newToken() };
res.send( fs.writeFileSync(feedPath(inbox.token), renderXML(Feed(inbox)));
renderHTML( res.send(
<Layout> renderHTML(
<Created inbox={inbox}></Created> <Layout>
</Layout> <Created inbox={inbox}></Created>
) </Layout>
); )
}); );
});
export const emailServer = new SMTPServer({ export const emailServer = new SMTPServer({
authOptional: true, authOptional: true,
@ -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") { if (process.env.NODE_ENV === "production") {
const productionWebApp = express() app.listen(443);
.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);
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(() => {});
}); });