+
Skip to content
Jetsung Chan edited this page Mar 4, 2025 · 5 revisions

开发

# 格式化
cargo fmt

# 检测
cargo fmt -- --check

构建

  1. 创建数据表
sqlite3 url_shortener.db < migrations/schema.sql
  1. 查看表
sqlite3 url_shortener.db ".tables";
sqlite3 url_shortener.db ".schema urls";
  1. 测试运行
cargo run
  1. 构建
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
Clone this wiki locally
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载