-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new CN site with alias "cnn", add redirect server
- Loading branch information
1 parent
2ea764c
commit 5802e24
Showing
14 changed files
with
717 additions
and
480 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 |
---|---|---|
|
@@ -42,8 +42,7 @@ jobs: | |
- uses: pnpm/[email protected] | ||
with: | ||
version: 6.0.2 | ||
- run: pnpm run ci | ||
- run: SITE_ALIAS=cn pnpm run build | ||
- run: pnpm run ci:server | ||
- name: Set up Docker Buildx | ||
uses: docker/setup-buildx-action@v2 | ||
with: | ||
|
@@ -62,3 +61,34 @@ jobs: | |
file: ./Dockerfile-CN | ||
push: true | ||
tags: ${{ secrets.DOCKERHUB_USERNAME }}/rankland-fe:cn | ||
build-and-push-for-cnn: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: 16 | ||
- uses: pnpm/[email protected] | ||
with: | ||
version: 6.0.2 | ||
- run: pnpm run ci | ||
- run: SITE_ALIAS=cnn pnpm run build | ||
- name: Set up Docker Buildx | ||
uses: docker/setup-buildx-action@v2 | ||
with: | ||
version: v0.9.1 | ||
driver-opts: | | ||
image=moby/buildkit:v0.10.6 | ||
- name: Login to DockerHub | ||
uses: docker/login-action@v2 | ||
with: | ||
username: ${{ secrets.DOCKERHUB_USERNAME }} | ||
password: ${{ secrets.DOCKERHUB_TOKEN }} | ||
- name: Build and Push Image for CN Next Site | ||
uses: docker/[email protected] | ||
with: | ||
context: ./ | ||
file: ./Dockerfile-CN-Next | ||
push: true | ||
tags: ${{ secrets.DOCKERHUB_USERNAME }}/rankland-fe:cnn |
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 |
---|---|---|
|
@@ -9,3 +9,5 @@ node_modules | |
/dist | ||
/logs | ||
/分发 | ||
.pnpm-debug.log | ||
npm-debug.log* |
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
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,11 @@ | ||
FROM algoux/nodebase:16 | ||
|
||
WORKDIR /app/ | ||
|
||
COPY package.json ./ | ||
COPY server ./server | ||
COPY dist ./dist | ||
|
||
ENV PATH="/app/server/node_modules/pm2/bin:${PATH}" | ||
ENV SITE_ALIAS=cnn | ||
CMD npm run start:docker |
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
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,122 @@ | ||
/* eslint-disable @typescript-eslint/no-require-imports */ | ||
const Koa = require('koa'); | ||
const Router = require('koa-router'); | ||
const bodyParser = require('koa-bodyparser'); | ||
const winston = require('winston'); | ||
const dayjs = require('dayjs'); | ||
const { v4: uuidv4 } = require('uuid'); | ||
|
||
const port = parseInt(process.env.PORT, 10) || 7002; | ||
|
||
const NEW_HOST = 'https://rl.algoux.cn'; | ||
|
||
const knownUrlKeywordToGeneralReplacementMap = { | ||
当前选中榜单的标识符: 'rankId', | ||
由官方整理和维护的: 'official', | ||
榜单合集: 'collection', | ||
探索: 'search', | ||
榜单: 'ranklist', | ||
游乐场: 'playground', | ||
直播: 'live', | ||
关键词: 'kw', | ||
聚焦: 'focus', | ||
是: 'yes', | ||
}; | ||
|
||
const convertUrl = (url) => { | ||
let newUrl = url; | ||
Object.keys(knownUrlKeywordToGeneralReplacementMap).forEach((key) => { | ||
newUrl = newUrl.replace(key, knownUrlKeywordToGeneralReplacementMap[key]); | ||
}); | ||
return newUrl; | ||
}; | ||
|
||
// app logger | ||
const appLoggingFormat = winston.format.printf(({ level, message, timestamp }) => { | ||
return `${dayjs(timestamp).format('YYYY-MM-DDTHH:mm:ssZ')} [${level}] ${message}`; | ||
}); | ||
const appLogger = winston.createLogger({ | ||
level: 'info', | ||
format: winston.format.combine(winston.format.splat(), winston.format.timestamp(), appLoggingFormat), | ||
transports: [ | ||
new winston.transports.File({ filename: 'logs/app-redirect-error.log', level: 'error' }), | ||
new winston.transports.File({ filename: 'logs/app-redirect-all.log' }), | ||
new winston.transports.Console(), | ||
], | ||
}); | ||
|
||
// koa app | ||
const app = new Koa(); | ||
app.proxy = true; | ||
|
||
// request id middleware | ||
app.use(async (ctx, next) => { | ||
ctx.requestId = uuidv4().substr(0, 8); | ||
ctx.set('X-Request-Id', ctx.requestId); | ||
return next(); | ||
}); | ||
|
||
// time cost middleware | ||
app.use(async (ctx, next) => { | ||
const _s = Date.now(); | ||
ctx.startAt = _s; | ||
await next(); | ||
ctx.set('X-Response-Time', Date.now() - _s); | ||
}); | ||
|
||
// error handler middleware | ||
app.use(async (ctx, next) => { | ||
try { | ||
await next(); | ||
} catch (e) { | ||
ctx.logger.error('Uncaught error: %O', e); | ||
ctx.throw(500, 'Internal Server Error'); | ||
} | ||
}); | ||
|
||
app.use(bodyParser()); | ||
|
||
// ctx logger middleware | ||
app.use((ctx, next) => { | ||
if (!ctx.logger) { | ||
const ctxLoggingFormat = winston.format.printf(({ level, message, timestamp }) => { | ||
return `${dayjs(timestamp).format('YYYY-MM-DDTHH:mm:ssZ')} [${level}] [${process.pid}] [${ctx.requestId}] [${ | ||
ctx.ip | ||
}/${Date.now() - ctx.startAt}ms ${ctx.method.toUpperCase()} ${ctx.url}] ${message}`; | ||
}); | ||
ctx.logger = winston.createLogger({ | ||
level: 'info', | ||
format: winston.format.combine(winston.format.splat(), winston.format.timestamp(), ctxLoggingFormat), | ||
transports: [ | ||
new winston.transports.File({ filename: 'logs/ctx-redirect-error.log', level: 'error' }), | ||
new winston.transports.File({ filename: 'logs/ctx-redirect-all.log' }), | ||
new winston.transports.Console(), | ||
], | ||
}); | ||
} | ||
return next(); | ||
}); | ||
|
||
const router = new Router(); | ||
|
||
router.get('*', (ctx, next) => { | ||
const newQuery = {}; | ||
Object.keys(ctx.query).forEach((key) => { | ||
const k = knownUrlKeywordToGeneralReplacementMap[key] || key; | ||
newQuery[k] = ctx.query[key]; | ||
}); | ||
const search = new URLSearchParams(newQuery).toString(); | ||
const newUrl = `${convertUrl(decodeURIComponent(ctx.path))}${search ? `?${search}` : ''}`; | ||
const nextUrl = `${NEW_HOST}${newUrl}`; | ||
ctx.logger.info('Redirecting to %s', nextUrl); | ||
ctx.redirect(nextUrl); | ||
}); | ||
|
||
app.use(router.routes()).use(router.allowedMethods()); | ||
|
||
async function main() { | ||
app.listen(port); | ||
console.log(`Redirection Server is listening on http://localhost:${port} (pid: ${process.pid})`); | ||
} | ||
|
||
main(); |
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
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,20 @@ | ||
module.exports = { | ||
apps: [ | ||
{ | ||
name: 'rankland-fe', | ||
cwd: '../', | ||
script: `server/deprecated-redirect.js`, | ||
max_memory_restart: '500M', | ||
exec_mode: 'cluster', | ||
instances: parseInt(process.env.WORKERS, 10) || 1, | ||
out_file: `logs/pm2-out.log`, | ||
error_file: `logs/pm2-error.log`, | ||
log_date_format: 'YYYY-MM-DD HH:mm:ss', | ||
merge_logs: true, | ||
min_uptime: '5s', | ||
env: { | ||
NODE_ENV: 'production', | ||
}, | ||
}, | ||
], | ||
}; |
Oops, something went wrong.