-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.ts
More file actions
166 lines (152 loc) · 5.51 KB
/
server.ts
File metadata and controls
166 lines (152 loc) · 5.51 KB
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
import Winston = require('winston');
import fs = require('fs');
import path = require('path');
import opn = require('opn');
import auth = require('basic-auth');
import * as csweb from "csweb";
Winston.remove(Winston.transports.Console);
Winston.add(Winston.transports.Console, <Winston.ConsoleTransportOptions>{
colorize: true,
label: 'csWeb',
prettyPrint: true
});
var cs = new csweb.csServer(__dirname, <csweb.csServerOptions>{
port: 3004,
swagger: false,
connectors: { }
});
var debug = true;
var passwords = {};
cs.start(() => {
readPass();
this.config = cs.config;
this.config.add('server', 'http://localhost:' + cs.options.port);
var bagDatabase = new csweb.BagDatabase(this.config);
var mapLayerFactory = new csweb.MapLayerFactory(<any>bagDatabase, cs.messageBus, cs.api, cs.dir);
cs.server.post('/projecttemplate', (req, res) => {
var creds = auth(req);
if (!creds || !passwords.hasOwnProperty(creds.name) || creds.pass !== passwords[creds.name]) {
console.log('Wrong password');
res.statusCode = 401;
res.end();
} else {
mapLayerFactory.process(req, res);
}
});
cs.server.post('/requestproject', (req, res) => {
var project = new csweb.Project;
project = req.body;
cs.api.addProject(project, {}, (result: csweb.CallbackResult) => {
if (result.result === csweb.ApiResult.OK) {
if (result.project.hasOwnProperty('id') && !passwords.hasOwnProperty(result.project.id)) {
passwords[result.project.id] = generatePass();
addPass(result.project.id + ':' + passwords[result.project.id]);
result.project['password'] = passwords[result.project.id];
} else {
console.log('Password already exists');
}
} else {
console.log('ID already exists');
}
res.statusCode = result.result;
res.send(result.project);
});
});
cs.server.post('/updategrouptitle', (req, res) => {
var creds = auth(req);
if (!creds || !passwords.hasOwnProperty(creds.name) || creds.pass !== passwords[creds.name]) {
console.log('Wrong password');
res.statusCode = 401;
res.end();
} else {
var data;
if (req.body) {
data = req.body;
cs.api.updateGroup(data.projectId, data.oldTitle, <any>{ id: data.newTitle, title: data.newTitle }, {}, (result: csweb.CallbackResult) => {
if (result && result.result === csweb.ApiResult.OK) {
res.statusCode = 200;
res.end();
} else {
res.statusCode = 404;
res.end();
}
});
}
}
});
cs.server.post('/clearproject', (req, res) => {
var creds = auth(req);
if (!creds || !passwords.hasOwnProperty(creds.name) || creds.pass !== passwords[creds.name]) {
console.log('Wrong password');
res.statusCode = 401;
res.end();
} else {
var data;
if (req.body) {
data = req.body;
if (!data.hasOwnProperty('projectId')) {
res.statusCode = 404;
res.end();
} else {
cs.api.clearProject(data.projectId, {}, (result: csweb.CallbackResult) => {
if (result && result.result === csweb.ApiResult.OK) {
res.statusCode = 200;
res.end();
} else {
res.statusCode = 404;
res.end();
}
});
}
}
}
});
cs.server.post('/bagsearchaddress', (req, res) => {
mapLayerFactory.processBagSearchQuery(req, res);
});
console.log('Excel2map functions started');
// Open start webpage
if (!debug) opn('http://localhost:' + cs.options.port);
});
function generatePass() {
var s: string = (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
return 'p' + s + (Date.now() % 9);
};
function readPass() {
var pwfile = path.join(__dirname, '.users.htpasswd');
fs.exists(pwfile, (exists) => {
if (exists) {
fs.readFile(pwfile, 'utf8', (err, data) => {
if (err) {
console.log('Error reading htpasswds');
return;
}
var entries = data.split('\n');
entries.forEach((e) => {
var un_pw = e.split(':');
passwords[un_pw[0]] = un_pw[1];
});
});
}
});
}
function addPass(entry: string) {
var pwfile = path.join(__dirname, '.users.htpasswd');
fs.exists(pwfile, (exists) => {
if (exists) {
fs.appendFile(pwfile, '\n' + entry, { encoding: 'utf8' }, (err) => {
if (err) {
console.log('Error adding htpasswd');
return;
}
});
} else {
fs.writeFile(pwfile, entry, { encoding: 'utf8' }, (err) => {
if (err) {
console.log('Error writing htpasswd file');
return;
}
});
}
});
}