diff --git a/bin/www b/bin/www new file mode 100644 index 0000000..ad67ecc --- /dev/null +++ b/bin/www @@ -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); +}); diff --git a/common.js b/common.js index 5a625e7..dbf6634 100644 --- a/common.js +++ b/common.js @@ -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() { @@ -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, [])) { @@ -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 \ No newline at end of file +module.exports.getDayKind = getDayKind; +module.exports.getDateKind = getDateKind; +module.exports.isHalt = isHalt; diff --git a/index.js b/index.js index fc9c2e6..d67da32 100644 --- a/index.js +++ b/index.js @@ -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")); @@ -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; diff --git a/package.json b/package.json index dec8765..acf461b 100644 --- a/package.json +++ b/package.json @@ -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 ", - "license": "MIT", + "main": "bin/www", + "repository": "https://github.com/BusHanyang/ERICA_shuttlecock_API.git", + "author": "BusHanyang ", + "license": "GPL-3.0", "dependencies": { "body-parser": "^1.19.0", "express": "^4.17.1", diff --git a/router/giksa.js b/router/giksa.js deleted file mode 100644 index 8d87595..0000000 --- a/router/giksa.js +++ /dev/null @@ -1,82 +0,0 @@ -const express = require('express') -const path = require('path') -const router = express.Router() -const fs = require('fs') -const func = require("../common"); - -function urlParse (urlstr) { - var urlArr = urlstr.split('/') - return urlArr -} - -var datekind = ""; -var daykind = ""; -var isHalt = ""; - - -router.get('/', (req, res) => { - // 접근 url 은 항상 "학기,방학/주중,주말/정류장 종류 의 형태로 들어옴." - var urlArr = urlParse(req.originalUrl) - - //Full Path로 접근 한 경우 - 기존 urlArr 배열 그대로 활용. - datekind = urlArr[1] - daykind = urlArr[2] - - if (urlArr.length == 2) { - //Short Path 로 접근 한 경우 - datekind = func.getDateKind(); - daykind = func.getDayKind(); - isHalt = func.isHalt(); - } - console.log("Date Kind : " + datekind + "\n") - console.log("Day Kind : " + daykind + "\n"); - console.log("isHalted : " + isHalt + "\n"); - - switch (datekind) { - case 'semester': // 학기중 - if (daykind == 'week') { // 주중 - var jsonPath = path.join(__dirname, '..', 'timetable', 'semester', 'week', 'Residence_week.json') - } else if (daykind == 'weekend') { // 주말 - var jsonPath = path.join(__dirname, '..', 'timetable', 'semester', 'weekend', 'Residence_weekend.json') - } - break - case 'vacation': // 방학중 - if (daykind == 'week') { // 주중 - var jsonPath = path.join(__dirname, '..', 'timetable', 'vacation', 'week', 'Residence_week.json') - } else if (daykind == 'weekend') { // 주말 - var jsonPath = path.join(__dirname, '..', 'timetable', 'vacation', 'weekend', 'Residence_weekend.json') - } - break - case 'vacation_session': // 방학중 - 계절학기 - if (daykind == 'week') { // 주중 - var jsonPath = path.join(__dirname, '..', 'timetable', 'vacation_session', 'week', 'Residence_week.json') - } else if (daykind == 'weekend') { // 주말 - var jsonPath = path.join(__dirname, '..', 'timetable', 'vacation_session', 'weekend', 'Residence_weekend.json') - } - break - default: - break - } - if (isHalt == "halted") { - return res.status(200).json({ status: "Halt" }); - } - - try { - var data = fs.readFileSync(jsonPath, 'UTF-8') - } catch (err) { - console.log(err) - return res.status(404).json({ error: 'Incorrect column' }) - } - let jsondata = JSON.parse(data) - jsondata.datekind = datekind - jsondata.daykind = daykind - - console.log(jsondata) - if (daykind == 'week') { //주중일 경우 - return res.json(jsondata.residence_week) - } else { //주말일 경우 - return res.json(jsondata.residence_weekend) - } -}) - -module.exports = router diff --git a/router/routes.js b/router/routes.js new file mode 100644 index 0000000..dd70fe9 --- /dev/null +++ b/router/routes.js @@ -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" }); + } + } + ); +} diff --git a/router/shuttlecock_i.js b/router/shuttlecock_i.js deleted file mode 100644 index a02d94a..0000000 --- a/router/shuttlecock_i.js +++ /dev/null @@ -1,81 +0,0 @@ -const express = require('express') -const path = require('path') -const router = express.Router() -const fs = require('fs') -const func = require('../common') - -function urlParse (urlstr) { - var urlArr = urlstr.split('/') - return urlArr -} - -var datekind = ""; -var daykind = ""; -var isHalt = ""; - - -router.get('/', (req, res) => { - // 접근 url 은 항상 "학기,방학/주중,주말/정류장 종류 의 형태로 들어옴." - var urlArr = urlParse(req.originalUrl) - - //Full Path로 접근 한 경우 - 기존 urlArr 배열 그대로 활용. - datekind = urlArr[1] - daykind = urlArr[2] - - if (urlArr.length == 2){ //Short Path 로 접근 한 경우 - datekind = func.getDateKind() - daykind = func.getDayKind() - isHalt = func.isHalt() - } - - console.log("Date Kind : " + datekind + "\n"); - console.log("Day Kind : " + daykind + "\n"); - console.log("isHalted : " + isHalt + "\n"); - - switch (datekind) { - case 'semester': // 학기중 - if (daykind == 'week') { // 주중 - var jsonPath = path.join(__dirname, '..', 'timetable', 'semester', 'week', 'Shuttlecock_I_week.json') - } else if (daykind == 'weekend') { // 주말 - var jsonPath = path.join(__dirname, '..', 'timetable', 'semester', 'weekend', 'Shuttlecock_I_weekend.json') - } - break - case 'vacation': // 방학중 - if (daykind == 'week') { // 주중 - var jsonPath = path.join(__dirname, '..', 'timetable', 'vacation', 'week', 'Shuttlecock_I_week.json') - } else if (daykind == 'weekend') { // 주말 - var jsonPath = path.join(__dirname, '..', 'timetable', 'vacation', 'weekend', 'Shuttlecock_I_weekend.json') - } - break - case 'vacation_session': // 계절학기중 - if (daykind == 'week') { // 주중 - var jsonPath = path.join(__dirname, '..', 'timetable', 'vacation_session', 'week', 'Shuttlecock_I_week.json') - } else if (daykind == 'weekend') { // 주말 - var jsonPath = path.join(__dirname, '..', 'timetable', 'vacation_session', 'weekend', 'Shuttlecock_I_weekend.json') - } - break - default: - break - } - - if (isHalt == "halted") { - return res.status(200).json({ status: "Halt" }); - } - - try { - var data = fs.readFileSync(jsonPath, 'UTF-8') - } catch (err) { - console.log(err) - return res.status(404).json({ error: 'Incorrect column' }) - } - let jsondata = JSON.parse(data); - jsondata.datekind = datekind; - jsondata.daykind = daykind; - if (daykind == 'week') { //주중일 경우 - return res.json(jsondata.shuttlecock_i_week) - } else { //주말일 경우 - return res.json(jsondata.shuttlecock_i_weekend) - } -}) - -module.exports = router diff --git a/router/shuttlecock_o.js b/router/shuttlecock_o.js deleted file mode 100644 index a5f3919..0000000 --- a/router/shuttlecock_o.js +++ /dev/null @@ -1,80 +0,0 @@ -const express = require('express') -const path = require('path') -const router = express.Router() -const fs = require('fs') -const func = require('../common') - -function urlParse (urlstr) { - var urlArr = urlstr.split('/') - return urlArr -} - -var datekind = ""; -var daykind = ""; -var isHalt = ""; - - -router.get('/', (req, res) => { - // 접근 url 은 항상 "학기,방학/주중,주말/정류장 종류 의 형태로 들어옴." - var urlArr = urlParse(req.originalUrl) - - //Full Path로 접근 한 경우 - 기존 urlArr 배열 그대로 활용. - datekind = urlArr[1] - daykind = urlArr[2] - - if (urlArr.length == 2){ //Short Path 로 접근 한 경우 - datekind = func.getDateKind() - daykind = func.getDayKind() - isHalt = func.isHalt() - } - - console.log("Date Kind : " + datekind + "\n"); - console.log("Day Kind : " + daykind + "\n"); - console.log("isHalted : " + isHalt + "\n"); - - switch (datekind) { - case 'semester': // 학기중 - if (daykind == 'week') { // 주중 - var jsonPath = path.join(__dirname, '..', 'timetable', 'semester', 'week', 'Shuttlecock_O_week.json') - } else if (daykind == 'weekend') { // 주말 - var jsonPath = path.join(__dirname, '..', 'timetable', 'semester', 'weekend', 'Shuttlecock_O_weekend.json') - } - break - case 'vacation': // 방학중 - if (daykind == 'week') { // 주중 - var jsonPath = path.join(__dirname, '..', 'timetable', 'vacation', 'week', 'Shuttlecock_O_week.json') - } else if (daykind == 'weekend') { // 주말 - var jsonPath = path.join(__dirname, '..', 'timetable', 'vacation', 'weekend', 'Shuttlecock_O_weekend.json') - } - break - case 'vacation_session': // 계절학기중 - if (daykind == 'week') { // 주중 - var jsonPath = path.join(__dirname, '..', 'timetable', 'vacation_session', 'week', 'Shuttlecock_O_week.json') - } else if (daykind == 'weekend') { // 주말 - var jsonPath = path.join(__dirname, '..', 'timetable', 'vacation_session', 'weekend', 'Shuttlecock_O_weekend.json') - } - break - default: - break - } - if (isHalt == "halted") { - return res.status(200).json({ status: "Halt" }); - } - - try { - var data = fs.readFileSync(jsonPath, 'UTF-8') - } catch (err) { - console.log(err) - return res.status(404).json({ error: 'Incorrect column' }) - } - let jsondata = JSON.parse(data); - jsondata.datekind = datekind; - jsondata.daykind = daykind; - if (daykind == 'week') { //주중일 경우 - return res.json(jsondata.shuttlecock_o_week) - } else { //주말일 경우 - return res.json(jsondata.shuttlecock_o_weekend) - } -}) - -module.exports = router diff --git a/router/subway.js b/router/subway.js deleted file mode 100644 index f2bba83..0000000 --- a/router/subway.js +++ /dev/null @@ -1,81 +0,0 @@ -const express = require('express') -const path = require('path') -const router = express.Router() -const fs = require('fs') -const func = require('../common') - - -function urlParse (urlstr) { - var urlArr = urlstr.split('/') - return urlArr -} - -var datekind = ""; -var daykind = ""; -var isHalt = ""; - - -router.get('/', (req, res) => { - // 접근 url 은 항상 "학기,방학/주중,주말/정류장 종류 의 형태로 들어옴." - var urlArr = urlParse(req.originalUrl) - - //Full Path로 접근 한 경우 - 기존 urlArr 배열 그대로 활용. - datekind = urlArr[1] - daykind = urlArr[2] - - if (urlArr.length == 2){ //Short Path 로 접근 한 경우 - datekind = func.getDateKind() - daykind = func.getDayKind() - isHalt = func.isHalt(); - } - - console.log("Date Kind : " + datekind + "\n"); - console.log("Day Kind : " + daykind + "\n"); - console.log("isHalted : " + isHalt + "\n"); - - switch (datekind) { - case 'semester': // 학기중 - if (daykind == 'week') { // 주중 - var jsonPath = path.join(__dirname, '..', 'timetable', 'semester', 'week', 'Subway_week.json') - } else if (daykind == 'weekend') { // 주말 - var jsonPath = path.join(__dirname, '..', 'timetable', 'semester', 'weekend', 'Subway_weekend.json') - } - break - case 'vacation': // 방학중 - if (daykind == 'week') { // 주중 - var jsonPath = path.join(__dirname, '..', 'timetable', 'vacation', 'week', 'Subway_week.json') - } else if (daykind == 'weekend') { // 주말 - var jsonPath = path.join(__dirname, '..', 'timetable', 'vacation', 'weekend', 'Subway_weekend.json') - } - break - case 'vacation_session': // 방학중-계절학기 - if (daykind == 'week') { // 주중 - var jsonPath = path.join(__dirname, '..', 'timetable', 'vacation_session', 'week', 'Subway_week.json') - } else if (daykind == 'weekend') { // 주말 - var jsonPath = path.join(__dirname, '..', 'timetable', 'vacation_session', 'weekend', 'Subway_weekend.json') - } - break - default: - break - } - if (isHalt == "halted") { - return res.status(200).json({ status: "Halt" }); - } - - try { - var data = fs.readFileSync(jsonPath, 'UTF-8') - } catch (err) { - console.log(err) - return res.status(404).json({ error: 'Incorrect column' }) - } - let jsondata = JSON.parse(data); - jsondata.datekind = datekind; - jsondata.daykind = daykind; - if(daykind == 'week'){ //주중일 경우 - return res.json(jsondata.subway_week) - } else { //주말일 경우 - return res.json(jsondata.subway_weekend) - } -}) - -module.exports = router diff --git a/router/yesulin.js b/router/yesulin.js deleted file mode 100644 index 6a04ea5..0000000 --- a/router/yesulin.js +++ /dev/null @@ -1,81 +0,0 @@ -const express = require('express') -const path = require('path') -const router = express.Router() -const fs = require('fs') -const func = require('../common') - -function urlParse (urlstr) { - var urlArr = urlstr.split('/') - return urlArr -} - -var datekind = ""; -var daykind = ""; -var isHalt = ""; - - -router.get('/', (req, res) => { - // 접근 url 은 항상 "학기,방학/주중,주말/정류장 종류" 의 형태로 들어옴. - var urlArr = urlParse(req.originalUrl) - - //Full Path로 접근 한 경우 - 기존 urlArr 배열 그대로 활용. - datekind = urlArr[1] - daykind = urlArr[2] - - if (urlArr.length == 2){ //Short Path 로 접근 한 경우 - datekind = func.getDateKind() - daykind = func.getDayKind() - isHalt = func.isHalt() - } - - console.log("Date Kind : " + datekind + "\n"); - console.log("Day Kind : " + daykind + "\n"); - console.log("isHalted : " + isHalt + "\n"); - - - switch (datekind) { - case 'semester': // 학기중 - if (daykind == 'week') { // 주중 - var jsonPath = path.join(__dirname, '..', 'timetable', 'semester', 'week', 'YesulIn_week.json') - } else if (daykind == 'weekend') { // 주말 - var jsonPath = path.join(__dirname, '..', 'timetable', 'semester', 'weekend', 'YesulIn_weekend.json') - } - break - case 'vacation': // 방학중 - if (daykind == 'week') { // 주중 - var jsonPath = path.join(__dirname, '..', 'timetable', 'vacation', 'week', 'YesulIn_week.json') - } else if (daykind == 'weekend') { // 주말 - var jsonPath = path.join(__dirname, '..', 'timetable', 'vacation', 'weekend', 'YesulIn_weekend.json') - } - break - case 'vacation_session': // 계절학기중 - if (daykind == 'week') { // 주중 - var jsonPath = path.join(__dirname, '..', 'timetable', 'vacation_session', 'week', 'YesulIn_week.json') - } else if (daykind == 'weekend') { // 주말 - var jsonPath = path.join(__dirname, '..', 'timetable', 'vacation_session', 'weekend', 'YesulIn_weekend.json') - } - break - default: - break - } - if(isHalt == "halted"){ - return res.status(200).json({status: "Halt" }) - } - try { - var data = fs.readFileSync(jsonPath, 'UTF-8') - } catch (err) { - console.log(err) - return res.status(404).json({ error: 'Incorrect column' }) - } - let jsondata = JSON.parse(data); - jsondata.datekind = datekind; - jsondata.daykind = daykind; - - if (daykind == 'week') { //주중일 경우 - return res.json(jsondata.yesulin_week) - } else { //주말일 경우 - return res.json(jsondata.yesulin_weekend) - } -}) - -module.exports = router