A read-only RESTful interface for HydroOJ, enabling seamless remote access via a companion CLI client or any HTTP consumer.
Source: github.com/felixtsu/hydro-restful-api · Maintainer: @felixtsu
- Read-only Access: Securely expose problems, submissions, homework, and contests.
- JWT Authentication: Industry-standard token-based security via
POST /rest-api/login. - Companion CLI: A full-featured Node.js CLI tool for terminal-based OJ interaction.
- Lightweight Addon: Runs directly within the HydroOJ process without extra ports.
| Component | Role | Location |
|---|---|---|
| Server addon | Runs inside the HydroOJ Node process. Registers /rest-api/* (read-only data + POST /rest-api/login for JWT). Only the Hydro server needs this. |
addon/ (npm: hydrooj-rest-api) |
| CLI client | Node.js 18+ tool for students, teachers, or scripts. Talks to your site over HTTPS; stores a token under ~/.config/hydrooj_cli/. Command: hydrooj-cli. |
cli/ts/ (npm: hydrooj-cli) |
These are two npm packages with independent versions. Publish or upgrade them separately.
Standard Installation (from npm):
# 1) Install into Hydro's home (`~/.hydro`), not globally.
cd ~/.hydro
npm install hydrooj-rest-api
# 2) Register the addon by absolute path (the folder that contains package.json):
ADDON_DIR="$(pwd)/node_modules/hydrooj-rest-api"
hydrooj addon add "$ADDON_DIR"
# 3) Restart HydroOJ after installing the addon.Development / local install
- Clone this repo and link the addon into your Hydro tree (see also Hydro plugins):
cd addon && npm link cd /path/to/hydrooj && npm link hydrooj-rest-api
- Security: Set
JWT_SECRET(see Security). - Restart HydroOJ.
CLI from source (no global publish needed):
cd cli/ts && npm install && npm run build && npm link
hydrooj-cli helpConfigure ~/.config/hydrooj_cli/config.json with baseUrl / base_url, or set HYDRO_API_URL. If your site uses /d/<domain>/ in the browser, include that path in the base URL.
npm install -g hydrooj-cli
hydrooj-cli login
hydrooj-cli listThe addon uses JSON Web Tokens (JWT) for authentication.
JWT_SECRET: Must be set in production to prevent token forgery.- How to set:
- Environment Variable: Set
JWT_SECRETin your shell or process manager (e.g.,export JWT_SECRET=$(openssl rand -base64 32)). - Hydro Panel: After installing the addon, go to System Settings -> Addon Config ->
hydrooj-rest-apiand enter yourjwtSecret.
- Environment Variable: Set
- After Installation: You must restart the HydroOJ process (e.g.,
pm2 restart hydro) for the secret and the addon to take effect. Verify by visitingyour-site.com/rest-api/problems; it should return a 401 JSON error instead of a 404 or a standard HTML page.
Generate a secure secret:
openssl rand -base64 32Detailed API specifications, including all endpoints and parameters, are available in SPEC.md.
源码: github.com/felixtsu/hydro-restful-api · 维护者:@felixtsu
- 只读访问:安全地暴露题目、提交记录、作业和比赛数据。
- JWT 认证:通过
POST /rest-api/login提供标准的令牌化安全认证。 - 配套 CLI:功能完备的 Node.js 命令行工具,支持终端交互。
- 轻量插件:直接在 HydroOJ 进程内运行,无需额外端口。
| 组件 | 角色 | 路径 |
|---|---|---|
| 服务端插件 | 跑在 HydroOJ 的 Node 进程内,注册 /rest-api/*(只读数据 + POST /rest-api/login 发 JWT)。只有跑 Hydro 的机器需要装。 |
addon/(npm:hydrooj-rest-api) |
| CLI 客户端 | Node 18+ 终端工具,供学生/教师或脚本使用;令牌存 ~/.config/hydrooj_cli/。命令:hydrooj-cli。 |
cli/ts/(npm:hydrooj-cli) |
二者是 两个 npm 包,版本可分别发布、升级。
标准安装 (从 npm 安装):
# 1) 进入 Hydro 的 home 目录(`~/.hydro`)安装,不要用 `-g` 全局安装。
cd ~/.hydro
npm install hydrooj-rest-api
# 2) 注册时请使用绝对路径(包含 package.json 的目录):
ADDON_DIR="$(pwd)/node_modules/hydrooj-rest-api"
hydrooj addon add "$ADDON_DIR"
# 3) 安装完成后请重启 HydroOJ 使插件生效。本地开发安装
- 克隆仓库并把插件链入 Hydro(详见 Hydro 插件):
cd addon && npm link cd /path/to/hydrooj && npm link hydrooj-rest-api
- 安全:配置
JWT_SECRET(见 安全配置)。 - 重启 HydroOJ。
从源码运行 CLI(无需先发 npm):
cd cli/ts && npm install && npm run build && npm link
hydrooj-cli help在 ~/.config/hydrooj_cli/config.json 中配置 baseUrl / base_url,或设置环境变量 HYDRO_API_URL。若站点 URL 带 /d/某域/,请写进 base URL。
全局安装:
npm install -g hydrooj-cli
hydrooj-cli login
hydrooj-cli list本插件使用 JWT 进行身份验证。
- 密钥安全:在生产环境中必须配置
JWT_SECRET以防令牌伪造。 - 配置方式:
- 环境变量:在 Shell 或进程管理器中设置
JWT_SECRET(如export JWT_SECRET=$(openssl rand -base64 32))。 - Hydro 后端面板:安装插件并重启后,在“系统设置” -> “插件配置” ->
hydrooj-rest-api中填入jwtSecret。
- 环境变量:在 Shell 或进程管理器中设置
- 安装后步骤:你必须重启 HydroOJ 进程(如
pm2 restart hydro)以使配置生效。可以通过访问你的域名/rest-api/problems进行验证,若返回 401 JSON 错误则表示插件已成功运行。
生成安全密钥示例:
openssl rand -base64 32关于所有端点和参数的详细说明,请参阅 SPEC.md。
Two packages: hydrooj-rest-api (addon/) and hydrooj-cli (cli/ts/).
- Account — Register at npmjs.com, then
npm login. With 2FA enabled, use an access token for CI. - Metadata — In each
package.json, keeprepository,bugs, andhomepageaccurate (owner felixtsu:https://github.com/felixtsu/hydro-restful-api). - Version — Bump
version(semver) in the package you are releasing before every publish. - CLI — From
cli/ts/,npm publish. TheprepublishOnlyscript runsnpm run buildsodist/is up to date. Verify withnpm install -g hydrooj-cli@<version>andhydrooj-cli help. - Addon — From
addon/,npm publish. On Hydro hosts, install withcd ~/.hydro && npm install hydrooj-rest-api@<version>and register via absolute path as in Quick Start. - Name conflicts — If an unscoped name is taken, use e.g.
@your-org/hydrooj-rest-apiand publish withnpm publish --access public.
Hydro’s plugin model is documented under Plugins.
Contributions are welcome! Please ensure any new API endpoints are documented in SPEC.md and supported in the CLI.