# short-url **Repository Path**: andnnl/short-url ## Basic Information - **Project Name**: short-url - **Description**: 短链。。。。。。。。。 - **Primary Language**: Rust - **License**: MIT - **Default Branch**: v1 - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-12-23 - **Last Updated**: 2025-12-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 短链服务 [![Cargo fmt/test](https://github.com/ArronYR/short-url/actions/workflows/rust.yml/badge.svg?branch=main)](https://github.com/ArronYR/short-url/actions/workflows/rust.yml) 1. 支持生成短链接 2. 支持短链接访问 ## Run ```shell cargo run ``` ## Deploy ### 交叉编译部署(推荐) 项目支持使用 Cross 工具进行交叉编译,可以在本地编译出 Linux 可执行文件: #### 安装 Cross ```shell cargo install cross ``` #### 安装 Podman(推荐) Cross 工具默认使用 Docker,但推荐使用 Podman 作为容器引擎: **Windows 系统**: ```powershell # 使用 Chocolatey 安装 choco install podman # 或者从官网下载安装包 # https://podman.io/getting-started/installation.html ``` **Linux 系统**: ```bash # Ubuntu/Debian sudo apt update sudo apt install podman # CentOS/RHEL sudo yum install podman ``` #### 配置 Cross 使用 Podman 项目已配置 `Cross.toml` 文件,会自动使用 Podman 作为容器引擎: ```toml [build] engine = "podman" [target.x86_64-unknown-linux-musl] image = "ghcr.io/cross-rs/x86_64-unknown-linux-musl:main" [target.aarch64-unknown-linux-musl] image = "ghcr.io/cross-rs/aarch64-unknown-linux-musl:main" ``` **注意**:Cross 项目已更新镜像标签策略,使用 `main` 标签而非 `latest` 标签来获取最新的工具链版本。 #### Podman 初始化(仅首次需要) ```powershell # Windows 系统需要初始化 Podman Machine podman machine init podman machine start # 验证 Podman 正常工作 podman --version ``` #### 编译 Linux 版本 ```shell # 编译 x86_64 Linux 版本(使用默认配置) cross build --target x86_64-unknown-linux-musl --release # 编译 x86_64 Linux 版本(使用自定义配置文件) cross build --target x86_64-unknown-linux-musl --config Cross-linux.toml --release # 编译后的文件位置 target/x86_64-unknown-linux-musl/release/short_url # 验证编译结果(文件大小约 225MB,静态链接) ls -lh target/x86_64-unknown-linux-musl/release/short_url ``` #### 其他架构支持 ```shell # ARM64 Linux 版本(如果容器环境支持) cross build --target aarch64-unknown-linux-musl --release # ARMv7 Linux 版本 cross build --target armv7-unknown-linux-musleabihf --release ``` #### 部署步骤 1. **将编译好的二进制文件上传到 Linux 服务器** ```bash # 使用 SCP 上传文件(替换用户名和服务器地址) scp target/x86_64-unknown-linux-musl/release/short_url user@your-server:/home/user/ ``` 2. **在服务器上设置执行权限** ```bash ssh user@your-server "chmod +x /home/user/short_url" ``` 3. **创建环境变量文件** ```bash ssh user@your-server "cat > .env << 'EOF' DATABASE_URL=sqlite://short_url.db SERVER_PORT=8080 SERVER_HOST=0.0.0.0 EOF" ``` 4. **启动服务** ```bash ssh user@your-server "./short_url" ``` #### 常见问题排查 **Podman 安装问题**(Windows): ```powershell # 如果 Podman 命令找不到,检查安装路径 Get-ChildItem -Path "C:\" -Recurse -Name "podman.exe" -ErrorAction SilentlyContinue # 添加到 PATH 环境变量 $env:PATH += ";C:\Program Files\RedHat\Podman" # 验证安装 podman --version # 检查Podman状态 podman machine ls # 启动Podman machine podman machine start # 验证连接 podman info --format "{{ .Version.OsArch }}" ``` **Podman Machine 启动失败**: ```powershell # 重新初始化 Podman Machine podman machine stop podman machine rm podman machine init podman machine start ``` **Cross 编译错误**: ```shell # 清理缓存重新编译 cargo clean rm -rf target cross build --target x86_64-unknown-linux-musl --release ``` **权限问题**(Linux 服务器): ```bash # 确保文件有执行权限 chmod +x short_url # 检查文件完整性 file short_url ``` # 安装必要的运行时库 sudo apt-get install libc6-dev ``` ### 环境配置 项目的数据存储使用MySQL数据库,在运行或部署时需要通过环境变量指定相关的配置信息: ```shell PORT=80 ORIGIN=https://s.cn DB_HOST=127.0.0.1 DB_PORT=3306 DB_USERNAME=short_url DB_PASSWORD=****** RUST_LOG=info CACHE_LIVE_TIME=7200 CACHE_MAX_CAP=1000 TOKEN=0uLcr3xYI2ndKTZv ``` ```shell PORT=8080 ORIGIN=https://s.cn DB_HOST=192.168.5.87 DB_PORT=3306 DB_USERNAME=root DB_PASSWORD=123456 DB_NAME=short_url RUST_LOG=info CACHE_LIVE_TIME=7200 CACHE_MAX_CAP=1000 TOKEN=0uLcr3xYI2ndKTZv ``` ### Windows环境下运行配置 在Windows系统下,您可以通过以下方式设置环境变量: **方式1:命令行设置(临时)** ```cmd set PORT=8080 set ORIGIN=https://s.cn set DB_HOST=127.0.0.1 set DB_PORT=3306 set DB_USERNAME=root set DB_PASSWORD=123456 set DB_NAME=short_url set RUST_LOG=info set CACHE_LIVE_TIME=7200 set CACHE_MAX_CAP=1000 set TOKEN=your_token_here cargo run ``` **方式2:PowerShell设置(临时)** ```powershell $env:PORT="8080" $env:ORIGIN="https://s.cn" $env:DB_HOST="127.0.0.1" $env:DB_PORT="3306" $env:DB_USERNAME="root" $env:DB_PASSWORD="123456" $env:DB_NAME="short_url" $env:RUST_LOG="info" $env:CACHE_LIVE_TIME="7200" $env:CACHE_MAX_CAP="1000" $env:TOKEN="uNUVajILOVwnK3kj" cargo run ``` **方式3:创建.env文件(推荐)** 在项目根目录创建`.env`文件,内容如下: ``` PORT=8080 ORIGIN=https://s.cn DB_HOST=127.0.0.1 DB_PORT=3306 DB_USERNAME=root DB_PASSWORD=123456 DB_NAME=short_url RUST_LOG=info CACHE_LIVE_TIME=7200 CACHE_MAX_CAP=1000 TOKEN=your_token_here ``` 程序启动时会自动加载`.env`文件中的环境变量,无需手动设置。 - 可以直接修改代码中`init_config`设置默认值,就不用设置环境变量了。 ### Docker 部署 项目提供了完整的 Docker 部署方案,包含前端、后端和数据库: #### 快速部署 ```bash # 给部署脚本执行权限 chmod +x deploy.sh # 一键部署 ./deploy.sh ``` #### 手动部署 ```bash # 构建并启动服务 docker-compose up -d # 查看服务状态 docker-compose ps # 查看日志 docker-compose logs -f ``` 详细配置请参考 `DOCKER_DEPLOYMENT.md` 文档。 ### Nginx 部署 对于生产环境,推荐使用 Nginx 作为反向代理: 1. 构建前端项目:`cd web && npm run build` 2. 配置 Nginx(参考 `nginx.conf`) 3. 启动后端服务和 Nginx 详细配置请参考 `DEPLOYMENT.md` 文档。 ## Usage 1. 生成短链接 ```shell # token 必须提供,用来控制非法调用,支持在环境变量中设置 # 支持批量生成,以原始链接的`md5`值作为返回内容的`key` > request curl -L 'http://127.0.0.1/api/generate' \ -H 'Api-Secret: 1FIsiEpxQo5l7H' \ -H 'Token: 53ROYinHId9qke' \ -H 'Content-Type: application/json' \ -d '{ "urls": [ "https://dtsnew.console.aliyun.com/connect/cn-beijing", "https://dtsnew.console.aliyun.com/connect/cn-hangzhou", "https://dtsnew.console.aliyun.com/connect/cn-zhangjiakou", "https://dtsnew.console.aliyun.com/connect/cn-shanghai" ], "expiredTs": 0 }' > response { "f3b5941fb8a51565526a5353242ab0d4": "https://127.0.0.1/mWejF", "4bce96459bcb6c6dc8e3428bc6dbec98": "https://127.0.0.1/WFu7j", "35798d470756c2f87a10fc884d9df82d": "https://127.0.0.1/d5GHE", "b4962b8b32c1684aa7eadd0b3cdc93ab": "https://127.0.0.1/KsQsa" } ``` 2. 访问短链接 浏览器打开生成的短链接 ## Preview ![Preview](./preview.png)