Skip to content

Commit

Permalink
add swagger-ui for debug and view apidoc
Browse files Browse the repository at this point in the history
  • Loading branch information
kanreisa committed Mar 10, 2017
1 parent d124a5a commit 15da41a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -65,6 +65,7 @@
"munin-plugin": "0.0.9",
"promise-queue": "^2.2.3",
"sift": "^3.2.6",
"swagger-ui": "^2.2.10",
"tail": "^1.2.1"
},
"devDependencies": {
Expand Down
25 changes: 20 additions & 5 deletions src/Mirakurun/Server.ts
Expand Up @@ -17,6 +17,7 @@

import * as fs from "fs";
import * as http from "http";
import * as url from "url";
import * as ip from "ip";
import * as express from "express";
import * as openapi from "express-openapi";
Expand Down Expand Up @@ -70,19 +71,33 @@ class Server {

app.use((req: express.Request, res: express.Response, next) => {

if (
(req.ip && ip.isPrivate(req.ip) === false) ||
req.get("Origin") !== undefined ||
req.get("Referer") !== undefined
) {
if (req.ip && ip.isPrivate(req.ip) === false) {
req.socket.end();
return;
}

if (req.get("Origin") !== undefined) {
res.status(403).end();
return;
}

if (req.get("Referer") !== undefined) {
const referer = url.parse(req.get("Referer"));
if (ip.isPrivate(referer.hostname) === false) {
res.status(403).end();
return;
}
}

res.setHeader("Server", "Mirakurun/" + pkg.version);
next();
});

if (fs.existsSync("node_modules/swagger-ui/dist") === true) {
app.use("/swagger-ui", express.static("node_modules/swagger-ui/dist"));
app.get("/api/debug", (req, res) => res.redirect("/swagger-ui/?url=/api/docs"));
}

const api = yaml.safeLoad(fs.readFileSync("api.yml", "utf8"));
api.info.version = pkg.version;

Expand Down

0 comments on commit 15da41a

Please sign in to comment.