Introduce Database handling through SeaORM
This commit is contained in:
parent
37331cd24d
commit
e4765a20ef
12 changed files with 169 additions and 2 deletions
5
src/entity/mod.rs
Normal file
5
src/entity/mod.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3
|
||||
|
||||
pub mod prelude;
|
||||
|
||||
pub mod user;
|
||||
3
src/entity/prelude.rs
Normal file
3
src/entity/prelude.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3
|
||||
|
||||
pub use super::user::Entity as User;
|
||||
19
src/entity/user.rs
Normal file
19
src/entity/user.rs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3
|
||||
|
||||
use sea_orm::entity::prelude::*;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
||||
#[sea_orm(table_name = "user")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key, auto_increment = false)]
|
||||
pub id: Uuid,
|
||||
#[sea_orm(unique)]
|
||||
pub name: String,
|
||||
#[sea_orm(column_type = "Binary(BlobSize::Blob(None))")]
|
||||
pub password: Vec<u8>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
15
src/main.rs
15
src/main.rs
|
|
@ -4,9 +4,12 @@ use axum::Router;
|
|||
use base64::{engine::general_purpose, Engine};
|
||||
use config::{Config, ConfigError};
|
||||
use jwt_simple::prelude::HS256Key;
|
||||
use migration::{Migrator, MigratorTrait};
|
||||
use sea_orm::{ConnectOptions, Database, DatabaseConnection};
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
use tower_http::services::ServeDir;
|
||||
|
||||
pub(crate) mod entity;
|
||||
mod routes;
|
||||
|
||||
#[derive(Clone)]
|
||||
|
|
@ -69,6 +72,8 @@ struct Settings {
|
|||
port: u16,
|
||||
api_allowed: Option<String>,
|
||||
serve_app: Option<PathBuf>,
|
||||
database_url: String,
|
||||
sqlx_logging: bool,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
|
|
@ -79,6 +84,7 @@ impl Settings {
|
|||
.set_default("port", "8085")?
|
||||
.set_default("api_allowed", None::<String>)?
|
||||
.set_default("serve_app", None::<String>)?
|
||||
.set_default("sqlx_logging", false)?
|
||||
.build()?;
|
||||
|
||||
cfg.try_deserialize()
|
||||
|
|
@ -87,6 +93,7 @@ impl Settings {
|
|||
|
||||
struct AppState {
|
||||
jwt_secret: Base64,
|
||||
db: DatabaseConnection,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
|
|
@ -102,10 +109,16 @@ async fn main() -> anyhow::Result<()> {
|
|||
|
||||
let addr: SocketAddr = format!("{}:{}", config.host, config.port).parse()?;
|
||||
|
||||
let mut opt = ConnectOptions::new(config.database_url);
|
||||
opt.sqlx_logging(config.sqlx_logging);
|
||||
|
||||
let state = Arc::new(AppState {
|
||||
jwt_secret: config.jwt_secret,
|
||||
db: Database::connect(opt).await?,
|
||||
});
|
||||
|
||||
Migrator::up(&state.db, None).await?;
|
||||
|
||||
let router = Router::new()
|
||||
.nest(
|
||||
"/api",
|
||||
|
|
@ -118,6 +131,8 @@ async fn main() -> anyhow::Result<()> {
|
|||
Some(path) => router.fallback_service(ServeDir::new(path)),
|
||||
};
|
||||
|
||||
tracing::info!("Listening on {addr}");
|
||||
|
||||
Ok(axum::Server::bind(&addr)
|
||||
.serve(router.into_make_service())
|
||||
.await?)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue