Skip to content
This repository was archived by the owner on Sep 1, 2022. It is now read-only.

Commit 29d7d99

Browse files
authored
Merge pull request #9 from BusHanyang/update-api
Update logic of api
2 parents e08eedc + de57520 commit 29d7d99

File tree

10 files changed

+157
-471
lines changed

10 files changed

+157
-471
lines changed

bin/www

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var app = require("../index")
2+
var http = require("http");
3+
4+
const PORT = 8080;
5+
6+
var server = http.createServer(app);
7+
8+
server.listen(PORT);
9+
10+
server.on("error", err => {
11+
if (error.syscall !== "listen") {
12+
throw error;
13+
}
14+
15+
switch (error.code) {
16+
case "EACCES":
17+
console.error(PORT + " requires elevated privileges");
18+
process.exit(1);
19+
break;
20+
case "EADDRINUSE":
21+
console.error(PORT + " is already in use");
22+
process.exit(1);
23+
break;
24+
default:
25+
throw error;
26+
}
27+
});
28+
server.on("listening", () => {
29+
console.log("Listening on " + PORT);
30+
});

common.js

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
const moment = require("moment");
22
const fs = require("fs");
33

4-
54
/*load setting value from json file*/
65
const data = JSON.parse(fs.readFileSync("./settings.json", "UTF-8"));
7-
const now = moment().utcOffset('+0900').format("YYYY-MM-DD");
6+
const now = moment()
7+
.utcOffset("+0900")
8+
.format("YYYY-MM-DD");
89

