Use Arc to pass database connection to requests
This commit is contained in:
parent
1a4392dd0b
commit
31bc662f3f
@ -1,6 +1,8 @@
|
|||||||
# Actix with SeaORM example app
|
# Actix with SeaORM example app
|
||||||
|
|
||||||
Rus server with auto-reloading:
|
Edit `.env` to point to your database.
|
||||||
|
|
||||||
|
Run server with auto-reloading:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
cargo install systemfd
|
cargo install systemfd
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
// use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};
|
// use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use actix_files as fs;
|
use actix_files as fs;
|
||||||
use actix_http::{body::Body, Response};
|
use actix_http::{body::Body, Response};
|
||||||
@ -12,7 +13,7 @@ use actix_web::{
|
|||||||
use listenfd::ListenFd;
|
use listenfd::ListenFd;
|
||||||
use sea_orm::entity::*;
|
use sea_orm::entity::*;
|
||||||
use sea_orm::query::*;
|
use sea_orm::query::*;
|
||||||
use sea_orm::EntityTrait;
|
use sea_orm::{DatabaseConnection, EntityTrait};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::env;
|
use std::env;
|
||||||
use tera::Tera;
|
use tera::Tera;
|
||||||
@ -23,9 +24,10 @@ mod setup;
|
|||||||
|
|
||||||
const DEFAULT_POSTS_PER_PAGE: usize = 25;
|
const DEFAULT_POSTS_PER_PAGE: usize = 25;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
struct AppState {
|
struct AppState {
|
||||||
db_url: String,
|
|
||||||
templates: tera::Tera,
|
templates: tera::Tera,
|
||||||
|
conn: Arc<DatabaseConnection>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
@ -43,11 +45,11 @@ struct FlashData {
|
|||||||
#[get("/")]
|
#[get("/")]
|
||||||
async fn list(
|
async fn list(
|
||||||
req: HttpRequest,
|
req: HttpRequest,
|
||||||
data: web::Data<AppState>,
|
data: web::Data<Arc<AppState>>,
|
||||||
opt_flash: Option<actix_flash::Message<FlashData>>,
|
opt_flash: Option<actix_flash::Message<FlashData>>,
|
||||||
) -> Result<HttpResponse, Error> {
|
) -> Result<HttpResponse, Error> {
|
||||||
let template = &data.templates;
|
let template = &data.templates;
|
||||||
let conn = sea_orm::Database::connect(&data.db_url).await.unwrap();
|
let conn = &data.conn;
|
||||||
|
|
||||||
// get params
|
// get params
|
||||||
let params = web::Query::<Params>::from_query(req.query_string()).unwrap();
|
let params = web::Query::<Params>::from_query(req.query_string()).unwrap();
|
||||||
@ -79,7 +81,7 @@ async fn list(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[get("/new")]
|
#[get("/new")]
|
||||||
async fn new(data: web::Data<AppState>) -> Result<HttpResponse, Error> {
|
async fn new(data: web::Data<Arc<AppState>>) -> Result<HttpResponse, Error> {
|
||||||
let template = &data.templates;
|
let template = &data.templates;
|
||||||
let ctx = tera::Context::new();
|
let ctx = tera::Context::new();
|
||||||
let body = template
|
let body = template
|
||||||
@ -90,10 +92,10 @@ async fn new(data: web::Data<AppState>) -> Result<HttpResponse, Error> {
|
|||||||
|
|
||||||
#[post("/")]
|
#[post("/")]
|
||||||
async fn create(
|
async fn create(
|
||||||
data: web::Data<AppState>,
|
data: web::Data<Arc<AppState>>,
|
||||||
post_form: web::Form<post::Model>,
|
post_form: web::Form<post::Model>,
|
||||||
) -> actix_flash::Response<HttpResponse, FlashData> {
|
) -> actix_flash::Response<HttpResponse, FlashData> {
|
||||||
let conn = sea_orm::Database::connect(&data.db_url).await.unwrap();
|
let conn = &data.conn;
|
||||||
|
|
||||||
let form = post_form.into_inner();
|
let form = post_form.into_inner();
|
||||||
|
|
||||||
@ -115,8 +117,8 @@ async fn create(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[get("/{id}")]
|
#[get("/{id}")]
|
||||||
async fn edit(data: web::Data<AppState>, id: web::Path<i32>) -> Result<HttpResponse, Error> {
|
async fn edit(data: web::Data<Arc<AppState>>, id: web::Path<i32>) -> Result<HttpResponse, Error> {
|
||||||
let conn = sea_orm::Database::connect(&data.db_url).await.unwrap();
|
let conn = &data.conn;
|
||||||
let template = &data.templates;
|
let template = &data.templates;
|
||||||
|
|
||||||
let post: post::Model = Post::find_by_id(id.into_inner())
|
let post: post::Model = Post::find_by_id(id.into_inner())
|
||||||
@ -136,11 +138,11 @@ async fn edit(data: web::Data<AppState>, id: web::Path<i32>) -> Result<HttpRespo
|
|||||||
|
|
||||||
#[post("/{id}")]
|
#[post("/{id}")]
|
||||||
async fn update(
|
async fn update(
|
||||||
data: web::Data<AppState>,
|
data: web::Data<Arc<AppState>>,
|
||||||
id: web::Path<i32>,
|
id: web::Path<i32>,
|
||||||
post_form: web::Form<post::Model>,
|
post_form: web::Form<post::Model>,
|
||||||
) -> actix_flash::Response<HttpResponse, FlashData> {
|
) -> actix_flash::Response<HttpResponse, FlashData> {
|
||||||
let conn = sea_orm::Database::connect(&data.db_url).await.unwrap();
|
let conn = &data.conn;
|
||||||
let form = post_form.into_inner();
|
let form = post_form.into_inner();
|
||||||
|
|
||||||
post::ActiveModel {
|
post::ActiveModel {
|
||||||
@ -162,10 +164,10 @@ async fn update(
|
|||||||
|
|
||||||
#[post("/delete/{id}")]
|
#[post("/delete/{id}")]
|
||||||
async fn delete(
|
async fn delete(
|
||||||
data: web::Data<AppState>,
|
data: web::Data<Arc<AppState>>,
|
||||||
id: web::Path<i32>,
|
id: web::Path<i32>,
|
||||||
) -> actix_flash::Response<HttpResponse, FlashData> {
|
) -> actix_flash::Response<HttpResponse, FlashData> {
|
||||||
let conn = sea_orm::Database::connect(&data.db_url).await.unwrap();
|
let conn = &data.conn;
|
||||||
|
|
||||||
let post: post::ActiveModel = Post::find_by_id(id.into_inner())
|
let post: post::ActiveModel = Post::find_by_id(id.into_inner())
|
||||||
.one(&conn)
|
.one(&conn)
|
||||||
@ -199,15 +201,16 @@ async fn main() -> std::io::Result<()> {
|
|||||||
// create post table if not exists
|
// create post table if not exists
|
||||||
let conn = sea_orm::Database::connect(&db_url).await.unwrap();
|
let conn = sea_orm::Database::connect(&db_url).await.unwrap();
|
||||||
let _ = setup::create_post_table(&conn).await;
|
let _ = setup::create_post_table(&conn).await;
|
||||||
|
let templates = Tera::new(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*")).unwrap();
|
||||||
|
let state = AppState {
|
||||||
|
templates: templates,
|
||||||
|
conn: Arc::new(conn),
|
||||||
|
};
|
||||||
|
|
||||||
let mut listenfd = ListenFd::from_env();
|
let mut listenfd = ListenFd::from_env();
|
||||||
let mut server = HttpServer::new(move || {
|
let mut server = HttpServer::new(move || {
|
||||||
let templates = Tera::new(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*")).unwrap();
|
|
||||||
App::new()
|
App::new()
|
||||||
.data(AppState {
|
.data(Arc::new(state.clone()))
|
||||||
db_url: db_url.to_owned(),
|
|
||||||
templates: templates,
|
|
||||||
})
|
|
||||||
.wrap(middleware::Logger::default()) // enable logger
|
.wrap(middleware::Logger::default()) // enable logger
|
||||||
.wrap(actix_flash::Flash::default())
|
.wrap(actix_flash::Flash::default())
|
||||||
.configure(init)
|
.configure(init)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user