Skip to content

Commit

Permalink
chore: init
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchuzhang committed Sep 10, 2022
0 parents commit c1fcadf
Show file tree
Hide file tree
Showing 20 changed files with 3,565 additions and 0 deletions.
43 changes: 43 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
kind: pipeline
type: docker
name: default

steps:
- name: test
image: 18-alpine3.15
commands:
- yarn
- yarn test

- name: docker-build
image: plugins/docker
settings:
repo: liuchuzhang/blue-music-be
tags: latest
username:
from_secret: docker_username
password:
from_secret: docker_password

- name: deploy
image: appleboy/drone-ssh
environment:
SSH_DEPLOY_SHELL:
from_secret: ssh_deploy_shell
settings:
host:
from_secret: ssh_host
port:
from_secret: ssh_port
username:
from_secret: ssh_username
password:
from_secret: ssh_password
envs:
- ssh_deploy_shell
script:
- $SSH_DEPLOY_SHELL -d music -e liuchuzhang/blue-music-be -i music-be -p 4001 -u /api

trigger:
branch:
- main
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
*.log
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

FROM node:18-alpine3.15
ENV NODE_ENV=production

WORKDIR /app

COPY ["package.json", "yarn.lock*", "./"]

RUN yarn

COPY . .

EXPOSE 4001
CMD ["yarn", "server"]
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# blue-music-be

## Usage

```shell
yarn && yarn dev
```
13 changes: 13 additions & 0 deletions __test__/hello.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import axios from 'axios'
import { stopServer } from './helpers/server'

afterAll(() => {
stopServer()
})

describe('hello', () => {
it('world', async () => {
const res = await axios.get('http://localhost:4001/api/hello/world')
expect(1 + 199).toEqual(200)
})
})
9 changes: 9 additions & 0 deletions __test__/helpers/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { exec } from 'child_process'
import { delay } from './utils'
import app from '../../src/app'

const server = app.listen(4001)

export const stopServer = async () => {
server.close()
}
2 changes: 2 additions & 0 deletions __test__/helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export const delay = (time?: number) => new Promise(r => setTimeout(r, time || 3000))
28 changes: 28 additions & 0 deletions __test__/proxy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import axios from 'axios'
import { stopServer } from './helpers/server'

afterAll(() => {
stopServer()
})

describe('proxy', () => {
it('kuwo', async () => {
const res = await axios.get('http://localhost:4001/api/kuwo')
expect(res.status).toEqual(200)
})

it('163', async () => {
const res = await axios.get('http://localhost:4001/api/music163/api/artist/10559')
expect(res.status).toEqual(200)
})

it('migu', async () => {
const res = await axios.get('http://localhost:4001/api/appcnfmigu/MIGUM3.0/v1.0/template/rank-list/release?dataVersion=1616469593718&templateVersion=9')
expect(res.status).toEqual(200)
})

it('migu', async () => {
const res = await axios.get('http://localhost:4001/api/cyqq/v8/fcg-bin/fcg_myqq_toplist.fcg?g_tk=5381&inCharset=utf-8&outCharset=utf-8&notice=0&format=json&uin=0&needNewCode=1&platform=h5')
expect(res.status).toEqual(200)
})
})
11 changes: 11 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
testMatch: ["**/__test__/**/*.test.ts"],
transform: {
'^.+\\.(t|j)sx?$': ['@swc-node/jest'],
},
globals: {
"ts-jest": {
tsconfig: "tsconfig.test.json",
},
},
}
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "blue-music-be",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "nodemon -r @swc-node/register src/server.ts",
"test": "jest",
"server": "node -r @swc-node/register src/server.ts"
},
"author": "",
"license": "ISC",
"dependencies": {
"@types/express": "^4.17.13",
"address": "^1.1.2",
"axios": "^0.27.2",
"express": "^4.18.1",
"http-proxy-middleware": "^2.0.0",
"ts-node": "^10.9.1",
"typescript": "^4.8.3"
},
"devDependencies": {
"@swc-node/jest": "^1.5.2",
"@swc-node/register": "^1.5.1",
"@types/jest": "^29.0.0",
"jest": "^29.0.2",
"nodemon": "^2.0.19"
}
}
9 changes: 9 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import express from 'express'
import { proxy, hello } from './routes'

