Use Figment config from Rocket
This commit is contained in:
parent
c673017b97
commit
bf595ca926
@ -48,15 +48,14 @@ async fn create(conn: Connection<'_, Db>, post_form: Form<post::Model>) -> Flash
|
||||
}
|
||||
|
||||
#[post("/<id>", data = "<post_form>")]
|
||||
async fn update(conn: Connection<'_, Db>, id: i32, post_form: Form<post::Model>) -> Flash<Redirect> {
|
||||
async fn update(
|
||||
conn: Connection<'_, Db>,
|
||||
id: i32,
|
||||
post_form: Form<post::Model>,
|
||||
) -> Flash<Redirect> {
|
||||
let db = conn.into_inner();
|
||||
|
||||
let post: post::ActiveModel = Post::find_by_id(id)
|
||||
.one(db)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.into();
|
||||
let post: post::ActiveModel = Post::find_by_id(id).one(db).await.unwrap().unwrap().into();
|
||||
|
||||
let form = post_form.into_inner();
|
||||
|
||||
@ -133,12 +132,7 @@ async fn edit(conn: Connection<'_, Db>, id: i32) -> Template {
|
||||
async fn delete(conn: Connection<'_, Db>, id: i32) -> Flash<Redirect> {
|
||||
let db = conn.into_inner();
|
||||
|
||||
let post: post::ActiveModel = Post::find_by_id(id)
|
||||
.one(db)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.into();
|
||||
let post: post::ActiveModel = Post::find_by_id(id).one(db).await.unwrap().unwrap().into();
|
||||
|
||||
post.delete(db).await.unwrap();
|
||||
|
||||
|
@ -1,5 +1,7 @@
|
||||
use async_trait::async_trait;
|
||||
use sea_orm::ConnectOptions;
|
||||
use sea_orm_rocket::{rocket::figment::Figment, Config, Database};
|
||||
use std::time::Duration;
|
||||
|
||||
#[derive(Database, Debug)]
|
||||
#[database("sea_orm")]
|
||||
@ -18,7 +20,15 @@ impl sea_orm_rocket::Pool for SeaOrmPool {
|
||||
|
||||
async fn init(figment: &Figment) -> Result<Self, Self::Error> {
|
||||
let config = figment.extract::<Config>().unwrap();
|
||||
let conn = sea_orm::Database::connect(&config.url).await.unwrap();
|
||||
let mut options: ConnectOptions = config.url.into();
|
||||
options
|
||||
.max_connections(config.max_connections as u32)
|
||||
.min_connections(config.min_connections.unwrap_or_default())
|
||||
.connect_timeout(Duration::from_secs(config.connect_timeout));
|
||||
if let Some(idle_timeout) = config.idle_timeout {
|
||||
options.idle_timeout(Duration::from_secs(idle_timeout));
|
||||
}
|
||||
let conn = sea_orm::Database::connect(options).await.unwrap();
|
||||
|
||||
Ok(SeaOrmPool { conn })
|
||||
}
|
||||
|
@ -115,22 +115,26 @@ impl ConnectOptions {
|
||||
}
|
||||
|
||||
/// Set the maximum number of connections of the pool
|
||||
pub fn max_connections(&mut self, value: u32) {
|
||||
pub fn max_connections(&mut self, value: u32) -> &mut Self {
|
||||
self.max_connections = Some(value);
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the minimum number of connections of the pool
|
||||
pub fn min_connections(&mut self, value: u32) {
|
||||
pub fn min_connections(&mut self, value: u32) -> &mut Self {
|
||||
self.min_connections = Some(value);
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the timeout duration when acquiring a connection
|
||||
pub fn connect_timeout(&mut self, value: Duration) {
|
||||
pub fn connect_timeout(&mut self, value: Duration) -> &mut Self {
|
||||
self.connect_timeout = Some(value);
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the idle duration before closing a connection
|
||||
pub fn idle_timeout(&mut self, value: Duration) {
|
||||
pub fn idle_timeout(&mut self, value: Duration) -> &mut Self {
|
||||
self.idle_timeout = Some(value);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user