server.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. server.get("/offline", (req, res) => app.render(req, res, "/offline"));
  73. /* View routes */
  74. server.get(
  75. "/reset-password/:resetPasswordToken?",
  76. catchErrors(auth.resetUserPassword),
  77. (req, res) => app.render(req, res, "/reset-password", { token: req.token })
  78. );
  79. server.get(
  80. "/verify/:verificationToken?",
  81. catchErrors(auth.verify),
  82. (req, res) => app.render(req, res, "/verify", { token: req.token })
  83. );
  84. /* User and authentication */
  85. server.post(
  86. "/api/auth/signup",
  87. validationCriterias,
  88. catchErrors(validateBody),
  89. catchErrors(auth.signup)
  90. );
  91. server.post(
  92. "/api/auth/login",
  93. validationCriterias,
  94. catchErrors(validateBody),
  95. catchErrors(auth.authLocal),
  96. catchErrors(auth.login)
  97. );
  98. server.post(
  99. "/api/auth/renew",
  100. catchErrors(auth.authJwt),
  101. catchErrors(auth.renew)
  102. );
  103. server.post(
  104. "/api/auth/changepassword",
  105. catchErrors(auth.authJwt),
  106. catchErrors(auth.changeUserPassword)
  107. );
  108. server.post(
  109. "/api/auth/generateapikey",
  110. catchErrors(auth.authJwt),
  111. catchErrors(auth.generateUserApiKey)
  112. );
  113. server.post(
  114. "/api/auth/resetpassword",
  115. catchErrors(auth.requestUserPasswordReset)
  116. );
  117. server.get(
  118. "/api/auth/usersettings",
  119. catchErrors(auth.authJwt),
  120. catchErrors(auth.userSettings)
  121. );
  122. /* URL shortener */
  123. server.post(
  124. "/api/url/submit",
  125. cors(),
  126. catchErrors(auth.authApikey),
  127. catchErrors(auth.authJwtLoose),
  128. catchErrors(auth.recaptcha),
  129. catchErrors(validateUrl),
  130. catchErrors(ipCooldownCheck),
  131. catchErrors(link.shortener)
  132. );
  133. server.post(
  134. "/api/url/deleteurl",
  135. catchErrors(auth.authApikey),
  136. catchErrors(auth.authJwt),
  137. catchErrors(link.deleteUserLink)
  138. );
  139. server.get(
  140. "/api/url/geturls",
  141. catchErrors(auth.authApikey),
  142. catchErrors(auth.authJwt),
  143. catchErrors(link.getUserLinks)
  144. );
  145. server.post(
  146. "/api/url/customdomain",
  147. catchErrors(auth.authJwt),
  148. catchErrors(link.setCustomDomain)
  149. );
  150. server.delete(
  151. "/api/url/customdomain",
  152. catchErrors(auth.authJwt),
  153. catchErrors(link.deleteCustomDomain)
  154. );
  155. server.get(
  156. "/api/url/stats",
  157. catchErrors(auth.authApikey),
  158. catchErrors(auth.authJwt),
  159. catchErrors(link.getLinkStats)
  160. );
  161. server.post("/api/url/requesturl", catchErrors(link.goToLink));
  162. server.post("/api/url/report", catchErrors(link.reportLink));
  163. server.post(
  164. "/api/url/admin/ban",
  165. catchErrors(auth.authApikey),
  166. catchErrors(auth.authJwt),
  167. catchErrors(auth.authAdmin),
  168. catchErrors(link.ban)
  169. );
  170. server.get(
  171. "/:id",
  172. catchErrors(link.goToLink),
  173. (req: Request, res: Response) => {
  174. switch (req.pageType) {
  175. case "password":
  176. return app.render(req, res, "/url-password", {
  177. protectedLink: req.protectedLink
  178. });
  179. case "info":
  180. default:
  181. return app.render(req, res, "/url-info", {
  182. linkTarget: req.linkTarget
  183. });
  184. }
  185. }
  186. );
  187. server.get("*", (req, res) => handle(req, res));
  188. server.listen(port, err => {
  189. if (err) throw err;
  190. console.log(`> Ready on http://localhost:${port}`);
  191. });
  192. });