This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1453 from pan93412/refactor/server-seperation
refactor(server): 使伺服器可以復用、更換廢棄函式
- Loading branch information
Showing
9 changed files
with
562 additions
and
189 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 |
---|---|---|
|
@@ -4,3 +4,4 @@ node_modules | |
.idea | ||
.vscode | ||
.history | ||
examples/moddef.json |
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 |
---|---|---|
@@ -1,146 +1,4 @@ | ||
#!/usr/bin/env node | ||
const fs = require('fs') | ||
const path = require('path') | ||
const express = require('express') | ||
const bodyParser = require('body-parser') | ||
const request = require('./util/request') | ||
const packageJSON = require('./package.json') | ||
const exec = require('child_process').exec | ||
const cache = require('./util/apicache').middleware | ||
const { cookieToJson } = require('./util/index') | ||
const fileUpload = require('express-fileupload') | ||
const decode = require('safe-decode-uri-component') | ||
|
||
// version check | ||
exec('npm info NeteaseCloudMusicApi version', (err, stdout, stderr) => { | ||
if (!err) { | ||
let version = stdout.trim() | ||
if (packageJSON.version < version) { | ||
console.log( | ||
`最新版本: ${version}, 当前版本: ${packageJSON.version}, 请及时更新`, | ||
) | ||
} | ||
} | ||
require('./server').serveNcmApi({ | ||
checkVersion: true, | ||
}) | ||
|
||
const app = express() | ||
app.set('trust proxy', true) | ||
|
||
// CORS & Preflight request | ||
app.use((req, res, next) => { | ||
if (req.path !== '/' && !req.path.includes('.')) { | ||
res.set({ | ||
'Access-Control-Allow-Credentials': true, | ||
'Access-Control-Allow-Origin': req.headers.origin || '*', | ||
'Access-Control-Allow-Headers': 'X-Requested-With,Content-Type', | ||
'Access-Control-Allow-Methods': 'PUT,POST,GET,DELETE,OPTIONS', | ||
'Content-Type': 'application/json; charset=utf-8', | ||
}) | ||
} | ||
req.method === 'OPTIONS' ? res.status(204).end() : next() | ||
}) | ||
|
||
// cookie parser | ||
app.use((req, res, next) => { | ||
req.cookies = {} | ||
//;(req.headers.cookie || '').split(/\s*;\s*/).forEach((pair) => { // Polynomial regular expression // | ||
;(req.headers.cookie || '').split(/;\s+|(?<!\s)\s+$/g).forEach((pair) => { | ||
let crack = pair.indexOf('=') | ||
if (crack < 1 || crack == pair.length - 1) return | ||
req.cookies[decode(pair.slice(0, crack)).trim()] = decode( | ||
pair.slice(crack + 1), | ||
).trim() | ||
}) | ||
next() | ||
}) | ||
|
||
// body parser | ||
app.use(bodyParser.json()) | ||
app.use(bodyParser.urlencoded({ extended: false })) | ||
|
||
app.use(fileUpload()) | ||
|
||
// static | ||
app.use(express.static(path.join(__dirname, 'public'))) | ||
|
||
// cache | ||
app.use(cache('2 minutes', (req, res) => res.statusCode === 200)) | ||
// router | ||
const special = { | ||
'daily_signin.js': '/daily_signin', | ||
'fm_trash.js': '/fm_trash', | ||
'personal_fm.js': '/personal_fm', | ||
} | ||
|
||
fs.readdirSync(path.join(__dirname, 'module')) | ||
.reverse() | ||
.forEach((file) => { | ||
if (!file.endsWith('.js')) return | ||
let route = | ||
file in special | ||
? special[file] | ||
: '/' + file.replace(/\.js$/i, '').replace(/_/g, '/') | ||
let question = require(path.join(__dirname, 'module', file)) | ||
|
||
app.use(route, (req, res) => { | ||
;[req.query, req.body].forEach((item) => { | ||
if (typeof item.cookie === 'string') { | ||
item.cookie = cookieToJson(decode(item.cookie)) | ||
} | ||
}) | ||
let query = Object.assign( | ||
{}, | ||
{ cookie: req.cookies }, | ||
req.query, | ||
req.body, | ||
req.files, | ||
) | ||
|
||
question(query, request) | ||
.then((answer) => { | ||
console.log('[OK]', decode(req.originalUrl)) | ||
|
||
const cookies = answer.cookie | ||
if (Array.isArray(cookies) && cookies.length > 0) { | ||
if (req.protocol === 'https') { | ||
// Try to fix CORS SameSite Problem | ||
res.append( | ||
'Set-Cookie', | ||
cookies.map((cookie) => { | ||
return cookie + '; SameSite=None; Secure' | ||
}), | ||
) | ||
} else { | ||
res.append('Set-Cookie', cookies) | ||
} | ||
} | ||
res.status(answer.status).send(answer.body) | ||
}) | ||
.catch((answer) => { | ||
console.log('[ERR]', decode(req.originalUrl), { | ||
status: answer.status, | ||
body: answer.body, | ||
}) | ||
if (!answer.body) { | ||
res.status(404).send({ | ||
code: 404, | ||
data: null, | ||
msg: 'Not Found', | ||
}) | ||
return | ||
} | ||
if (answer.body.code == '301') answer.body.msg = '需要登录' | ||
res.append('Set-Cookie', answer.cookie) | ||
res.status(answer.status).send(answer.body) | ||
}) | ||
}) | ||
}) | ||
|
||
const port = process.env.PORT || 3000 | ||
const host = process.env.HOST || '' | ||
|
||
app.server = app.listen(port, host, () => { | ||
console.log(`server running @ http://${host ? host : 'localhost'}:${port}`) | ||
}) | ||
|
||
module.exports = app |
This file was deleted.
Oops, something went wrong.
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,22 @@ | ||
const fsPromises = require('fs/promises') | ||
const path = require('path') | ||
const server = require('../server') | ||
|
||
const exportFile = path.join(__dirname, 'moddef.json') | ||
|
||
async function main() { | ||
const def = await server.getModulesDefinitions( | ||
path.join(__dirname, '..', 'module'), | ||
{ | ||
'daily_signin.js': '/daily_signin', | ||
'fm_trash.js': '/fm_trash', | ||
'personal_fm.js': '/personal_fm', | ||
}, | ||
false, | ||
) | ||
|
||
fsPromises.writeFile(exportFile, JSON.stringify(def, null, 4)) | ||
console.log(`👍 Get your own definition at: ${exportFile}`) | ||
} | ||
|
||
main() |
Oops, something went wrong.
36942a3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
netease-cloud-music-api – ./docs
neteasecloudmusicapi.vercel.app
netease-cloud-music-api-git-master-binaryify.vercel.app
36942a3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
netease-cloud-music-api-binaryify – ./
netease-cloud-music-api-binaryify.vercel.app
netease-cloud-music-api-binaryify-binaryify.vercel.app
netease-cloud-music-api-binaryify-git-master-binaryify.vercel.app