forked from techninja8/rust-url-link-shortener
-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Jetsung Chan edited this page Mar 4, 2025
·
5 revisions
# 格式化
cargo fmt
# 检测
cargo fmt -- --check
- 创建数据表
sqlite3 url_shortener.db < migrations/schema.sql
- 查看表
sqlite3 url_shortener.db ".tables";
sqlite3 url_shortener.db ".schema urls";
- 测试运行
cargo run
- 构建
cargo build --release
查看服务
$ curl http://127.0.0.1:3000/
Short URL Service
创建短链
# 随机生成
$ curl -X POST http://127.0.0.1:3000/shorten -d '{"url": "https://github.com/forkdo/shorturl"}' -H "Content-Type: application/json"
{"short_code":"6ef212fa","short_url":"http://localhost:3000/6ef212fa"}
# 自定义短码
$ curl -X POST http://127.0.0.1:3000/shorten -d '{"url": "https://github.com/forkdo/shorturl", "code": "shorturl"}' -H "Content-Type: application/json"
{"short_code":"shorturl","short_url":"http://localhost:3000/shorturl"}
查看短链
$ curl -I http://localhost:3000/6ef212fa
HTTP/1.1 307 Temporary Redirect
location: https://github.com/forkdo/shorturl
content-length: 0
date: Tue, 04 Mar 2025 10:55:09 GMT
获取短链
$ curl http://localhost:3000/get/6ef212fa
{"short_code":"6ef212fa","original_url":"https://github.com/forkdo/shorturl"}
查询数据库中的数据
$ sqlite3 url_shortener.db 'select * from urls'
1|a6b070e5|https://github.com/forkdo/shorturl
2|bb81d89c|https://github.com/jetsung
3|f96eaa5a|https://gitcode.com/jetsung