This commit is contained in:
Leandro Facchinetti 2020-03-19 11:50:15 -04:00
parent f192ef56ca
commit f22e678b31
1 changed files with 32 additions and 33 deletions

View File

@ -7,41 +7,29 @@ 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 webApp = express(); const webApp = express()
if (process.env.NODE_ENV === "production") .use(express.static("static"))
webApp.use((req, res, next) => { .use(express.urlencoded({ extended: true }))
if ( .get("/", (req, res) =>
req.protocol !== "https" || res.send(
req.hostname !== "www.kill-the-newsletter.com" renderHTML(
) <Layout>
return res.redirect( <Form></Form>
301, </Layout>
`https://www.kill-the-newsletter.com${req.originalUrl}` )
);
next();
});
webApp.use(express.static("static"));
webApp.use(express.urlencoded({ extended: true }));
webApp.get("/", (req, res) =>
res.send(
renderHTML(
<Layout>
<Form></Form>
</Layout>
) )
) )
); .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( renderHTML(
renderHTML( <Layout>
<Layout> <Created inbox={inbox}></Created>
<Created inbox={inbox}></Created> </Layout>
</Layout> )
) );
); });
});
const emailApp = new SMTPServer({ const emailApp = new SMTPServer({
authOptional: true, authOptional: true,
@ -81,6 +69,17 @@ 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") {
webApp.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();
});
webApp.listen(443); webApp.listen(443);
} }
export const emailServer = emailApp.listen( export const emailServer = emailApp.listen(