11个版本
0.1.12 | 2023年10月31日 |
---|---|
0.1.11 | 2023年10月19日 |
0.1.10 | 2023年9月2日 |
0.1.9 | 2023年8月10日 |
0.1.0 | 2019年8月4日 |
156 在 HTTP服务器
每月 78 次下载
335KB
633 行
gotcha
提供特色Web框架
旨在
- actix-web的所有功能
- 自动生成swagger API
- 内置消息机制
- 基于环境的配置系统,支持环境解析器
${ANY_ENV_VAR}
和路径变量${app.database.name}
,由 yaac 提供支持 - 日志系统
- 可选的Prometheus集成
- 基于sqlx的魔法ORM
- 基于cron的任务系统
入门
将依赖项添加到 Cargo.toml
gotcha = {version = "0.1"}
tokio = {version = "1", features = ["macros", 'rt-multi-thread']}
serde = {version="1", features=["derive"]}
use gotcha::{get, App, GotchaAppWrapperExt, GotchaCli, HttpServer, Responder};
use serde::Deserialize;
#[get("/")]
pub async fn hello_world() -> impl Responder {
"hello world"
}
#[derive(Debug, Deserialize, Clone)]
struct Config {}
#[tokio::main]
async fn main() {
GotchaCli::<_, Config>::new()
.server(|config| async move {
HttpServer::new(|| {
App::new()
.into_gotcha()
.service(hello_world)
.data(config)
.done()
})
.bind(("127.0.0.1", 8080))
.unwrap()
.run()
.await;
})
.run()
.await
}
Conservator ORM
Conservator ORM基于sqlx,目前仅支持postgres
#[derive(Debug, Deserialize, Serialize, Domain, FromRow)]
#[domain(table = "users")]
pub struct UserDomain {
#[domain(primary_key)]
pub id: Uuid,
pub username: String,
pub email: String,
pub password: String,
pub role: UserRole,
pub create_at: DateTime<Utc>,
pub last_login_at: DateTime<Utc>,
}
由结构体派生出的 Domain
会自动生成方法,如
find_by_id
返回可选实体fetch_one_by_id
返回实体或抛出异常fetch_all
返回所有实体create
接受Createable
以插入到表中
#[derive(Debug, Deserialize, Serialize, Creatable)]
pub struct NewUser {
pub username: String,
pub email: String,
pub password: String,
}
Createable
表示它可以由魔法ORM执行,使用 UserDomain::create(NewUser{...})
在用户表中创建新用户。
#[sql]
还提供了一些方便的方式来编写自定义SQL查询
use conservator::sql;
impl UserService {
#[sql(find)]
pub async fn find_user<E>(email: &str, executor: E) -> Result<Option<UserEntity>, Error> {
"select * from users where email = :email"
}
}
注意,与sqlx的 $1
不同,我们在SQL中使用参数 :email
,它可以在不需要任何修改的情况下用于本地的SQL执行工具,如IDEA。
依赖项
~23–35MB
~618K SLoC