-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
206 lines (174 loc) · 5.58 KB
/
server.js
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
var package = require('./package.json');
var compression = require('compression');
var path = require('path');
var express = require('express');
var cors = require('cors');
var app = express();
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var url = require("url");
var colors = require('colors');
var passport = require('passport');
var orcinusd = require('orcinusd');
var db = require("./db");
var jwt = require('jsonwebtoken');
var authMW = express.Router();
module.exports = function(){
/*
Environment :
ORCINUS_HOST=<hostname>
ORCINUS_PORT=<port>
ORCINUS_HTTP_CORS= example : http://domain1.com, http://domain2.com or *
*/
var DEPLOYMENT = process.env.ORCINUS || "dev";
var PORT = process.env.ORCINUS_PORT || 4000;
var HOST = process.env.ORCINUS_HOST || "0.0.0.0";
var CORS = process.env.ORCINUS_HTTP_CORS || false;
var SOCK = process.env.ORCINUS_DOCKER_SOCKET || "/var/run/docker.sock";
var DBHOST = process.env.ORCINUS_DB || "orcinus-db/orcinus";
var OMURA = process.env.ORCINUS_OMURA || "omura.orcinus.id";
if(!process.env.ORCINUS_SECRET){
process.env.ORCINUS_SECRET = "orcinus"
}
var SECRET = process.env.ORCINUS_SECRET;
if(DEPLOYMENT === 'prod'){
process.env.NODE_ENV = production;
}
if(!process.env.ORCINUS_DOMAIN){
process.env.ORCINUS_DOMAIN = "svc.orcinus.id"
}
var ENDPOINT = process.env.ORCINUS_DOMAIN;
if(!process.env.ORCINUS_NETWORK){
process.env.ORCINUS_NETWORK = "orcinus"
}
var NETWORK = process.env.ORCINUS_NETWORK;
var ping = require("./apis/ping");
var info = require("./apis/info");
var cluster = require("./apis/cluster");
var service = require("./apis/service");
var stack = require("./apis/stack");
var task = require("./apis/task");
var volume = require("./apis/volume");
var container = require("./apis/container");
var auth = require("./apis/auth");
/*
* Database connection
*/
app.locals.orcinusdb = db(DBHOST);
db.users();
var corsOpt = {
"origin": false,
"methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
"preflightContinue": false,
"optionsSuccessStatus": 204,
"allowedHeaders": ["Origin", "X-Requested-With", "Content-Type", "Accept", "Authorization", "x-access-token"]
}
if(CORS){
console.log(colors.yellow("==> HTTP CORS Active!"));
corsOpt = {
"origin": CORS,
"methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
"preflightContinue": false,
"optionsSuccessStatus": 204,
"allowedHeaders": ["Origin", "X-Requested-With", "Content-Type", "Accept", "Authorization", "x-access-token"]
}
}
app.options('*', cors(corsOpt))
if(SOCK.indexOf("http") >= 0 || SOCK.indexOf("https") >= 0){
var sockParse = url.parse(SOCK);
var proto = sockParse.protocol.replace(":","");
var host = sockParse.hostname;
var port = sockParse.port;
SOCK = {protocol: proto, host: host, port: port};
}
else{
SOCK = { socketPath: SOCK };
}
if(DEPLOYMENT === 'dev'){
app.use(logger('dev'));
}
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(compression());
app.locals.orcinus = new orcinusd(SOCK);
app.locals.secret = SECRET;
app.use(express.static(path.join(__dirname, './www')));
app.use(passport.initialize());
app.get('/',function(req,res){
res.sendFile(path.join(__dirname, './www', 'index.html'));
});
/*
* Authentication
*/
// midleware
authMW.use(cors(corsOpt),function(req, res, next) {
// check header or url parameters or post parameters for token
var token = req.body.token || req.query.token || req.headers['x-access-token'];
// decode token
if (token) {
// verifies secret and checks exp
jwt.verify(token, SECRET, function(err, decoded) {
if (err) {
return res.status(403).json({ success: false, message: 'Failed to authenticate token.' });
} else {
// if everything is good, save to request for use in other routes
req.decoded = decoded;
next();
}
});
} else {
// if there is no token
// return an error
return res.status(403).send({
success: false,
message: 'No token provided.'
});
}
});
app.use('/apis/auth', cors(corsOpt), auth);
/*
* INFO
*/
app.use('/apis/info', cors(corsOpt), function(req, res, next) {
var info = {
name: package.name,
version: package.version,
description: package.description,
bugs: package.bugs,
repository: OMURA,
cors: corsOpt.origin,
deployment: DEPLOYMENT,
endpoint: ENDPOINT
}
res.send(info);
});
/*
* Apis router
*/
app.use('/apis/ping', authMW, ping);
app.use('/apis/info', authMW, info);
app.use('/apis/cluster', authMW, cluster);
app.use('/apis/service', authMW, service);
app.use('/apis/stack', authMW, stack);
app.use('/apis/task', authMW, task);
app.use('/apis/volume', authMW, volume);
app.use('/apis/container', authMW, container);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
res.sendFile(path.join(__dirname, './www', 'index.html'));
});
app.listen(parseInt(PORT), HOST, function(error) {
if (error) {
console.error(error);
} else {
console.info(colors.green("==> Listening on port %s. Visit http://%s:%s/ in your browser."), PORT,HOST, PORT);
}
});
return app;
}