-
Notifications
You must be signed in to change notification settings - Fork 9
/
api.js
executable file
·133 lines (107 loc) · 2.71 KB
/
api.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
//var mongoose = require('mongoose');
var express = require('express');
var bodyParser = require('body-parser');
//var session = require('express-session');
//var MongoStore = require('connect-mongo')(session);
var dpanel = require('./lib/dpanel.js');
var project = 'dpanel';
var port = 9999;
var app = express();
app.use(bodyParser.urlencoded({extended:false}))
/*app.use(session({
secret: 'jujubee',
store: new MongoStore({
db: project
}),
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 365
}
}));*/
/*app.use( function(req,res,next){
var session = req.session;
if(session.user){
req.user = session.user
}
next();
});*/
app.get('/user', function(req,res){
res.send(req.user);
});
app.post('/user/login', function(req,res){
res.send(req.user);
});
//app.use(isAdmin);
app.get('/list', function(req,res){
dpanel.list().then( function(containers) {
res.send(containers);
});
});
app.post('/stop/:domain', function(req,res){
var domain = req.params.domain;
dpanel.stop(domain).then( function(){
res.send(['stopped',req.params]);
}).fail( function(err){
res.send(['error',err]);
});
});
app.post('/delete/:domain', function(req,res){
var domain = req.params.domain;
dpanel.delete(domain).then( function(){
res.send(['deleted',req.params]);
}).fail( function(err){
res.send(['error',err]);
})
})
app.post('/start/:domain/:repo_user/:image', startDomain);
app.post('/start/:domain/:image?', startDomain);
app.listen(port, console.log);
function startDomain(req,res){
var domain = req.params.domain;
var repo_user = req.params.repo_user;
var image = req.params.image;
if(repo_user){
image = repo_user + '/' + image;
}
//some defaults
switch(image){
case 'wordpress':
image = 'oskarhane/docker-wordpress-nginx-ssh';
break;
}
if(!image){
image = 'oskarhane/docker-wordpress-nginx-ssh';
}
console.log(image);
dpanel.start(domain,image).then( function(container){
res.send(
['started', container, req.params]
);
}).fail( function(container){
res.send(
['error', container, req.params]
);
});
}
function testing(){
var testings = 'testing';
return function testing(req,res,next){
console.log(testings);
next();
}
}
function authenticate(req,res,next){
req.user = req.session.user = {
'username': 'test',
'level': 'admin'
}
next();
}
function isAdmin(req,res,next) {
try {
if (req.user.level == 'admin') {
next();
}
} catch (e) {
res.send('unauthorized');
}
}