server.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. console.log({ error });
  47. res
  48. .status(500)
  49. .json({ error: "Sorry an error ocurred. Please try again later." });
  50. if (process.env.RAVEN_DSN) {
  51. Raven.captureException(error, {
  52. user: { email: req.user && req.user.email }
  53. });
  54. }
  55. next();
  56. });
  57. server.use((req, _res, next) => {
  58. req.realIP =
  59. (req.headers["x-real-ip"] as string) ||
  60. req.connection.remoteAddress ||
  61. "";
  62. return next();
  63. });
  64. server.use(link.customDomainRedirection);
  65. server.get("/", (req, res) => app.render(req, res, "/"));
  66. server.get("/login", (req, res) => app.render(req, res, "/login"));
  67. server.get("/logout", (req, res) => app.render(req, res, "/logout"));
  68. server.get("/settings", (req, res) => app.render(req, res, "/settings"));
  69. server.get("/stats", (req, res) => app.render(req, res, "/stats", req.query));
  70. server.get("/terms", (req, res) => app.render(req, res, "/terms"));
  71. server.get("/report", (req, res) => app.render(req, res, "/report"));
  72. server.get("/banned", (req, res) => app.render(req, res, "/banned"));
  73. server.get("/offline", (req, res) => app.render(req, res, "/offline"));
  74. /* View routes */
  75. server.get(
  76. "/reset-password/:resetPasswordToken?",
  77. catchErrors(auth.resetUserPassword),
  78. (req, res) => app.render(req, res, "/reset-password", { token: req.token })
  79. );
  80. server.get(
  81. "/verify/:verificationToken?",
  82. catchErrors(auth.verify),
  83. (req, res) => app.render(req, res, "/verify", { token: req.token })
  84. );
  85. /* User and authentication */
  86. server.post(
  87. "/api/auth/signup",
  88. validationCriterias,
  89. catchErrors(validateBody),
  90. catchErrors(auth.signup)
  91. );
  92. server.post(
  93. "/api/auth/login",
  94. validationCriterias,
  95. catchErrors(validateBody),
  96. catchErrors(auth.authLocal),
  97. catchErrors(auth.login)
  98. );
  99. server.post(
  100. "/api/auth/renew",
  101. catchErrors(auth.authJwt),
  102. catchErrors(auth.renew)
  103. );
  104. server.post(
  105. "/api/auth/changepassword",
  106. catchErrors(auth.authJwt),
  107. catchErrors(auth.changeUserPassword)
  108. );
  109. server.post(
  110. "/api/auth/generateapikey",
  111. catchErrors(auth.authJwt),
  112. catchErrors(auth.generateUserApiKey)
  113. );
  114. server.post(
  115. "/api/auth/resetpassword",
  116. catchErrors(auth.requestUserPasswordReset)
  117. );
  118. server.get(
  119. "/api/auth/usersettings",
  120. catchErrors(auth.authJwt),
  121. catchErrors(auth.userSettings)
  122. );
  123. /* URL shortener */
  124. server.post(
  125. "/api/url/submit",
  126. cors(),
  127. catchErrors(auth.authApikey),
  128. catchErrors(auth.authJwtLoose),
  129. catchErrors(auth.recaptcha),
  130. catchErrors(validateUrl),
  131. catchErrors(ipCooldownCheck),
  132. catchErrors(link.shortener)
  133. );
  134. server.post(
  135. "/api/url/deleteurl",
  136. catchErrors(auth.authApikey),
  137. catchErrors(auth.authJwt),
  138. catchErrors(link.deleteUserLink)
  139. );
  140. server.get(
  141. "/api/url/geturls",
  142. catchErrors(auth.authApikey),
  143. catchErrors(auth.authJwt),
  144. catchErrors(link.getUserLinks)
  145. );
  146. server.post(
  147. "/api/url/customdomain",
  148. catchErrors(auth.authJwt),
  149. catchErrors(link.setCustomDomain)
  150. );
  151. server.delete(
  152. "/api/url/customdomain",
  153. catchErrors(auth.authJwt),
  154. catchErrors(link.deleteCustomDomain)
  155. );
  156. server.get(
  157. "/api/url/stats",
  158. catchErrors(auth.authApikey),
  159. catchErrors(auth.authJwt),
  160. catchErrors(link.getLinkStats)
  161. );
  162. server.post("/api/url/requesturl", catchErrors(link.goToLink));
  163. server.post("/api/url/report", catchErrors(link.reportLink));
  164. server.post(
  165. "/api/url/admin/ban",
  166. catchErrors(auth.authApikey),
  167. catchErrors(auth.authJwt),
  168. catchErrors(auth.authAdmin),
  169. catchErrors(link.ban)
  170. );
  171. server.get(
  172. "/:id",
  173. catchErrors(link.goToLink),
  174. (req: Request, res: Response) => {
  175. switch (req.pageType) {
  176. case "password":
  177. return app.render(req, res, "/url-password", {
  178. protectedLink: req.protectedLink
  179. });
  180. case "info":
  181. default:
  182. return app.render(req, res, "/url-info", {
  183. linkTarget: req.linkTarget
  184. });
  185. }
  186. }
  187. );
  188. server.get("*", (req, res) => handle(req, res));
  189. server.listen(port, err => {
  190. if (err) throw err;
  191. console.log(`> Ready on http://localhost:${port}`);
  192. });
  193. });