Skip to content

Commit 7f2d78a

Browse files
author
Kao
authored
Merge pull request Samsung#187 from VeronikaSanko/master
Add Dexter-server Swagger documentation
2 parents 9879e15 + fc7962c commit 7f2d78a

File tree

7 files changed

+77
-35
lines changed

7 files changed

+77
-35
lines changed

README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,15 @@ i.e. get rid of all the development changes.
239239

240240
</details>
241241

242+
## How to open API documentation in Swagger
243+
<details>
244+
<summary>
245+
You can use Swagger to know more about Dexter API
246+
</summary>
247+
Web page with Dexter API documentation in Swagger will be available during Dexter server running on the same host and post as Dexter server.
248+
You can find it under [host]:[port]/api/api-docs address.
249+
</details>
250+
242251
# Future plans
243252
<ul>
244253
<li>Supporting more languages
@@ -283,4 +292,4 @@ Dexter is fully open source and BSD-licensed. See [the full license](../master/L
283292
<img src="../master/wiki/images/DevelopedBySamsung.svg" height="auto" width="50%" title="Not loading? Click to see full size!"/>
284293
</p>
285294
<br>
286-
<br>
295+
<br>

project/dexter-cs/DexterCS/Src/AnalysisEntityFactory.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#region Copyright notice
1+
#region Copyright notice
22
/**
33
* Copyright (c) 2018 Samsung Electronics, Inc.,
44
* All rights reserved.
@@ -25,6 +25,7 @@
2525
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2626
*/
2727
#endregion
28+
2829
using System.Collections.Generic;
2930

3031
namespace DexterCS
@@ -75,4 +76,4 @@ public AnalysisResult CreateAnalysisResultList(List<AnalysisResult> resultList)
7576
}
7677

7778
}
78-
}
79+
}

project/dexter-cs/DexterCS/Src/CLIAnalysisResultHandler.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#region Copyright notice
1+
#region Copyright notice
22
/**
33
* Copyright (c) 2018 Samsung Electronics, Inc.,
44
* All rights reserved.
@@ -26,6 +26,7 @@
2626
*/
2727
#endregion
2828
using DexterCS.Client;
29+
using DexterCS.Client;
2930
using DexterCS.Job;
3031
using log4net;
3132
using System;
@@ -42,7 +43,6 @@ public class CLIAnalysisResultHandler : IAnalysisResultHandler
4243
private string dexterWebUrl;
4344

4445
private ICLIResultFile cliResultFile = new CLIResultFile();
45-
4646
private int totalCnt = 0;
4747
private int totalOccurenceCnt = 0;
4848
private int criticalCnt = 0;
@@ -225,4 +225,4 @@ private void WriteResultFile(List<Defect> allDefectList, string sourceFileFullPa
225225
}
226226
}
227227
}
228-
}
228+
}

project/dexter-cs/DexterDepend/Src/DexterDependPlugin.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ public void Dispose()
130130
{
131131
//TODO:
132132
}
133-
134133
public void Init()
135134
{
136135
InitCheckerConfig();
@@ -187,4 +186,4 @@ public bool SupportLanguage(DexterConfig.LANGUAGE language)
187186
}
188187

189188
}
190-
}
189+
}

project/dexter-server/package.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@
88
"dependencies": {
99
"angular": "^1.5.8",
1010
"bluebird": "^3.3.0",
11-
"express": "3.5.1",
11+
"body-parser": "1.9.x",
12+
"errorhandler": "^1.5.0",
13+
"express": "4.16.4",
1214
"json-bigint": "^0.1.4",
1315
"line-reader": "^0.2.4",
1416
"lodash": "^4.0.0",
17+
"method-override": "^3.0.0",
18+
"minimist": "*",
1519
"moment": "^2.5.1",
1620
"mysql": "^2.11.1",
1721
"q": "^1.4.1",
1822
"request": "^2.67.0",
23+
"swagger-node-express": "^2.0.3",
24+
"swagger-ui-express": "^4.0.1",
1925
"underscore": "^1.8.3",
2026
"winston": "^0.8.3"
2127
},

project/dexter-server/server.js

+33-23
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
var express = require('express');
2828
var path = require('path');
2929
var http = require('http');
30+
var methodOverride = require('method-override');
31+
var errorhandler = require('errorhandler');
3032

3133
var database = require("./util/database");
3234
var util = require('./util/dexter-util');
@@ -40,7 +42,9 @@ var codeMetrics = require("./routes/codeMetrics");
4042
var functionMetrics = require("./routes/functionMetrics");
4143
var monitor = require("./routes/monitor");
4244
var adminSE = require("./routes/adminSE");
43-
45+
var methodOverride = require('method-override');
46+
var swaggerUi = require('swagger-ui-express');
47+
var swaggerDocument = require('./api-docs.json');
4448
var app = express();
4549