const app = express()

app.use('/api', proxy)
app.use('/api/hello', hello)

export default app
1 change: 1 addition & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './proxy'
57 changes: 57 additions & 0 deletions src/constants/proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
export interface ProxyItem {
prefix: string
target: string
domain?: string
}

export const proxyList: ProxyItem[] = [
{
prefix: '/uyqq',
target: 'https://u.y.qq.com'
},
{
prefix: '/cyqq',
target: 'https://c.y.qq.com'
},
{
prefix: '/music163',
target: 'https://music.163.com'
},
{
prefix: '/kuwo',
target: 'https://www.kuwo.cn'
},
{
prefix: '/kwroot/root',
target: 'https://www.kuwo.cn',
domain: 'www.kuwo.cn'
},
{
prefix: '/songsearchkugou',
target: 'https://songsearch.kugou.com'
},
{
prefix: '/apibilibili',
target: 'https://api.bilibili.com'
},
{
prefix: '/jadeitemigu',
target: 'https://jadeite.migu.cn'
},
{
prefix: '/interface3',
target: 'https://interface3.music.163.com'
},
{
prefix: '/iyqq',
target: 'https://i.y.qq.com'
},
{
prefix: '/antiserverkuwo',
target: 'https://antiserver.kuwo.cn'
},
{
prefix: '/appcnfmigu',
target: 'https://app.c.nf.migu.cn'
}
]
9 changes: 9 additions & 0 deletions src/routes/hello.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import express, { Response, Request } from 'express'

const router = express.Router()

router.get('/world', (req: Request, res: Response, next) => {
res.send('Hello World')
})

export default router
2 changes: 2 additions & 0 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as hello } from './hello'
export { default as proxy } from './proxy'
47 changes: 47 additions & 0 deletions src/routes/proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import express from 'express'
import { createProxyMiddleware } from 'http-proxy-middleware'
import { ProxyItem, proxyList } from '../constants'

const router = express.Router()

const getKuwoProxy = (): ProxyItem[] => {
const array26 = Array(26).fill(null)
return array26.map((_, first) => array26.map((_, second) => {
const keyword = String.fromCharCode(97 + first) + String.fromCharCode(97 + second)

return {
prefix: `/${keyword}sycdnkuwo`,
target: `http://${keyword}.sycdn.kuwo.cn`
}
})).flat()
}

proxyList.concat(getKuwoProxy()).forEach(item => {
const host = item.target.replace(/^http[s]?:\/\//, '')

router.use(
item.prefix,
createProxyMiddleware({
target: item.target,
changeOrigin: true,
pathRewrite: {
[`^/api${item.prefix}`]: ''
},
cookieDomainRewrite: {
'*': item.domain || ''
},
cookiePathRewrite: {
'*': '/'
},
headers: {
Referer: item.target,
Host: host,
'sec-ch-ua': '"Chromium";v="104", " Not A;Brand";v="99", "Google Chrome";v="104"',
'User-Agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36'
}
})
)
})

export default router
6 changes: 6 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import address from 'address'
import app from './app'

app.listen(4001)

console.log(`server start on http://${address.ip()}:4001`)
16 changes: 16 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"target": "ESNext",
"strict": true,
"moduleResolution": "node",
"module": "CommonJS",
"noUnusedLocals": false,
"noUnusedParameters": false,
"esModuleInterop": true,
"allowJs": true,
"allowSyntheticDefaultImports": true,
"types": ["node"]
},
"include": ["*.ts", "./**/*.ts"],
"exclude": ["node_modules"]
}
8 changes: 8 additions & 0 deletions tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"types": ["node", "jest"]
},
"extends": "./tsconfig.json",
"include": ["./__test__/*.ts", "./__test__/**/*.ts"],
"exclude": ["node_modules"]
}
Loading

0 comments on commit c1fcadf

Please sign in to comment.