UNPKG

5.61 kBPlain TextView Raw
1/**
2 * Module dependencies.
3 */
4import * as express from "express";
5import * as compression from "compression"; // compresses requests
6import * as session from "express-session";
7// import * as cookieSession from "cookie-session";
8import * as bodyParser from "body-parser";
9import * as errorHandler from "errorhandler";
10import * as dotenv from "dotenv";
11import * as mongo from "connect-mongo";
12import * as path from "path";
13import * as mongoose from "mongoose";
14import * as passport from "passport";
15import * as cors from "cors";
16import expressValidator = require("express-validator");
17const MongoStore = mongo(session);
18
19/**
20 * Load environment variables from .env file, where API keys and passwords are configured.
21 */
22dotenv.config({ path: ".env" });
23
24// import * as admin from "firebase-admin";
25
26/**
27 * Models
28 */
29import "./models/form.model";
30import "./models/bpmn.model";
31
32/**
33 * Controllers (route handlers).
34 */
35import * as homeController from "./controllers/home";
36import * as userController from "./controllers/user.controller";
37import * as configController from "./controllers/config.controller";
38import * as formController from "./controllers/form.controller";
39import * as bpmnController from "./controllers/bpmn.controller";
40import * as diagramController from "./controllers/diagram.controller";
41// import * as apiController from "./controllers/api";
42import * as fakeController from "./controllers/fake.controller";
43import * as dataController from "./controllers/data-provider.controller";
44import * as eventController from "./controllers/event.controller";
45import * as sourceController from "./controllers/source.controller";
46
47/**
48 * API keys and Passport configuration.
49 */
50import * as passportConfig from "./config/passport";
51
52/**
53 * Create Express server.
54 */
55const app: express.Application = express();
56/**
57 * Connect to MongoDB.
58 */
59// mongoose.Promise = global.Promise;
60mongoose.connect(process.env.MONGODB_URI || process.env.MONGOLAB_URI);
61
62mongoose.connection.on("error", () => {
63 console.log("MongoDB connection error. Please make sure MongoDB is running.");
64 process.exit();
65});
66
67/**
68 * Express configuration.
69 */
70app.set("port", process.env.PORT || 3000);
71
72app.use(compression());
73app.use(bodyParser.json());
74app.use(bodyParser.urlencoded({ extended: true }));
75app.use(expressValidator());
76
77const originsWhitelist = [ process.env.TEST_SERVER_ADDRESS, process.env.SERVER_ADDRESS ];
78const corsOptions = {
79 origin: function(origin: any, callback: any) {
80 const isWhitelisted = originsWhitelist.indexOf(origin) !== -1;
81 callback(undefined, isWhitelisted);
82 },
83 credentials: true
84};
85app.use(cors(corsOptions));
86
87app.use(
88 session({
89 resave: true,
90 saveUninitialized: true,
91 secret: process.env.SESSION_SECRET,
92 store: new MongoStore({
93 url: process.env.MONGODB_URI || process.env.MONGOLAB_URI,
94 autoReconnect: true
95 })
96 })
97);
98// app.use(cookieSession({
99// name: "session",
100// keys: ["key1"],
101// maxAge: 24 * 60 * 60 * 1000
102// }));
103
104app.use(passport.initialize());
105app.use(passport.session());
106app.use((req, res, next) => {
107 res.locals.user = req.user;
108 next();
109});
110app.use((req, res, next) => {
111 // After successful login, redirect back to the intended page
112 if (
113 !req.user &&
114 req.path !== "/api/user/signin" &&
115 req.path !== "/signup" &&
116 !req.path.match(/^\/auth/) &&
117 !req.path.match(/\./)
118 ) {
119 req.session.returnTo = req.path;
120 } else if (req.user && req.path == "/account") {
121 req.session.returnTo = req.path;
122 }
123 next();
124});
125app.use(express.static(path.join(__dirname, "../src"), { maxAge: 31557600000 }));
126
127/**
128 * Primary app routes.
129 */
130// app.get("*", (req, res) => {
131// res.sendFile(path.join(__dirname, "../src/index-jit.html"));
132// });
133app.get("/", homeController.index);
134app.use("/api/user", userController.router);
135app.use("/api/config", configController.router);
136app.use("/api/form", formController.router);
137app.use("/api/bpmn", bpmnController.router);
138app.use("/api/diagram", diagramController.router);
139app.use("/api/fake", fakeController.router);
140app.use("/api/data", dataController.router);
141app.use("/api/event", eventController.router);
142app.use("/api/source", sourceController.router);
143
144// app.get("/login", userController.getLogin);
145// app.post("/login", userController.postLogin);
146// app.get("/logout", userController.logout);
147// app.get("/forgot", userController.getForgot);
148// app.post("/forgot", userController.postForgot);
149// app.get("/reset/:token", userController.getReset);
150// app.post("/reset/:token", userController.postReset);
151// app.get("/signup", userController.getSignup);
152// app.post("/signup", userController.postSignup);
153// app.get("api/account", passportConfig.isAuthenticated, userController.getAccount);
154
155app.post("/api/account/profile", userController.postUpdateProfile);
156app.post("/account/password", passportConfig.isAuthenticated, userController.postUpdatePassword);
157app.post("/account/delete", passportConfig.isAuthenticated, userController.postDeleteAccount);
158app.get("/account/unlink/:provider", passportConfig.isAuthenticated, userController.getOauthUnlink);
159
160// app.get("/api", apiController.getApi);
161
162/**
163 * Error Handler. Provides full stack - remove for production
164 */
165app.use(errorHandler());
166
167app.listen(app.get("port"), () => {
168 console.log(" App is running at http://localhost:%d in %s mode", app.get("port"), app.get("env"));
169 console.log(" Press CTRL-C to stop\n");
170 sourceController.sourceJob();
171});
172
173module.exports = app;