Skip to content
Rust实现的pastebin
Rust CSS HTML
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
static
templates
.editorconfig
.env.default
.gitignore
Cargo.toml
Makefile.toml
README.md
踩坑与解决.md

README.md

简易PASTEBIN服务

因为被 borrow checker 折磨,该项目就这样摸了

大致参考 https://zhuanlan.zhihu.com/p/73961522 这篇文章依样画葫芦。

加了点注释,强化了封装。把 Web Server 换成了 Rocket

Web界面已上线,欢迎访问 https://paste.fun4go.top 进行尝鲜。

截图

创建paste

显示paste

部署

$ cargo build -- release
$ ./target/release/naive-pastebin

环境变量

Prefix: PASTEBIN_

Shared Variable

var default unit description
ADDR localhost Binding address
PORT 8085 Binding port
CRYPT_KEY fun4wut Crypto key for short url
MAX_POST_SIZE 32768 byte Max length of POST request body
DOMAIN ADDR:PORT Domain where you deploy your site

Built-in Memory Store

var default unit description
MAX_STORE_SIZE 104857600 byte An ambiguous size count for controlling server memory usage
CLEAN_DURATION 60000 millisecond GC interval

API

存储

  1. Request

    POST /api/save?<title>&[exp]

    exp为过期时间,单位为秒,默认为不过期

    interface Request {
        content: string,
    }
  2. Response

    interface Response {
        code: number,
        msg: string,
        data: {
            key: string
        }
    }

查找

  1. Request

    GET /api/find/<key>
  2. Response

    interface Response {
       code: number,
       msg: string,
       data: {
            title: string,
            lang: string,
            content: string,
            saving_time: number,
            expiration: number,
            view_count: number
       }
    }

直接返回 raw

GET /raw/<key>

Benchmark

find

$ ab -n 13000 -c 50 http://localhost:8085/api/save/xxxx

save

$ ab -n 13000 -c 50 -p `json_file` -T "application/json" http://localhost:8085/api/save\?title\=233\&exp\=86400

TODO

  • 单元测试
  • 健壮的Error机制
  • 考虑对命令行用户的友好度
  • Web界面
  • iframe / js脚本 形式嵌入【参照 https://pastebin.com/
  • 前端代码高亮
  • raw 字符串显示
  • 核心代码优化,减少 clone 造成的内存开销
  • 过期时间可为无限
  • 数据备份【 snapshot / log
  • 冷数据存入硬盘
  • 硬盘写操作的异步化
  • 编辑/删除/回滚功能【使用一个密钥来验证,密钥只会在新建时出现,保存在浏览器 localStorage 中】
  • 历史版本显示
You can’t perform that action at this time.