910
/*Functions about define datekind and daykind to make URL query string. */
1011
function getDateKind() {
@@ -15,7 +16,12 @@ function getDateKind() {
1516
if (moment(now).isBetween(semester.start, semester.end, null, [])) {
1617
return "semester";
1718
} else if (
18-
moment(now).isBetween(vacation_session.start, vacation_session.end, null, [])
19+
moment(now).isBetween(
20+
vacation_session.start,
21+
vacation_session.end,
22+
null,
23+
[]
24+
)
1925
) {
2026
return "vacation_session";
2127
} else if (moment(now).isBetween(vacation.start, vacation.end, null, [])) {
@@ -29,34 +35,35 @@ function getDayKind() {
2935
const holiday = data.calendar[3].holiday;
3036

3137
// console.log((moment().utcOffset('+0900').day()));
32-
let momentDay = moment().utcOffset('+0900').day();
33-
if(holiday.indexOf(now) !== -1){//holiday 배열에서 오늘날짜 값을 찾을 경우
38+
let momentDay = moment()
39+
.utcOffset("+0900")
40+
.day();
41+
if (holiday.indexOf(now) !== -1) {
42+
//holiday 배열에서 오늘날짜 값을 찾을 경우
3443
console.log("공휴일 보정");
35-
momentDay = 0;//일요일로 날짜 보정.
44+
momentDay = 0; //일요일로 날짜 보정.
3645
}
3746

3847
switch (momentDay) {
39-
case 0://sunday
48+
case 0: //sunday
4049
return "weekend";
41-
case 6://saturday
50+
case 6: //saturday
4251
return "weekend";
4352
default:
4453
return "week";
4554
}
46-
4755
}
4856

49-
function isHalt(){
50-
const haltList = data.calendar[4].halt;
51-
console.log(haltList);
52-
if(haltList.indexOf(now) !== -1){
53-
return "halted"
54-
} else {
55-
return ""
56-
}
57+
function isHalt() {
58+
const haltList = data.calendar[4].halt;
59+
console.log(haltList);
60+
if (haltList.indexOf(now) !== -1) {
61+
return "halted";
62+
} else {
63+
return "";
64+
}
5765
}
5866

59-
60-
module.exports.getDayKind = getDayKind
61-
module.exports.getDateKind = getDateKind
62-
module.exports.isHalt = isHalt
67+
module.exports.getDayKind = getDayKind;
68+
module.exports.getDateKind = getDateKind;
69+
module.exports.isHalt = isHalt;

index.js

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
const express = require("express");
2-
const http = require("http");
32
const app = express();
4-
const helmet = require('helmet')
3+
const helmet = require("helmet");
54

6-
app.use(helmet())
7-
app.use(helmet.noCache())
5+
app.use(helmet());
6+
app.use(helmet.noCache());
87

98
/*middle ware*/
109
app.use(express.static(__dirname + "/public"));
@@ -16,40 +15,7 @@ app.use(function(req, res, next) {
1615
});
1716

1817
/* router */
19-
/*학기중*/
20-
app.use(['/semester/week/giksa', '/semester/weekend/giksa', 'vacation/week/giksa', 'vacation/weekend/giksa', '/giksa'], require('./router/giksa'))
21-
app.use(['/semester/week/shuttlecock_i', '/semester/weekend/shuttlecock_i', 'vacation/week/shuttlecock_i', 'vacation/weekend/shuttlecock_i', '/shuttlecock_i'], require('./router/shuttlecock_i'))
22-
app.use(['/semester/week/shuttlecock_o', '/semester/weekend/shuttlecock_o', 'vacation/week/shuttlecock_o', 'vacation/weekend/shuttlecock_o', '/shuttlecock_o'], require('./router/shuttlecock_o'))
23-
app.use(['/semester/week/subway', '/semester/weekend/subway', 'vacation/week/subway', 'vacation/weekend/subway', '/subway'], require('./router/subway'))
24-
app.use(['/semester/week/yesulin', '/semester/weekend/yesulin', 'vacation/week/yesulin', 'vacation/weekend/yesulin', '/yesulin'], require('./router/yesulin'))
25-
26-
/*방학중 - 계절학기*/
27-
app.use(['/vacation_session/week/giksa', '/vacation_session/weekend/giksa', 'vacation/week/giksa', 'vacation/weekend/giksa'], require('./router/giksa'))
28-
app.use(['/vacation_session/week/shuttlecock_i', '/vacation_session/weekend/shuttlecock_i', 'vacation/week/shuttlecock_i', 'vacation/weekend/shuttlecock_i'], require('./router/shuttlecock_i'))
29-
app.use(['/vacation_session/week/shuttlecock_o', '/vacation_session/weekend/shuttlecock_o', 'vacation/week/shuttlecock_o', 'vacation/weekend/shuttlecock_o'], require('./router/shuttlecock_o'))
30-
app.use(['/vacation_session/week/subway', '/vacation_session/weekend/subway', 'vacation/week/subway', 'vacation/weekend/subway'], require('./router/subway'))
31-
app.use(['/vacation_session/week/yesulin', '/vacation_session/weekend/yesulin', 'vacation/week/yesulin', 'vacation/weekend/yesulin'], require('./router/yesulin'))
32-
33-
34-
/*방학중*/
35-
app.use(['/vacation/week/giksa', '/vacation/weekend/giksa', 'vacation/week/giksa', 'vacation/weekend/giksa'], require('./router/giksa'))
36-
app.use(['/vacation/week/shuttlecock_i', '/vacation/weekend/shuttlecock_i', 'vacation/week/shuttlecock_i', 'vacation/weekend/shuttlecock_i'], require('./router/shuttlecock_i'))
37-
app.use(['/vacation/week/shuttlecock_o', '/vacation/weekend/shuttlecock_o', 'vacation/week/shuttlecock_o', 'vacation/weekend/shuttlecock_o'], require('./router/shuttlecock_o'))
38-
app.use(['/vacation/week/subway', '/vacation/weekend/subway', 'vacation/week/subway', 'vacation/weekend/subway'], require('./router/subway'))
39-
app.use(['/vacation/week/yesulin', '/vacation/weekend/yesulin', 'vacation/week/yesulin', 'vacation/weekend/yesulin'], require('./router/yesulin'))
40-
41-
app.use((req, res, next) => {
42-
next(createError(404));
43-
});
44-
45-
app.use((err, req, res, next) => {
46-
res.status(404);
47-
res.json({ errorcode: "404" });
48-
});
49-
50-
/*server*/
51-
// Create an HTTP service.
52-
http.createServer(app).listen(8080);
53-
18+
var routes = require("./router/routes");
19+
routes(app);
5420

5521
module.exports = app;

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{
22
"name": "shuttlecock_API",
33
"version": "1.0.0",
4-
"start": "node index.js",
4+
"start": "node bin/www",
55
"scripts": {
6-
"start": "node index.js",
6+
"start": "node bin/www",
77
"test": "node ./node_modules/mocha/bin/mocha --recursive --exit ./**/*.spec.js"
88
},
99
"description": "Shuttlecock Timetable API",
10-
"main": "index.js",
11-
"repository": "https://github.com/CXZ7720/shuttlecock_API.git",
12-
"author": "Yeonggyun Kim <kygha7205@naver.com>",
13-
"license": "MIT",
10+
"main": "bin/www",
11+
"repository": "https://github.com/BusHanyang/ERICA_shuttlecock_API.git",
12+
"author": "BusHanyang <hybus.app@gmail.com>",
13+
"license": "GPL-3.0",
1414
"dependencies": {
1515
"body-parser": "^1.19.0",
1616
"express": "^4.17.1",

router/giksa.js

Lines changed: 0 additions & 82 deletions
This file was deleted.

router/routes.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
const util = require("../common");
2+
const fs = require("fs");
3+
const path = require("path");
4+
5+
/* router */
6+
module.exports = function(app) {
7+
/* full path */
8+
app.use("/:daykind/:isWeek/:where", Validation);
9+
10+
/* short path */
11+
app.use("/:where", halt, Validation);
12+
13+
/* error handle */
14+
app.use("*", (req, res, next) => {
15+
next({ errorcode: "404" });
16+
});
17+
18+
app.use((err, req, res, next) => {
19+
console.log(err);
20+
res.status(404);
21+
res.json(err);
22+
});
23+
};
24+
25+
function halt(req, res, next) {
26+
let isHalt = util.isHalt();
27+
if (isHalt == "halted") {
28+
res.status(200).json({ status: "Halt" });
29+
return false;
30+
} else {
31+
next();
32+
}
33+
}
34+
35+
function Validation(req, res, next) {
36+
let {
37+
daykind = util.getDateKind(),
38+
isWeek = util.getDayKind(),
39+
where
40+
} = req.params;
41+
42+
if (
43+
(daykind == "semester" ||
44+
daykind == "vacation" ||
45+
daykind == "vacation_session") &&
46+
(isWeek == "week" || isWeek == "weekend")
47+
) {
48+
sendResult(daykind, isWeek, where, res, next);
49+
} else {
50+
next({ error: "Incorrect column1" });
51+
}
52+
}
53+
54+
function sendResult(daykind, isWeek, where, res, next) {
55+
let pre;
56+
if (where == "giksa") {
57+
pre = "Residence_";
58+
} else if (where == "shuttlecock_i") {
59+
pre = "Shuttlecock_I_";
60+
} else if (where == "shuttlecock_o") {
61+
pre = "Shuttlecock_O_";
62+
} else if (where == "subway") {
63+
pre = "Subway_";
64+
} else if (where == "yesulin") {
65+
pre = "YesulIn_";
66+
} else {
67+
next({ error: "Incorrect column2" });
68+
}
69+
70+
fs.readFile(
71+
path.join(
72+
__dirname,
73+
"../timetable",
74+
daykind,
75+
isWeek,
76+
pre + isWeek + ".json"
77+
),
78+
(err, data) => {
79+
if (err) next({ error: "Incorrect column3" });
80+
try {
81+
data = JSON.parse(data);
82+
res.json(data[pre.toLowerCase() + isWeek]);
83+
} catch (err) {
84+
next({ error: "Incorrect column4" });
85+
}
86+
}
87+
);
88+
}

0 commit comments

Comments
 (0)