Skip to content

从源代码构建

如果您想要从头搭建一个保存站,请阅读此章节。

准备工作

在终端中执行如下命令:

bash
git clone https://github.com/laikit-dev/luogu-saver.git #克隆仓库
cd luogu-saver #进入目录
npm install #安装依赖
cp ormconfig.example.json ormconfig.json
cp settings.example.json settings.json
cp config.example.json config.json #极其重要
cp accounts.example.json accounts.json

修改默认值

修改 ormconfig.json

bash
vim ormconfig.json

代码:

json
{
    "type": "mariadb",
    "synchronize": true,
    "logging": true
}

修改 type 为您的数据库类型。(常见:mysqlmariadbpostgressqlite)。

修改 config.json

bash
vim config.json

代码:

json
export default {
	debug: true,
	port: 55086,
	database: {
		host: 'localhost',
		port: 3306,
		user: 'luogu_saver',
		password: 'your_password',
		database: 'luogu_save'
	},
	pagination: {
		search: 10,
		problem: 50
	},
	request: {
		concurrency: 2,
		maxRequestToken: 20,
		tokenRefillInterval: 1000
	},
	jwtSecret: 'your_jwt_secret',
     requestHeader: {
		'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36',
		'x-luogu-type': 'content-only'
	},
	queue: {
		processInterval: 200,
		maxLength: 500
	},
	recent: {
		article: {
			default: 20,
			max: 2000
		}
	},
	redis: {
		host: '127.0.0.1',
		port: 6379,
		password: null,
		db: 0
	},
	service: { // 用于爬取犇犇
		name: "luogu-saver",
		api_url: "https://api-benben.imken.dev",
		crawler_url: "https://spider-benben.imken.dev",
		ws_url: "wss://spider-benben.imken.dev",
		callback_timeout: 30000
	},
	rss: {
		article: {
			recentHours: 24
		}
	}
}
  • 修改 port 为您喜欢的端口。
  • 修改 database 为您自己的数据库信息。
  • 修改 jwtSecret 为您喜欢的密钥。
  • 修改 redis 为您自己的 Redis 信息。
  • 其他内容请自行修改。

    注意

    未安装或不想使用 Redis?请不要修改此部分内容,我们将自动取消 Redis 的使用,无需您手动配置。

不知道配置数据库,可以加入 QQ 群(1017248143)讨论。

修改 accounts.json

注意

这个玩意是爬取题解能否提交用的,需要用到您的洛谷账号。如果不需要题解信息爬取,可以不修改。请勿泄露 __client_id

bash
vim accounts.json

代码如下:

json
[
	{ "_uid": "114514", "__client_id": "1145141919810" }
]

修改 _uid 为您的洛谷 UID,修改 __client_id 为您的洛谷 Client ID。 如何获取 Client ID?

  1. 登录您的洛谷账号。

  2. 点击您键盘上的 F12 按钮,打开开发者工具。

  3. 点击 Application应用程序 标签 -> Cookie -> https://www.luogu.com.cn -> 找到 __client_id,复制即可。

  • Edge 浏览器示例:
  • Chrome 浏览器示例:

修改 settings.json

settings.json 是一些设置文件,一般情况下无需修改

运行

在终端中执行如下命令:

bash
node app.js

出现 Server is running on port xxx 说明成功。此时访问 http://127.0.0.1:端口号 即可。

管理员操作

添加/取消管理员

提示

仅需在第一次运行时用命令行工具添加管理员。后续可以直接在后台操作。需要先注册一个用户(Token)。注意您的 Token 与 luogu.me 并不相同两者互不影响。

命令行操作

进入您的数据库软件的终端,执行如下命令:

sql
use luogu_save;

这里的 luogu_save 为您数据库的名称。在这里获取数据库名称:

json
export default {
	debug: true,
	port: 55086,
	database: {
		host: 'localhost',
		port: 3306,
		user: 'luogu_saver',
		password: 'your_password',
		database: 'luogu_save' // here
	},
	pagination: {
		search: 10,
		problem: 50
	},
	// 后略……
}

接下来执行:

sql
UPDATE token SET role = 1 WHERE id = 'your_token';
/* role = 1 为管理员,如果要取消管理员,把 1 改成 0 即可!*/

比如你的 Token 是 dfhsugfuidsgfiusodfgafio,那么命令为:

sql
/* 给予管理权 */
UPDATE token SET role = 1 WHERE id = 'dfhsugfuidsgfiusodfgafio';
/* 取消管理权 */
UPDATE token SET role = 0 WHERE id = 'dfhsugfuidsgfiusodfgafio';

没有傻瓜不会了吧。

后台操作

请先确保您的账号是管理员。

登录您的账号,进入后台(https://127.0.0.1:端口号/admin),点击 Token 管理,找到您要操作的用户,点齿轮,再点 设为管理员

取消管理员同理。

其他操作

请在后台自行探索。操作很简单。

后续步骤

恭喜您,您已经搭建了一个保存站。

更新

请在终端中执行如下命令:

bash
git pull
npm install #以防缺少新依赖,小版本更新不需要(节省时间)
node app.js #启动

配置 PM2

PM2 是一个 Node.js 的进程管理工具,它可以帮助您管理您的 Node.js 应用程序。

管理后台的重启服务功能也依赖 PM2。

在终端执行:

bash
npm install -g pm2
pm2 start app.js --name luogu-saver

即可。

配置 Systemctl

Systemctl 是一个 Linux 的进程管理工具,它可以帮助您管理您的 Node.js 应用程序。

AI 声明

此部分内容由 AI 生成,未经验证。

创建systemd服务文件

bash
# 创建服务文件
sudo nano /etc/systemd/system/luogu-saver.service

服务文件内容

ini
[Unit]
Description=Luogu Saver Node.js Application
After=network.target

[Service]
Type=simple
User=你的用户名
WorkingDirectory=/path/to/luogu-saver
ExecStart=/usr/bin/node app.js
Restart=always
RestartSec=10
Environment=NODE_ENV=production

[Install]
WantedBy=multi-user.target

启用和启动服务

bash
# 重新加载systemd配置
sudo systemctl daemon-reload

# 启用服务(开机自启)
sudo systemctl enable luogu-saver

# 启动服务
sudo systemctl start luogu-saver

# 检查状态
sudo systemctl status luogu-saver

基于 GPL-3.0 协议开源