Skip to content

Commit b7260cf

Browse files
committed
🐳 chore: 支持自动部署
1 parent 3343f2b commit b7260cf

File tree

7 files changed

+610
-389
lines changed

7 files changed

+610
-389
lines changed

.github/workflows/docker.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Publish Docker image
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
jobs:
8+
push_to_registry:
9+
name: Push Docker image to multiple registries
10+
runs-on: ubuntu-latest
11+
permissions:
12+
packages: write
13+
contents: read
14+
steps:
15+
- name: Check out the repo
16+
uses: actions/checkout@v4
17+
18+
- name: Log in to Docker Hub
19+
uses: docker/login-action@v3
20+
with:
21+
username: ${{ secrets.DOCKER_USERNAME }}
22+
password: ${{ secrets.DOCKER_PASSWORD }}
23+
24+
- name: Log in to the Container registry
25+
uses: docker/login-action@v3
26+
with:
27+
registry: ghcr.io
28+
username: ${{ github.actor }}
29+
password: ${{ secrets.GITHUB_TOKEN }}
30+
31+
- name: Extract metadata (tags, labels) for Docker
32+
id: meta
33+
uses: docker/metadata-action@v5
34+
with:
35+
images: |
36+
imsyy/dailyhot-api
37+
ghcr.io/${{ github.repository }}
38+
39+
- name: Build and push Docker image
40+
uses: docker/build-push-action@v5
41+
with:
42+
context: .
43+
file: ./Dockerfile
44+
push: true
45+
tags: ${{ steps.meta.outputs.tags }}
46+
labels: ${{ steps.meta.outputs.labels }}

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM node:16-alpine
1+
FROM node:18-alpine
22
WORKDIR /app
33

44
COPY package*.json ./

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ pnpm start
117117
docker build -t dailyhot-api .
118118
# 运行
119119
docker run -p 6688:6688 -d dailyhot-api
120+
# 或使用 Docker Compose
121+
docker-compose up -d
120122
```
121123

122124
### 在线部署

docker-compose.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
services:
2+
DailyhotApi:
3+
build:
4+
context: .
5+
image: dailyhot-api
6+
container_name: dailyhot-api
7+
volumes:
8+
- /etc/localtime:/etc/localtime:ro
9+
- /etc/timezone:/etc/timezone:ro
10+
ports:
11+
- 6688:6688
12+
restart: always

index.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,28 @@ app.use(
2727
}),
2828
);
2929

30+
// CORS
3031
app.use(async (ctx, next) => {
31-
if (domain === "*") {
32-
await next();
32+
ctx.set("Access-Control-Allow-Origin", domain);
33+
ctx.set("Access-Control-Allow-Methods", "GET, OPTIONS");
34+
ctx.set("Access-Control-Allow-Headers", "Content-Type, Authorization");
35+
ctx.set("Access-Control-Allow-Credentials", "true");
36+
// 处理预检请求
37+
if (ctx.method === "OPTIONS") {
38+
ctx.status = 200;
3339
} else {
34-
if (ctx.headers.origin === domain || ctx.headers.referer === domain) {
40+
if (domain === "*") {
3541
await next();
3642
} else {
37-
ctx.status = 403;
38-
ctx.body = {
39-
code: 403,
40-
message: "请通过正确的域名访问",
41-
};
43+
if (ctx.headers.origin === domain || ctx.headers.referer === domain) {
44+
await next();
45+
} else {
46+
ctx.status = 403;
47+
ctx.body = {
48+
code: 403,
49+
message: "请通过正确的域名访问",
50+
};
51+
}
4252
}
4353
}
4454
});

package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dailyhot_api",
3-
"version": "1.0.7",
3+
"version": "1.0.8",
44
"description": "An api on Today's Hot list",
55
"main": "index.js",
66
"scripts": {
@@ -14,19 +14,19 @@
1414
"author": "imsyy",
1515
"license": "MIT",
1616
"dependencies": {
17-
"axios": "^1.3.4",
17+
"axios": "^1.6.3",
1818
"cheerio": "1.0.0-rc.12",
19-
"dotenv": "^16.0.3",
20-
"eslint": "^8.48.0",
21-
"eslint-plugin-vue": "^9.17.0",
22-
"koa": "^2.14.1",
23-
"koa-bodyparser": "^4.3.0",
24-
"koa-router": "^12.0.0",
19+
"dotenv": "^16.3.1",
20+
"eslint": "^8.56.0",
21+
"eslint-plugin-vue": "^9.19.2",
22+
"koa": "^2.15.0",
23+
"koa-bodyparser": "^4.4.1",
24+
"koa-router": "^12.0.1",
2525
"koa-static": "^5.0.0",
26-
"koa-views": "^8.0.0",
26+
"koa-views": "^8.1.0",
2727
"koa2-cors": "^2.0.6",
2828
"node-cache": "^5.1.2",
2929
"nodemon": "^2.0.22",
30-
"prettier": "^3.0.2"
30+
"prettier": "^3.1.1"
3131
}
3232
}

0 commit comments

Comments
 (0)