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

Commit

Permalink
Merge pull request #9 from BusHanyang/update-api
Browse files Browse the repository at this point in the history
Update logic of api
  • Loading branch information
CXZ7720 authored Apr 5, 2020
2 parents e08eedc + de57520 commit 29d7d99
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 471 deletions.
30 changes: 30 additions & 0 deletions bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var app = require("../index")
var http = require("http");

const PORT = 8080;

var server = http.createServer(app);

server.listen(PORT);

server.on("error", err => {
if (error.syscall !== "listen") {
throw error;
}

switch (error.code) {
case "EACCES":
console.error(PORT + " requires elevated privileges");
process.exit(1);
break;
case "EADDRINUSE":
console.error(PORT + " is already in use");
process.exit(1);
break;
default:
throw error;
}
});
server.on("listening", () => {
console.log("Listening on " + PORT);
});
49 changes: 28 additions & 21 deletions common.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const moment = require("moment");
const fs = require("fs");


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

/*Functions about define datekind and daykind to make URL query string. */
function getDateKind() {
Expand All @@ -15,7 +16,12 @@ function getDateKind() {
if (moment(now).isBetween(semester.start, semester.end, null, [])) {
return "semester";
} else if (
moment(now).isBetween(vacation_session.start, vacation_session.end, null, [])
moment(now).isBetween(
vacation_session.start,
vacation_session.end,
null,
[]
)
) {
return "vacation_session";
} else if (moment(now).isBetween(vacation.start, vacation.end, null, [])) {
Expand All @@ -29,34 +35,35 @@ function getDayKind() {
const holiday = data.calendar[3].holiday;

// console.log((moment().utcOffset('+0900').day()));
let momentDay = moment().utcOffset('+0900').day();
if(holiday.indexOf(now) !== -1){//holiday 배열에서 오늘날짜 값을 찾을 경우
let momentDay = moment()
.utcOffset("+0900")
.day();
if (holiday.indexOf(now) !== -1) {
//holiday 배열에서 오늘날짜 값을 찾을 경우
console.log("공휴일 보정");
momentDay = 0;//일요일로 날짜 보정.
momentDay = 0; //일요일로 날짜 보정.
}

switch (momentDay) {
case 0://sunday
case 0: //sunday
return "weekend";
case 6://saturday
case 6: //saturday
return "weekend";
default:
return "week";
}

}

function isHalt(){
const haltList = data.calendar[4].halt;
console.log(haltList);
if(haltList.indexOf(now) !== -1){
return "halted"
} else {
return ""
}
function isHalt() {
const haltList = data.calendar[4].halt;
console.log(haltList);
if (haltList.indexOf(now) !== -1) {
return "halted";
} else {
return "";
}
}


module.exports.getDayKind = getDayKind
module.exports.getDateKind = getDateKind
module.exports.isHalt = isHalt
module.exports.getDayKind = getDayKind;
module.exports.getDateKind = getDateKind;
module.exports.isHalt = isHalt;
44 changes: 5 additions & 39 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const express = require("express");
const http = require("http");
const app = express();
const helmet = require('helmet')
const helmet = require("helmet");

app.use(helmet())
app.use(helmet.noCache())
app.use(helmet());
app.use(helmet.noCache());

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

/* router */
/*학기중*/
app.use(['/semester/week/giksa', '/semester/weekend/giksa', 'vacation/week/giksa', 'vacation/weekend/giksa', '/giksa'], require('./router/giksa'))
app.use(['/semester/week/shuttlecock_i', '/semester/weekend/shuttlecock_i', 'vacation/week/shuttlecock_i', 'vacation/weekend/shuttlecock_i', '/shuttlecock_i'], require('./router/shuttlecock_i'))
app.use(['/semester/week/shuttlecock_o', '/semester/weekend/shuttlecock_o', 'vacation/week/shuttlecock_o', 'vacation/weekend/shuttlecock_o', '/shuttlecock_o'], require('./router/shuttlecock_o'))
app.use(['/semester/week/subway', '/semester/weekend/subway', 'vacation/week/subway', 'vacation/weekend/subway', '/subway'], require('./router/subway'))
app.use(['/semester/week/yesulin', '/semester/weekend/yesulin', 'vacation/week/yesulin', 'vacation/weekend/yesulin', '/yesulin'], require('./router/yesulin'))

/*방학중 - 계절학기*/
app.use(['/vacation_session/week/giksa', '/vacation_session/weekend/giksa', 'vacation/week/giksa', 'vacation/weekend/giksa'], require('./router/giksa'))
app.use(['/vacation_session/week/shuttlecock_i', '/vacation_session/weekend/shuttlecock_i', 'vacation/week/shuttlecock_i', 'vacation/weekend/shuttlecock_i'], require('./router/shuttlecock_i'))
app.use(['/vacation_session/week/shuttlecock_o', '/vacation_session/weekend/shuttlecock_o', 'vacation/week/shuttlecock_o', 'vacation/weekend/shuttlecock_o'], require('./router/shuttlecock_o'))
app.use(['/vacation_session/week/subway', '/vacation_session/weekend/subway', 'vacation/week/subway', 'vacation/weekend/subway'], require('./router/subway'))
app.use(['/vacation_session/week/yesulin', '/vacation_session/weekend/yesulin', 'vacation/week/yesulin', 'vacation/weekend/yesulin'], require('./router/yesulin'))


/*방학중*/
app.use(['/vacation/week/giksa', '/vacation/weekend/giksa', 'vacation/week/giksa', 'vacation/weekend/giksa'], require('./router/giksa'))
app.use(['/vacation/week/shuttlecock_i', '/vacation/weekend/shuttlecock_i', 'vacation/week/shuttlecock_i', 'vacation/weekend/shuttlecock_i'], require('./router/shuttlecock_i'))
app.use(['/vacation/week/shuttlecock_o', '/vacation/weekend/shuttlecock_o', 'vacation/week/shuttlecock_o', 'vacation/weekend/shuttlecock_o'], require('./router/shuttlecock_o'))
app.use(['/vacation/week/subway', '/vacation/weekend/subway', 'vacation/week/subway', 'vacation/weekend/subway'], require('./router/subway'))
app.use(['/vacation/week/yesulin', '/vacation/weekend/yesulin', 'vacation/week/yesulin', 'vacation/weekend/yesulin'], require('./router/yesulin'))

app.use((req, res, next) => {
next(createError(404));
});

app.use((err, req, res, next) => {
res.status(404);
res.json({ errorcode: "404" });
});

/*server*/
// Create an HTTP service.
http.createServer(app).listen(8080);

var routes = require("./router/routes");
routes(app);

module.exports = app;
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"name": "shuttlecock_API",
"version": "1.0.0",
"start": "node index.js",
"start": "node bin/www",
"scripts": {
"start": "node index.js",
"start": "node bin/www",
"test": "node ./node_modules/mocha/bin/mocha --recursive --exit ./**/*.spec.js"
},
"description": "Shuttlecock Timetable API",
"main": "index.js",
"repository": "https://github.com/CXZ7720/shuttlecock_API.git",
"author": "Yeonggyun Kim <kygha7205@naver.com>",
"license": "MIT",
"main": "bin/www",
"repository": "https://github.com/BusHanyang/ERICA_shuttlecock_API.git",
"author": "BusHanyang <hybus.app@gmail.com>",
"license": "GPL-3.0",
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.17.1",
Expand Down
82 changes: 0 additions & 82 deletions router/giksa.js

This file was deleted.

88 changes: 88 additions & 0 deletions router/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const util = require("../common");
const fs = require("fs");
const path = require("path");

/* router */
module.exports = function(app) {
/* full path */
app.use("/:daykind/:isWeek/:where", Validation);

/* short path */
app.use("/:where", halt, Validation);

/* error handle */
app.use("*", (req, res, next) => {
next({ errorcode: "404" });
});

app.use((err, req, res, next) => {
console.log(err);
res.status(404);
res.json(err);
});
};

function halt(req, res, next) {
let isHalt = util.isHalt();
if (isHalt == "halted") {
res.status(200).json({ status: "Halt" });
return false;
} else {
next();
}
}

function Validation(req, res, next) {
let {
daykind = util.getDateKind(),
isWeek = util.getDayKind(),
where
} = req.params;

if (
(daykind == "semester" ||
daykind == "vacation" ||
daykind == "vacation_session") &&
(isWeek == "week" || isWeek == "weekend")
) {
sendResult(daykind, isWeek, where, res, next);
} else {
next({ error: "Incorrect column1" });
}
}

function sendResult(daykind, isWeek, where, res, next) {
let pre;
if (where == "giksa") {
pre = "Residence_";
} else if (where == "shuttlecock_i") {
pre = "Shuttlecock_I_";
} else if (where == "shuttlecock_o") {
pre = "Shuttlecock_O_";
} else if (where == "subway") {
pre = "Subway_";
} else if (where == "yesulin") {
pre = "YesulIn_";
} else {
next({ error: "Incorrect column2" });
}

fs.readFile(
path.join(
__dirname,
"../timetable",
daykind,
isWeek,
pre + isWeek + ".json"
),
(err, data) => {
if (err) next({ error: "Incorrect column3" });
try {
data = JSON.parse(data);
res.json(data[pre.toLowerCase() + isWeek]);
} catch (err) {
next({ error: "Incorrect column4" });
}
}
);
}
Loading

0 comments on commit 29d7d99

Please sign in to comment.