1 |
|
2 |
|
3 |
|
4 | import * as express from "express";
|
5 | import * as compression from "compression";
|
6 | import * as session from "express-session";
|
7 |
|
8 | import * as bodyParser from "body-parser";
|
9 | import * as errorHandler from "errorhandler";
|
10 | import * as dotenv from "dotenv";
|
11 | import * as mongo from "connect-mongo";
|
12 | import * as path from "path";
|
13 | import * as mongoose from "mongoose";
|
14 | import * as passport from "passport";
|
15 | import * as cors from "cors";
|
16 | import expressValidator = require("express-validator");
|
17 | const MongoStore = mongo(session);
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | dotenv.config({ path: ".env" });
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | import "./models/form.model";
|
30 | import "./models/bpmn.model";
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | import * as homeController from "./controllers/home";
|
36 | import * as userController from "./controllers/user.controller";
|
37 | import * as configController from "./controllers/config.controller";
|
38 | import * as formController from "./controllers/form.controller";
|
39 | import * as bpmnController from "./controllers/bpmn.controller";
|
40 | import * as diagramController from "./controllers/diagram.controller";
|
41 |
|
42 | import * as fakeController from "./controllers/fake.controller";
|
43 | import * as dataController from "./controllers/data-provider.controller";
|
44 | import * as eventController from "./controllers/event.controller";
|
45 | import * as sourceController from "./controllers/source.controller";
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | import * as passportConfig from "./config/passport";
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | const app: express.Application = express();
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | mongoose.connect(process.env.MONGODB_URI || process.env.MONGOLAB_URI);
|
61 |
|
62 | mongoose.connection.on("error", () => {
|
63 | console.log("MongoDB connection error. Please make sure MongoDB is running.");
|
64 | process.exit();
|
65 | });
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | app.set("port", process.env.PORT || 3000);
|
71 |
|
72 | app.use(compression());
|
73 | app.use(bodyParser.json());
|
74 | app.use(bodyParser.urlencoded({ extended: true }));
|
75 | app.use(expressValidator());
|
76 |
|
77 | const originsWhitelist = [ process.env.TEST_SERVER_ADDRESS, process.env.SERVER_ADDRESS ];
|
78 | const corsOptions = {
|
79 | origin: function(origin: any, callback: any) {
|
80 | const isWhitelisted = originsWhitelist.indexOf(origin) !== -1;
|
81 | callback(undefined, isWhitelisted);
|
82 | },
|
83 | credentials: true
|
84 | };
|
85 | app.use(cors(corsOptions));
|
86 |
|
87 | app.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 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | app.use(passport.initialize());
|
105 | app.use(passport.session());
|
106 | app.use((req, res, next) => {
|
107 | res.locals.user = req.user;
|
108 | next();
|
109 | });
|
110 | app.use((req, res, next) => {
|
111 |
|
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 | });
|
125 | app.use(express.static(path.join(__dirname, "../src"), { maxAge: 31557600000 }));
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | app.get("/", homeController.index);
|
134 | app.use("/api/user", userController.router);
|
135 | app.use("/api/config", configController.router);
|
136 | app.use("/api/form", formController.router);
|
137 | app.use("/api/bpmn", bpmnController.router);
|
138 | app.use("/api/diagram", diagramController.router);
|
139 | app.use("/api/fake", fakeController.router);
|
140 | app.use("/api/data", dataController.router);
|
141 | app.use("/api/event", eventController.router);
|
142 | app.use("/api/source", sourceController.router);
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | app.post("/api/account/profile", userController.postUpdateProfile);
|
156 | app.post("/account/password", passportConfig.isAuthenticated, userController.postUpdatePassword);
|
157 | app.post("/account/delete", passportConfig.isAuthenticated, userController.postDeleteAccount);
|
158 | app.get("/account/unlink/:provider", passportConfig.isAuthenticated, userController.getOauthUnlink);
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 | app.use(errorHandler());
|
166 |
|
167 | app.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 |
|
173 | module.exports = app;
|