forked from cylab-tw/raccoon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
150 lines (134 loc) · 4.24 KB
/
routes.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
'use strict';
/**
* Created by Macy Gong.
*/
const path = require('path');
const mongodb = require('./models/mongodb');
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
const { pluginsConfig } = require("./plugins/config");
const _ = require("lodash"); // eslint-disable-line @typescript-eslint/naming-convention
module.exports = function (app) {
for (let pluginName in pluginsConfig) {
let plugin = pluginsConfig[pluginName];
if (plugin.before && plugin.enable) require(`plugins/${pluginName}`)(app);
}
//app.set('json spaces', 4);
app.get('/register', function (req, res) {
res.sendFile('register.html', {
root: __dirname + '/public/html'
});
});
app.post('/loging/getAccessToken', async function (req, res) {
let username = req.query.username;
let password = req.query.password;
let [authStatu ,user] = await myAuth(username ,password);
let statusMessage = {
"0" : "MongoDB Error" ,
"1" : "login success" ,
"2" : "invalid username or password" ,
"3" : "invalid username or password" ,
"4" : "the user is not active"
};
function authFailure() {
return res.json({
"code" : authStatu ,
"message": statusMessage[authStatu]
});
}
let statusFunc = {
"0" : authFailure ,
"1" : () => {
let token = jwt.sign({name:username} , "MicalaSecretSalt" , {expiresIn: '1d'});
user.token = token;
user.save(function (err) {
if (err) {
return res.send(err);
}
return res.json({
code : authStatu ,
message: "驗證成功!" ,
token : "Bearer " + token ,
username : username
});
});
} ,
"2" : authFailure ,
"3" : authFailure ,
"4" : authFailure
};
return statusFunc[authStatu]();
});
app.get('/checkIsLogin' , async function(req ,res) {
let islogin = await require('./api/Api_function').isTokenLogin(req ,res);
res.send(islogin);
});
app.get('/logout', async function (req, res) {
let user = _.get("req", "user.user");
await mongodb.users
.findOneAndUpdate({ account: user }, { $set: { token: "" } })
.exec();
req.logout();
res.redirect('/');
});
app.get('/api/profile', function (req, res) {
if (req.user) {
return res.send(req.user);
}
return res.send(null);
});
app.use('/api/dicom', require('api/dicom'));
//#region fhir
app.use('/api/fhir/metadata' , require('api/FHIR/metadata'));
app.use('/api/fhir/Organization' , require('api/FHIR/organization'));
app.use('/api/fhir/Patient', require('api/FHIR/patient'));
app.use('/api/fhir/Endpoint', require('api/FHIR/endpoint'));
app.use('/api/fhir/ImagingStudy', require('api/FHIR/ImagingStudy'));
//#endregion fhir
app.use('/dicom-web', require('api/dicom-web'));
app.use('/dicom-web', require('./api/dicom-web/stow'));
app.use('/' , require('./api/dicom-xml'));
//#region WEB
app.use('/dicom', require('web/dicom'));
app.use('/', require('web/index'));
//#endregion
app.route('/:url(api|auth|web)/*').get((req, res) => {
res.status(404).json({
status: 404,
message: "not found"
});
});
app.route('/favicon.ico').get((req, res) => {
res.send("");
});
for (let pluginName in pluginsConfig) {
let plugin = pluginsConfig[pluginName];
if(!plugin.before && plugin.enable) require(`plugins/${pluginName}`)(app);
}
};
async function myAuth(username, password) {
return new Promise((resolve) => {
mongodb.users.find({ account: username })
.exec((err, result) => {
if (err) {
resolve([0 , err]); //錯誤
}
else {
if (result.length > 0) {
if (bcrypt.compareSync(password, result[0].password) && result[0].status == 1) {
resolve([1 , result[0]]);//帳號密碼正確且開通
}
else if (bcrypt.compareSync(password, result[0].password) && result[0].status == 0) {
resolve([4 , '']); //無開通
}
else {
resolve([2 , '']); //密碼錯誤
}
}
else {
resolve([3 , '']); //無帳號
}
}
});
});
}