server.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import "./configToEnv";
  2. import dotenv from "dotenv";
  3. dotenv.config();
  4. import nextApp from "next";
  5. import express, { Request, Response } from "express";
  6. import helmet from "helmet";
  7. import morgan from "morgan";
  8. import Raven from "raven";
  9. import cookieParser from "cookie-parser";
  10. import passport from "passport";
  11. import cors from "cors";
  12. import {
  13. validateBody,
  14. validationCriterias,
  15. validateUrl,
  16. ipCooldownCheck
  17. } from "./controllers/validateBodyController";
  18. import * as auth from "./controllers/authController";
  19. import * as link from "./controllers/linkController";
  20. import { initializeDb } from "./knex";
  21. import "./cron";
  22. import "./passport";
  23. if (process.env.RAVEN_DSN) {
  24. Raven.config(process.env.RAVEN_DSN).install();
  25. }
  26. const catchErrors = fn => (req, res, next) =>
  27. Promise.resolve(fn(req, res, next)).catch(next);
  28. const port = Number(process.env.PORT) || 3000;
  29. const dev = process.env.NODE_ENV !== "production";
  30. const app = nextApp({ dir: "./client", dev });
  31. const handle = app.getRequestHandler();
  32. app.prepare().then(async () => {
  33. await initializeDb();
  34. const server = express();
  35. server.set("trust proxy", true);
  36. if (process.env.NODE_ENV !== "production") {
  37. server.use(morgan("dev"));
  38. }
  39. server.use(helmet());
  40. server.use(cookieParser());
  41. server.use(express.json());
  42. server.use(express.urlencoded({ extended: true }));
  43. server.use(passport.initialize());
  44. server.use(express.static("static"));
  45. server.use((error, req, res, next) => {
  46. res
  47. .status(500)
  48. .json({ error: "Sorry an error ocurred. Please try again later." });
  49. if (process.env.RAVEN_DSN) {
  50. Raven.captureException(error, {
  51. user: { email: req.user && req.user.email }
  52. });
  53. }
  54. next();
  55. });
  56. server.use((req, _res, next) => {
  57. req.realIP =
  58. (req.headers["x-real-ip"] as string) ||
  59. req.connection.remoteAddress ||
  60. "";
  61. return next();
  62. });
  63. server.use(link.customDomainRedirection);
  64. server.get("/", (req, res) => app.render(req, res, "/"));
  65. server.get("/login", (req, res) => app.render(req, res, "/login"));
  66. server.get("/logout", (req, res) => app.render(req, res, "/logout"));
  67. server.get("/settings", (req, res) => app.render(req, res, "/settings"));
  68. server.get("/stats", (req, res) => app.render(req, res, "/stats", req.query));
  69. server.get("/terms", (req, res) => app.render(req, res, "/terms"));
  70. server.get("/report", (req, res) => app.render(req, res, "/report"));
  71. server.get("/banned", (req, res) => app.render(req, res, "/banned"));
  72. /* View routes */
  73. server.get(
  74. "/reset-password/:resetPasswordToken?",
  75. catchErrors(auth.resetUserPassword),
  76. (req, res) => app.render(req, res, "/reset-password", { token: req.token })
  77. );
  78. server.get(
  79. "/verify/:verificationToken?",
  80. catchErrors(auth.verify),
  81. (req, res) => app.render(req, res, "/verify", { token: req.token })
  82. );
  83. /* User and authentication */
  84. server.post(
  85. "/api/auth/signup",
  86. validationCriterias,
  87. catchErrors(validateBody),
  88. catchErrors(auth.signup)
  89. );
  90. server.post(
  91. "/api/auth/login",
  92. validationCriterias,
  93. catchErrors(validateBody),
  94. catchErrors(auth.authLocal),
  95. catchErrors(auth.login)
  96. );
  97. server.post(
  98. "/api/auth/renew",
  99. catchErrors(auth.authJwt),
  100. catchErrors(auth.renew)
  101. );
  102. server.post(
  103. "/api/auth/changepassword",
  104. catchErrors(auth.authJwt),
  105. catchErrors(auth.changeUserPassword)
  106. );
  107. server.post(
  108. "/api/auth/generateapikey",
  109. catchErrors(auth.authJwt),
  110. catchErrors(auth.generateUserApiKey)
  111. );
  112. server.post(
  113. "/api/auth/resetpassword",
  114. catchErrors(auth.requestUserPasswordReset)
  115. );
  116. server.get(
  117. "/api/auth/usersettings",
  118. catchErrors(auth.authJwt),
  119. catchErrors(auth.userSettings)
  120. );
  121. /* URL shortener */
  122. server.post(
  123. "/api/url/submit",
  124. cors(),
  125. catchErrors(auth.authApikey),
  126. catchErrors(auth.authJwtLoose),
  127. catchErrors(auth.recaptcha),
  128. catchErrors(validateUrl),
  129. catchErrors(ipCooldownCheck),
  130. catchErrors(link.shortener)
  131. );
  132. server.post(
  133. "/api/url/deleteurl",
  134. catchErrors(auth.authApikey),
  135. catchErrors(auth.authJwt),
  136. catchErrors(link.deleteUserLink)
  137. );
  138. server.get(
  139. "/api/url/geturls",
  140. catchErrors(auth.authApikey),
  141. catchErrors(auth.authJwt),
  142. catchErrors(link.getUserLinks)
  143. );
  144. server.post(
  145. "/api/url/customdomain",
  146. catchErrors(auth.authJwt),
  147. catchErrors(link.setCustomDomain)
  148. );
  149. server.delete(
  150. "/api/url/customdomain",
  151. catchErrors(auth.authJwt),
  152. catchErrors(link.deleteCustomDomain)
  153. );
  154. server.get(
  155. "/api/url/stats",
  156. catchErrors(auth.authApikey),
  157. catchErrors(auth.authJwt),
  158. catchErrors(link.getLinkStats)
  159. );
  160. server.post("/api/url/requesturl", catchErrors(link.goToLink));
  161. server.post("/api/url/report", catchErrors(link.reportLink));
  162. server.post(
  163. "/api/url/admin/ban",
  164. catchErrors(auth.authApikey),
  165. catchErrors(auth.authJwt),
  166. catchErrors(auth.authAdmin),
  167. catchErrors(link.ban)
  168. );
  169. server.get(
  170. "/:id",
  171. catchErrors(link.goToLink),
  172. (req: Request, res: Response) => {
  173. switch (req.pageType) {
  174. case "password":
  175. return app.render(req, res, "/url-password", {
  176. protectedLink: req.protectedLink
  177. });
  178. case "info":
  179. default:
  180. return app.render(req, res, "/url-info", {
  181. linkTarget: req.linkTarget
  182. });
  183. }
  184. }
  185. );
  186. server.get("*", (req, res) => handle(req, res));
  187. server.listen(port, err => {
  188. if (err) throw err;
  189. console.log(`> Ready on http://localhost:${port}`);
  190. });
  191. });