server.ts 5.7 KB

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