add sea-orm for sqlite db
This commit is contained in:
parent
b7152c47eb
commit
bb545ab742
@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "actix-web-sample-app"
|
||||
version = "0.1.0"
|
||||
edition = "2018"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
@ -17,8 +17,11 @@ base64 = "0.13.0"
|
||||
rand = "0.8.4"
|
||||
url = "2.2.2"
|
||||
http = "0.2.5"
|
||||
|
||||
dotenv = "0.15"
|
||||
futures = "0.3.18"
|
||||
serde = "1.0.130"
|
||||
serde_json = "1.0.71"
|
||||
serde_derive = "1.0.130"
|
||||
|
||||
sea-orm = { version = "^0", features = [ "sqlx-sqlite", "runtime-actix-native-tls", "macros" ], default-features = false }
|
||||
|
||||
|
BIN
database.db
Normal file
BIN
database.db
Normal file
Binary file not shown.
1
migrations/20211206152819_SampleTable.sql
Normal file
1
migrations/20211206152819_SampleTable.sql
Normal file
@ -0,0 +1 @@
|
||||
-- Add migration script here
|
0
src/actix-admin/mod.rs
Normal file
0
src/actix-admin/mod.rs
Normal file
38
src/entity/mod.rs
Normal file
38
src/entity/mod.rs
Normal file
@ -0,0 +1,38 @@
|
||||
// setup
|
||||
use sea_orm::sea_query::{ColumnDef, TableCreateStatement};
|
||||
use sea_orm::{error::*, sea_query, ConnectionTrait, DbConn, ExecResult};
|
||||
|
||||
mod post;
|
||||
pub use post::Entity as Post;
|
||||
|
||||
// setup
|
||||
async fn create_table(db: &DbConn, stmt: &TableCreateStatement) -> Result<ExecResult, DbErr> {
|
||||
let builder = db.get_database_backend();
|
||||
db.execute(builder.build(stmt)).await
|
||||
}
|
||||
|
||||
pub async fn create_post_table(db: &DbConn) -> Result<ExecResult, DbErr> {
|
||||
let stmt = sea_query::Table::create()
|
||||
.table(post::Entity)
|
||||
.if_not_exists()
|
||||
.col(
|
||||
ColumnDef::new(post::Column::Id)
|
||||
.integer()
|
||||
.not_null()
|
||||
.auto_increment()
|
||||
.primary_key(),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(post::Column::Title)
|
||||
.string()
|
||||
.not_null(),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(post::Column::Text)
|
||||
.string()
|
||||
.not_null(),
|
||||
)
|
||||
.to_owned();
|
||||
|
||||
create_table(db, &stmt).await
|
||||
}
|
18
src/entity/post.rs
Normal file
18
src/entity/post.rs
Normal file
@ -0,0 +1,18 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize)]
|
||||
#[sea_orm(table_name = "posts")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
#[serde(skip_deserializing)]
|
||||
pub id: i32,
|
||||
pub title: String,
|
||||
#[sea_orm(column_type = "Text")]
|
||||
pub text: String,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
33
src/main.rs
33
src/main.rs
@ -9,14 +9,19 @@ use oauth2::{
|
||||
AuthUrl, ClientId, ClientSecret,
|
||||
RedirectUrl, TokenUrl,
|
||||
};
|
||||
use std::time::{Duration};
|
||||
use std::env;
|
||||
use sea_orm::{{ DatabaseConnection, ConnectOptions }};
|
||||
|
||||
mod web_auth;
|
||||
mod entity;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AppState {
|
||||
pub oauth: BasicClient,
|
||||
pub api_base_url: String,
|
||||
pub tmpl: Tera
|
||||
pub tmpl: Tera,
|
||||
pub db: DatabaseConnection
|
||||
}
|
||||
|
||||
fn index(session: Session, data: web::Data<AppState>) -> HttpResponse {
|
||||
@ -31,7 +36,7 @@ fn index(session: Session, data: web::Data<AppState>) -> HttpResponse {
|
||||
|
||||
#[actix_rt::main]
|
||||
async fn main() {
|
||||
HttpServer::new(|| {
|
||||
dotenv::dotenv().ok();
|
||||
let oauth2_client_id = ClientId::new(
|
||||
env::var("OAUTH2_CLIENT_ID")
|
||||
.expect("Missing the OAUTH2_CLIENT_ID environment variable."),
|
||||
@ -68,16 +73,28 @@ async fn main() {
|
||||
concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*")
|
||||
).unwrap();
|
||||
|
||||
let app_state = web::Data::new(
|
||||
AppState {
|
||||
dotenv::dotenv().ok();
|
||||
let db_url = env::var("DATABASE_URL").expect("DATABASE_URL is not set in .env file");
|
||||
let mut opt = ConnectOptions::new(db_url);
|
||||
opt.max_connections(100)
|
||||
.min_connections(5)
|
||||
.connect_timeout(Duration::from_secs(8))
|
||||
.idle_timeout(Duration::from_secs(8))
|
||||
.sqlx_logging(true);
|
||||
|
||||
let conn = sea_orm::Database::connect(opt).await.unwrap();
|
||||
let _ = entity::create_post_table(&conn).await;
|
||||
|
||||
let app_state = AppState {
|
||||
oauth: client,
|
||||
api_base_url,
|
||||
tmpl: tera
|
||||
}
|
||||
);
|
||||
tmpl: tera,
|
||||
db: conn
|
||||
};
|
||||
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.app_data(app_state)
|
||||
.app_data(web::Data::new(app_state.clone()))
|
||||
.wrap(CookieSession::signed(&[0; 32]).secure(false))
|
||||
.route("/", web::get().to(index))
|
||||
.route("/login", web::get().to(web_auth::login))
|
||||
|
Loading…
Reference in New Issue
Block a user