-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move all adapter files to a dir
- Loading branch information
1 parent
9dc6620
commit 8e7cdfd
Showing
5 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Register Cron Event | ||
module.exports = (cron) => { | ||
// return true | ||
return [ | ||
// Register cron | ||
cron.countRequests, | ||
cron.updateSentences | ||
// cron.checkUpdate | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = [ | ||
// Dev Plugins | ||
['koa-livereload', require('koa-livereload')()] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict' | ||
const path = require('path') | ||
const nconf = require('nconf') | ||
|
||
module.exports = [ | ||
// MiddleWares | ||
['requestId', require('./src/middlewares/requestId')()], | ||
['countRequests', require('./src/middlewares/countRequest')()], | ||
['responseHandler', require('./src/middlewares/responseHandler')()], | ||
|
||
// Mail Error | ||
['MailError', require('./src/middlewares/MailError')()], | ||
|
||
// Basic Plugins | ||
['koa-helmet', require('koa-helmet')()], | ||
['koa-query-pretty', require('koa-query-pretty')()], | ||
['koa-jsonp', require('@hitokoto/koa-jsonp')()], | ||
['koa-bodyparser', require('koa-bodyparser')({ | ||
enableTypes: ['json', 'form'], | ||
formLimit: '10mb', | ||
jsonLimit: '10mb' | ||
})], | ||
['kcors', require('kcors')({ | ||
origin: '*', | ||
allowMethods: ['GET', 'HEAD', 'PUT', 'POST', 'DELETE', 'PATCH'], | ||
exposeHeaders: ['X-Request-Id'] | ||
})], | ||
['koa-favicon', require('koa-favicon')(path.join(__dirname, './public/favicon.ico'))], | ||
!nconf.get('compress_body') || ['koa-compress', require('koa-compress')({ | ||
filter: (contentType) => { | ||
return /text/i.test(contentType) | ||
}, | ||
threshold: 2048, | ||
flush: require('zlib').Z_SYNC_FLUSH | ||
})], | ||
['logger', require('./src/logger')()] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// This file is a map of child processes | ||
const path = require('path') | ||
const winston = require('winston') | ||
const colors = require('colors') | ||
const AB = require('./src/extensions/sentencesABSwitcher') | ||
|
||
module.exports = { | ||
processes: [ | ||
{ | ||
path: path.join(__dirname, './src/cron.js'), // The absolute path of the process file | ||
name: 'cronJob', // The name of the process module | ||
messageListener: (message, { event, moduleName }) => { | ||
// emit msg to global process route | ||
if (message && message.key) { | ||
message.from = moduleName | ||
event.emit('message', message, moduleName) | ||
} | ||
}, // the handler of the receiving message | ||
isDev: false, // if set true, this process will start only in Dev | ||
isProd: false // if set true, this process will start only in prod | ||
// if isDev and isProd both be set false, process will both start in Dev and Prod | ||
} | ||
], | ||
receivers: [ | ||
{ | ||
key: 'switchAB', | ||
to: 'ab', | ||
from: 'cronJob', | ||
listener: (targetDB) => { | ||
winston.verbose('[AB] receiving signal, switching to db: ' + colors.yellow(targetDB)) | ||
AB.setDatabase(targetDB) | ||
} | ||
}, | ||
{ | ||
key: 'loaded', | ||
to: 'core', | ||
from: 'cronJob', | ||
listener: (message, moduleName) => { | ||
winston.verbose('[init] all cronJobs are loaded. ') | ||
} | ||
}, | ||
{ | ||
key: 'error', | ||
to: 'core', | ||
from: 'cronJob', | ||
listener: (data, moduleName) => { | ||
console.log(colors.red(data)) | ||
winston.error('[init] error was thrown while loading cron jobs, process existing.') | ||
process.exit(1) | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
const nconf = require('nconf') | ||
|
||
module.exports = (router, middlewares, controller) => { | ||
// Route Map | ||
/* router.get('/', async (ctx, next) => { | ||
ctx.body = { | ||
message: 'Hello World', | ||
ts: Date.now() | ||
} | ||
}) | ||
*/ | ||
if (nconf.get('dev')) { | ||
router.get('/crash', async ctx => { | ||
throw new Error('崩溃测试') | ||
}) | ||
router.get('/test', async ctx => { | ||
const nconf = require('nconf') | ||
const os = require('os') | ||
let memoryUsage = 0 | ||
for (const v of Object.values(process.memoryUsage())) { | ||
memoryUsage += parseInt(v) | ||
} | ||
memoryUsage = memoryUsage / (1024 * 1024) | ||
ctx.body = { | ||
header: ctx.headers, | ||
host: ctx.request.host, | ||
server_id: nconf.get('api_name'), | ||
server_status: { | ||
memory: { | ||
totol: os.totalmem() / (1024 * 1024), | ||
free: os.freemem() / (1024 * 1024), | ||
usage: memoryUsage | ||
}, | ||
cpu: os.cpus(), | ||
load: os.loadavg() | ||
}, | ||
hostname: ctx.request.hostname, | ||
URL: ctx.request.URL, | ||
url: ctx.request.url, | ||
origin: ctx.request.origin, | ||
originalUrl: ctx.request.originalUrl, | ||
queryParams: ctx.query, | ||
queryLength: ctx.query && ctx.query.c ? ctx.query.c.length : '', | ||
now: new Date().toUTCString() | ||
} | ||
}) | ||
} | ||
|
||
router.get('/', controller.hitokoto) | ||
// router.get('/test', controller.hello.index) | ||
router.get('/status', controller.status) | ||
|
||
// Netease API | ||
router.get('/nm/search/:keyword', controller.netease.search) | ||
router.get('/nm/playlist/:id', controller.netease.playlist) | ||
router.get('/nm/picture/:id/:height?', controller.netease.picture) | ||
router.get('/nm/artist/:id', controller.netease.artist) | ||
router.get('/nm/album/:id', controller.netease.album) | ||
router.get('/nm/lyric/:id', controller.netease.lyric) | ||
router.get('/nm/url/:id', controller.netease.url) | ||
router.get('/nm/detail/:id', controller.netease.detail) | ||
router.get('/nm/summary/:id', controller.netease.summary) | ||
router.get('/nm/redirect/music/:id', controller.netease.redirect) | ||
router.get('/nm/record/:uid', controller.netease.record) | ||
router.get('/nm/comment/music/:id', controller.netease.musicComment) | ||
router.get('/nm/url/mv/:mvid', async (ctx) => { | ||
ctx.redirect(301, `/nm/mv/${ctx.params.mvid}`) | ||
}) | ||
router.get('/nm/mv/:mvid', controller.netease.mv) | ||
// router.get('/nm/dj/program/detail/:pid', controller.netease.djProgramInfo) | ||
router.get('/nm/user/dj/:uid', controller.netease.userDj) | ||
router.get('/nm/dj/:rid', controller.netease.djProgram) | ||
router.get('/nm/dj/detail/:rid', controller.netease.djDetail) | ||
|
||
return router | ||
} |