Docker部署必应每日一图前后端
前言
之前通过 PHP
自建了一个必应每日壁纸的 API
,最近发现一个可以收集每日壁纸的程序,并且带有前后端,界面比较美观,就是需要数据库,部署下来感觉还可以。
项目特点
- 有前后端
- 支持回溯
- 可以获取几种处理后的图:缩略图、高斯模糊、灰度,同时有HD、UHD
- 提供 Docker 部署
项目地址和官方演示地址如下:
部署
compose
使用 Docker
部署,再反向代理。
1mkdir -p /opt/bing/{data,mariadb_data,ssl_certs} &&
2touch /opt/bing/nginx.conf /opt/bing/data/config.js
3cd /opt/bing && vim compose.yml
1version: '3'
2services:
3 bing:
4 image: androidmumo/bing
5 container_name: bing
6 volumes:
7 - ./data:/usr/src/app/data
8 environment:
9 - TZ=Asia/Shanghai
10 depends_on:
11 - mariadb
12 restart: unless-stopped
13
14 mariadb:
15 image: mariadb:latest
16 container_name: mariadb
17 volumes:
18 - ./mariadb_data:/var/lib/mysql
19 environment:
20 - MYSQL_ROOT_PASSWORD=bing_root_password
21 - MYSQL_DATABASE=bing
22 - MYSQL_USER=bing
23 - MYSQL_PASSWORD=bing
24 - TZ=Asia/Shanghai
25 restart: unless-stopped
26
27 nginx:
28 image: nginx:1.22-alpine
29 container_name: bing-nginx
30 ports:
31 - "80:80"
32 - "443:443"
33 volumes:
34 - ./nginx.conf:/etc/nginx/conf.d/default.conf
35 - ./ssl_certs:/etc/nginx/ssl
36 depends_on:
37 - bing
38 restart: unless-stopped
config
在 /opt/data/config.js
中写入自己的配置:
1// 用户配置文件 config.js
2
3// 基础配置
4const baseConfig = {
5 port: 3000, // 服务启动端口号 (默认为3000)
6 updateTime: "00:01:00", // 每天更新时间 (开始从必应官方服务器下载图片的时间)
7 DelayTime: 5, // 延迟时间(分钟) 即每天00:05:00的时候才显示当天的图片。性能较差的实例应适当调大此值 (仅针对'/api/getImage'接口)
8 surviveDays: 90, // 图片存活天数(即图片保存多少天,到期即清理) 0为不清理
9 retryTimeout: 10000, // 错误重试间隔。共重试10次,每次间隔时间递增,这里指的是首次间隔时间 (单位:ms)
10 key: 'abcdefgh', // 鉴权密钥。用于需要鉴权才能访问的接口
11};
12
13// 数据库配置 (注意:除数据库连接池大小外,以下配置项提及的内容需在安装前准备好并填入)
14const databaseConfig = {
15 host: "mariadb ", // 数据库链接地址
16 port: "3306", // 数据库连接端口
17 database: "bing", // 数据库名
18 user: "bing", // 数据库用户名
19 password: "bing", // 数据库密码
20 connectionLimit: 100, // 数据库连接池大小
21};
22
23// 网站信息配置
24const infoConfig = {
25 link: [
26 {
27 label: "白馬空谷的主页", // 链接名称
28 url: "https://www.mcloc.cn/" // 链接地址
29 },
30 {
31 label: "白馬空谷的博客",
32 url: "https://blog.mcloc.cn/"
33 }
34 ],
35 htmlSlot: {
36 beforeFooter: ``, // 页脚上方HTML插槽
37 afterFooter: `<a style="margin-right: 10px;" target="_blank" href="https://beian.miit.gov.cn/">晋ICP备20001086号-1</a>
38 <a style="margin-right: 10px; display: flex; align-items: center;" target="_blank" href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=41080202000141">
39 <img style="width: 14px; margin-right: 6px;" src="https://www.mcloc.cn/wp-content/uploads/2020/04/beiantubiao-19.png"/>
40 <span>豫公网安备 41080202000141号</span>
41 </a>` // 页脚下方HTML插槽
42 }
43}
44
45module.exports = {
46 baseConfig,
47 databaseConfig,
48 infoConfig,
49};
反向代理
在 /opt/bing/nginx.conf
中写入反向代理的配置,将第 3 行域名修改为自己的域名,第 15 行 SSL
证书配置中的证书和私钥名称修改为对应的名称,并放入映射的位置。本文是将证书文件放置在 /opt/bing/ssl_certs/
目录下。
1server {
2 listen 80;
3 server_name bing.grew.cc;
4
5 # 将 HTTP 请求重定向到 HTTPS
6 location / {
7 return 301 https://$host$request_uri;
8 }
9}
10
11server {
12 listen 443 ssl;
13 server_name bing.grew.cc;
14
15 # SSL 证书配置
16 ssl_certificate /etc/nginx/ssl/bing.grew.cc.crt;
17 ssl_certificate_key /etc/nginx/ssl/bing.grew.cc.key;
18
19 # SSL 配置优化
20 ssl_protocols TLSv1.2 TLSv1.3;
21 ssl_prefer_server_ciphers on;
22 ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384;
23 ssl_session_timeout 1d;
24 ssl_session_cache shared:SSL:10m;
25
26 # 安全相关 HTTP 头
27 add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
28 add_header X-Frame-Options SAMEORIGIN;
29 add_header X-Content-Type-Options nosniff;
30
31 # 代理设置
32 location / {
33 proxy_pass http://bing:3000; # 注意这里使用服务名称
34 proxy_set_header Host $host;
35 proxy_set_header X-Real-IP $remote_addr;
36 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
37 proxy_set_header X-Forwarded-Proto $scheme;
38
39 # WebSocket 支持(如果需要)
40 proxy_http_version 1.1;
41 proxy_set_header Upgrade $http_upgrade;
42 proxy_set_header Connection "upgrade";
43
44 # 超时设置
45 proxy_connect_timeout 60s;
46 proxy_send_timeout 60s;
47 proxy_read_timeout 60s;
48 }
49}
接口
GET_IMAGE 获取当天图片
请求方法: GET
默认地址: http://localhost:3000/api/getImage
参数(query): 无
请求示例:
1http://localhost:3000/api/getImage
返回示例: 直接返回当天图片,可直接用作图片URL。
GET_LIST 获取图片列表
请求方法: GET
默认地址: http://localhost:3000/api/getList
参数(query):
Key | Value | 说明 |
---|---|---|
pageSize | Number | 每页数据条数 |
currentPage | Number | 目标页数 |
请求示例:
1http://localhost:3000/api/getList?pageSize=3¤tPage=2
返回示例:
1{
2 "totle": 10,
3 "list": [
4 {
5 "id": 7,
6 "title": "亚伯拉罕湖中的树,加拿大艾伯塔 (© Coolbiere/Getty Images)",
7 "date": "2021-04-15",
8 "base64": "data:image/jpeg;base64,/9j/4AAQSkZJ...",
9 "url": {
10 "hd": "http://localhost:3000/img/2021/04/15/2021-04-15_hd.jpg",
11 "uhd": "http://localhost:3000/img/2021/04/15/2021-04-15_uhd.jpg",
12 "gaussian": "http://localhost:3000/img/2021/04/15/2021-04-15_hd_gaussian_20.jpg",
13 "greyscale": "http://localhost:3000/img/2021/04/15/2021-04-15_hd_greyscale.jpg",
14 "thumbnail": "http://localhost:3000/img/2021/04/15/2021-04-15_hd_thumbnail_480_270.jpg"
15 },
16 "color": {
17 "Muted": "#5182ac",
18 "Vibrant": "#24a3c8",
19 "DarkMuted": "#314257",
20 "LightMuted": "#93aecb",
21 "DarkVibrant": "#115d7b",
22 "LightVibrant": "#7ec2de"
23 },
24 "timestamp": "2021-04-15T08:34:50.000Z"
25 },
26 // more...
27 ]
28}
GET_INFO 获取图片详情
请求方法: GET
默认地址: http://localhost:3000/api/getInfo
参数(query):
Key | Value | 说明 |
---|---|---|
id | Number | 数据ID |
请求示例:
1http://localhost:3000/api/getInfo?id=1
返回示例:
1{
2 "info": {
3 "id": 1,
4 "title": "塞勒斯堡的玉米迷宫,宾夕法尼亚州,美国 (© Alex Potemkin/Getty Images)",
5 "date": "2023-10-23",
6 "base64": "data:image/jpeg;base64,/9j/4AAQSk...",
7 "url": {
8 "hd": "/img/2023/10/23/2023-10-23_hd.jpg",
9 "uhd": "/img/2023/10/23/2023-10-23_uhd.jpg",
10 "greyscale": "/img/2023/10/23/2023-10-23_hd_greyscale.jpg",
11 "thumbnail": "/img/2023/10/23/2023-10-23_hd_thumbnail_480_270.jpg",
12 "gaussian": "/img/2023/10/23/2023-10-23_hd_gaussian_20.jpg"
13 },
14 "color": {
15 "Vibrant": "#dd9413",
16 "DarkVibrant": "#70600e",
17 "LightVibrant": "#e9c36b",
18 "Muted": "#3c6c4c",
19 "DarkMuted": "#4e4c32",
20 "LightMuted": "#856314"
21 },
22 "timestamp": "2023-10-23T01:09:30.000Z"
23 }
24}
文章标题: Docker部署必应每日一图前后端
文章链接: https://blog.grew.cc/posts/bing-docker/
版权声明: 本博客所有文章除特别声明外,均采用CC BY-NC-SA 4.0。