add sea-orm for sqlite db
This commit is contained in:
parent
b7152c47eb
commit
bb545ab742
@ -1,7 +1,7 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "actix-web-sample-app"
|
name = "actix-web-sample-app"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2018"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# 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"
|
rand = "0.8.4"
|
||||||
url = "2.2.2"
|
url = "2.2.2"
|
||||||
http = "0.2.5"
|
http = "0.2.5"
|
||||||
|
dotenv = "0.15"
|
||||||
futures = "0.3.18"
|
futures = "0.3.18"
|
||||||
serde = "1.0.130"
|
serde = "1.0.130"
|
||||||
serde_json = "1.0.71"
|
serde_json = "1.0.71"
|
||||||
serde_derive = "1.0.130"
|
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 {}
|
103
src/main.rs
103
src/main.rs
@ -9,14 +9,19 @@ use oauth2::{
|
|||||||
AuthUrl, ClientId, ClientSecret,
|
AuthUrl, ClientId, ClientSecret,
|
||||||
RedirectUrl, TokenUrl,
|
RedirectUrl, TokenUrl,
|
||||||
};
|
};
|
||||||
|
use std::time::{Duration};
|
||||||
use std::env;
|
use std::env;
|
||||||
|
use sea_orm::{{ DatabaseConnection, ConnectOptions }};
|
||||||
|
|
||||||
mod web_auth;
|
mod web_auth;
|
||||||
|
mod entity;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
pub oauth: BasicClient,
|
pub oauth: BasicClient,
|
||||||
pub api_base_url: String,
|
pub api_base_url: String,
|
||||||
pub tmpl: Tera
|
pub tmpl: Tera,
|
||||||
|
pub db: DatabaseConnection
|
||||||
}
|
}
|
||||||
|
|
||||||
fn index(session: Session, data: web::Data<AppState>) -> HttpResponse {
|
fn index(session: Session, data: web::Data<AppState>) -> HttpResponse {
|
||||||
@ -31,53 +36,65 @@ fn index(session: Session, data: web::Data<AppState>) -> HttpResponse {
|
|||||||
|
|
||||||
#[actix_rt::main]
|
#[actix_rt::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
HttpServer::new(|| {
|
dotenv::dotenv().ok();
|
||||||
let oauth2_client_id = ClientId::new(
|
let oauth2_client_id = ClientId::new(
|
||||||
env::var("OAUTH2_CLIENT_ID")
|
env::var("OAUTH2_CLIENT_ID")
|
||||||
.expect("Missing the OAUTH2_CLIENT_ID environment variable."),
|
.expect("Missing the OAUTH2_CLIENT_ID environment variable."),
|
||||||
);
|
);
|
||||||
let oauth2_client_secret = ClientSecret::new(
|
let oauth2_client_secret = ClientSecret::new(
|
||||||
env::var("OAUTH2_CLIENT_SECRET")
|
env::var("OAUTH2_CLIENT_SECRET")
|
||||||
.expect("Missing the OAUTH2_CLIENT_SECRET environment variable."),
|
.expect("Missing the OAUTH2_CLIENT_SECRET environment variable."),
|
||||||
);
|
);
|
||||||
let oauth2_server =
|
let oauth2_server =
|
||||||
env::var("OAUTH2_SERVER").expect("Missing the OAUTH2_SERVER environment variable.");
|
env::var("OAUTH2_SERVER").expect("Missing the OAUTH2_SERVER environment variable.");
|
||||||
|
|
||||||
let auth_url = AuthUrl::new(format!("https://{}/oauth2/v2.0/authorize", oauth2_server))
|
let auth_url = AuthUrl::new(format!("https://{}/oauth2/v2.0/authorize", oauth2_server))
|
||||||
.expect("Invalid authorization endpoint URL");
|
.expect("Invalid authorization endpoint URL");
|
||||||
let token_url = TokenUrl::new(format!("https://{}/oauth2/v2.0/token", oauth2_server))
|
let token_url = TokenUrl::new(format!("https://{}/oauth2/v2.0/token", oauth2_server))
|
||||||
.expect("Invalid token endpoint URL");
|
.expect("Invalid token endpoint URL");
|
||||||
|
|
||||||
let api_base_url = "https://graph.microsoft.com/v1.0".to_string();
|
let api_base_url = "https://graph.microsoft.com/v1.0".to_string();
|
||||||
|
|
||||||
// Set up the config for the OAuth2 process.
|
// Set up the config for the OAuth2 process.
|
||||||
let client = BasicClient::new(
|
let client = BasicClient::new(
|
||||||
oauth2_client_id,
|
oauth2_client_id,
|
||||||
Some(oauth2_client_secret),
|
Some(oauth2_client_secret),
|
||||||
auth_url,
|
auth_url,
|
||||||
Some(token_url),
|
Some(token_url),
|
||||||
)
|
)
|
||||||
// This example will be running its own server at 127.0.0.1:5000.
|
// This example will be running its own server at 127.0.0.1:5000.
|
||||||
.set_redirect_uri(
|
.set_redirect_uri(
|
||||||
RedirectUrl::new("http://localhost:5000/auth".to_string())
|
RedirectUrl::new("http://localhost:5000/auth".to_string())
|
||||||
.expect("Invalid redirect URL"),
|
.expect("Invalid redirect URL"),
|
||||||
);
|
);
|
||||||
|
|
||||||
let tera =
|
let tera =
|
||||||
Tera::new(
|
Tera::new(
|
||||||
concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*")
|
concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*")
|
||||||
).unwrap();
|
).unwrap();
|
||||||
|
|
||||||
let app_state = web::Data::new(
|
dotenv::dotenv().ok();
|
||||||
AppState {
|
let db_url = env::var("DATABASE_URL").expect("DATABASE_URL is not set in .env file");
|
||||||
oauth: client,
|
let mut opt = ConnectOptions::new(db_url);
|
||||||
api_base_url,
|
opt.max_connections(100)
|
||||||
tmpl: tera
|
.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,
|
||||||
|
db: conn
|
||||||
|
};
|
||||||
|
|
||||||
|
HttpServer::new(move || {
|
||||||
App::new()
|
App::new()
|
||||||
.app_data(app_state)
|
.app_data(web::Data::new(app_state.clone()))
|
||||||
.wrap(CookieSession::signed(&[0; 32]).secure(false))
|
.wrap(CookieSession::signed(&[0; 32]).secure(false))
|
||||||
.route("/", web::get().to(index))
|
.route("/", web::get().to(index))
|
||||||
.route("/login", web::get().to(web_auth::login))
|
.route("/login", web::get().to(web_auth::login))
|
||||||
|
Loading…
Reference in New Issue
Block a user