This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 259
/
vorlon.webServer.ts
172 lines (150 loc) · 6.92 KB
/
vorlon.webServer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import express = require("express");
import path = require("path");
import stylus = require("stylus");
import fs = require("fs");
import http = require("http");
import mkdirp = require("mkdirp");
//Vorlon
import iwsc = require("./vorlon.IWebServerComponent");
import vauth = require("./vorlon.authentication");
import vorloncontext = require("../config/vorlon.servercontext");
export module VORLON {
export class WebServer {
private _bodyParser = require("body-parser");
private _cookieParser = require("cookie-parser");
private _methodOverride = require("method-override");
private _session = require("express-session");
private _json = require("json");
private _multer = require("multer");
private _passport = require("passport");
private _localStrategy = require("passport-local");
private _twitterStrategy = require("passport-twitter");
// private _flash = require('connect-flash');
private _components: Array<iwsc.VORLON.IWebServerComponent>;
private _app: express.Express;
private _log: vorloncontext.VORLON.ILogger;
private _httpServer : http.Server;
private httpConfig: vorloncontext.VORLON.IHttpConfig;
private baseURLConfig: vorloncontext.VORLON.IBaseURLConfig;
constructor(context : vorloncontext.VORLON.IVorlonServerContext) {
this._app = express();
this._components = new Array<iwsc.VORLON.IWebServerComponent>();
this.httpConfig = context.httpConfig;
this.baseURLConfig = context.baseURLConfig;
this._log = context.logger;
}
public init(): void {
for (var id in this._components) {
var component = this._components[id];
component.addRoutes(this._app, this._passport);
}
}
public get components(): Array<iwsc.VORLON.IWebServerComponent> {
return this._components;
}
public set components(comp: Array<iwsc.VORLON.IWebServerComponent>) {
this._components = comp;
}
public start(): void {
var app = this._app;
//Command line
var stopExecution = false;
process.argv.forEach(function (val, index, array) {
switch (val) {
case "--version":
fs.readFile(path.join(__dirname, "../../package.json"), "utf8",(err, packageData) => {
if (err) {
this._log.error("Error reading package.json file");
return;
}
var _package = JSON.parse(packageData.replace(/^\uFEFF/, ''));
this._log.info('Vorlon.js v' + _package.version);
});
stopExecution = true;
break;
}
});
if (stopExecution) {
return;
}
var cors = require("cors");
//Sets
app.set('host', this.httpConfig.host);
app.set('port', this.httpConfig.port);
app.set('socket', this.httpConfig.socket);
app.set('views', path.join(__dirname, '../views'));
app.set('view engine', 'jade');
// Cors
var corsOptions = {
allowedHeaders: "*",
exposedHeaders: ["X-VorlonProxyEncoding", "Content-Encoding", "Content-Length"]
};
app.use(cors(corsOptions));
app.options('*', cors(corsOptions));
//Uses
this._passport.use(new this._localStrategy(function(username, password, done) {
// insert your MongoDB check here. For now, just a simple hardcoded check.
if (username === vauth.VORLON.Authentication.UserName && password === vauth.VORLON.Authentication.Password)
{
done(null, { user: username });
}
else
{
done(null, false);
}
})
);
this._passport.serializeUser(function(user, done) {
done(null, user);
});
this._passport.deserializeUser(function(user, done) {
done(null, user);
});
app.use(stylus.middleware(path.join(__dirname, '../public')));
app.use(this.baseURLConfig.baseURL, express.static(path.join(__dirname, '../public')));
app.use(this._bodyParser.urlencoded({ extended: false }));
app.use(this._bodyParser.json());
app.use(this._cookieParser());
app.set(this._multer());
app.use(this._methodOverride());
app.use(this._session({
secret: '1th3is4is3as2e5cr6ec7t7keyf23or1or5lon5',
expires: false,
saveUninitialized: true,
resave: true }));
app.use(this._passport.initialize());
app.use(this._passport.session());
vauth.VORLON.Authentication.loadAuthConfig(this.baseURLConfig.baseURL);
this.init();
if (this.httpConfig.socket) {
// In case we didn't shutdown gracefully last time
if (fs.existsSync(app.get('socket'))) {
fs.unlinkSync(app.get('socket'));
}
mkdirp.sync(path.dirname(app.get('socket')));
this._httpServer = this.httpConfig.httpModule.createServer(app).listen(
app.get('socket'), () => {
fs.chmodSync(app.get('socket'), '700'); // Socket is user only for security
this._log.info('Vorlon.js SERVER listening on socket ' + app.get('socket'));
});
} else if (this.httpConfig.useSSL) {
this._httpServer = this.httpConfig.httpModule.createServer(this.httpConfig.options, app).listen(
app.get('port'), app.get('host'), undefined, () => {
this._log.info('Vorlon.js SERVER with SSL listening at ' + app.get('host') + ':' + app.get('port'));
});
} else {
this._httpServer = this.httpConfig.httpModule.createServer(app).listen(
app.get('port'), app.get('host'), undefined, () => {
this._log.info('Vorlon.js SERVER listening at ' + app.get('host') + ':' + app.get('port'));
});
}
for (var id in this._components) {
var component = this._components[id];
component.start(this._httpServer);
}
}
public get httpServer(){
return this._httpServer;
}
}
}