4650
global.runOptions = {
@@ -85,6 +89,7 @@ function initialize(){
8589
setExecutionMode();
8690
setAppConfigure();
8791
initModules();
92+
initSwagger();
8893
initRestAPI();
8994
startServer();
9095
}
@@ -93,13 +98,13 @@ function setRunOptionsByCliOptions(){
9398
var cliOptions = util.getCliOptions();
9499

95100
global.runOptions.port = cliOptions.getValue('p', 4982);
96-
global.runOptions.databaseHost = cliOptions.getValue('database.host', 'localhost');
101+
global.runOptions.databaseHost = cliOptions.getValue('database.host', 'dexter-test');
97102
global.runOptions.databasePort = cliOptions.getValue('database.port', 3306);
98-
global.runOptions.databaseUser = cliOptions.getValue('database.user', '');
99-
global.runOptions.databasePassword = cliOptions.getValue('database.password', '');
103+
global.runOptions.databaseUser = cliOptions.getValue('database.user', 'root');
104+
global.runOptions.databasePassword = cliOptions.getValue('database.password', 'gre4d');
100105
global.runOptions.databaseAdminUser = cliOptions.getValue('database.admin.user', '');
101106
global.runOptions.databaseAdminPassword = cliOptions.getValue('database.admin.password', '');
102-
global.runOptions.databaseName = cliOptions.getValue('database.name', '');
107+
global.runOptions.databaseName = cliOptions.getValue('database.name', 'my_dexter_db');
103108
global.runOptions.serverName = cliOptions.getValue('server.name', 'dexter-server-default');
104109
global.runOptions.serverIP = util.getLocalIPAddress();
105110
}
@@ -111,24 +116,22 @@ function setExecutionMode(){
111116
}
112117

113118
function setAppConfigure(){
114-
app.configure(function () {
115-
app.set("jsonp callback", true);
116-
app.set('views', path.join(__dirname, 'views'));
117-
app.set('view engine', 'jade');
118-
app.use(express.static(path.join(__dirname, 'public')));
119-
app.use(express.json({limit:'300mb'}));
120-
app.use(express.urlencoded());
121-
app.use(express.methodOverride());
122-
});
123-
124-
app.configure('development', function(){
125-
app.use(express.errorHandler({ dumpExceptions: true, showStack: true}));
126-
});
127-
128-
129-
app.configure('production', function(){
130-
app.use(express.errorHandler({"dumpExceptions": false, "showStack": false}));
131-
});
119+
120+
app.set("jsonp callback", true);
121+
app.set('views', path.join(__dirname, 'views'));
122+
app.set('view engine', 'jade');
123+
app.use(express.static(path.join(__dirname, 'public')));
124+
app.use(express.json({limit:'300mb'}));
125+
app.use(express.urlencoded());
126+
app.use(methodOverride());
127+
128+
if (process.env.NODE_ENV === 'development') {
129+
app.use(errorhandler({ dumpExceptions: true, showStack: true}));
130+
};
131+
132+
if (process.env.NODE_ENV === 'production') {
133+
app.use(errorhandler({"dumpExceptions": false, "showStack": false}));
134+
};
132135

133136

134137
app.all('*', function(req, res, next){
@@ -140,6 +143,7 @@ function setAppConfigure(){
140143
});
141144
}
142145

146+
143147
function initModules(){
144148
log.init();
145149
database.init();
@@ -167,6 +171,12 @@ function addAccessLog(req){
167171
config.addAccessLog(parameter);
168172
}
169173

174+
function initSwagger() {
175+
log.info("Setting up Swagger");
176+
app.use('/api/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
177+
178+
}
179+
170180
function isNoNeedToAddAccessLog(url){
171181
return noNeedAccessLogUriList.indexOf(url) >= 0;
172182
}

project/dexter-server/util/auth-util.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@
2626
var express = require('express');
2727
var account = require("../routes/account");
2828

29-
exports.getBasicAuth = express.basicAuth(function(user, pass){
30-
return account.checkAccount(user, pass);
31-
});
29+
exports.getBasicAuth = function(req, res, next) {
30+
var user, pass;
31+
32+
if (req.headers.authorization) {
33+
var auth = new Buffer(req.headers.authorization.substring(6), 'base64').toString().split(':');
34+
35+
if (auth) {
36+
user = auth[0];
37+
pass = auth[1];
38+
}
39+
}
40+
41+
if (! account.checkAccount(user, pass)) {
42+
res.statusCode = 401;
43+
res.setHeader('WWW-Authenticate' , 'Basic realm="Dexter"');
44+
res.end('Unauthorized');
45+
} else {
46+
next();
47+
}
48+
};

0 commit comments

Comments
 (0)