Define SharedState type
This commit is contained in:
parent
54edd87706
commit
daa5ff77c0
@ -2,17 +2,12 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use actix_files as fs;
|
use actix_files as fs;
|
||||||
use actix_http::{body::Body, Response};
|
|
||||||
use actix_web::dev::ServiceResponse;
|
|
||||||
use actix_web::http::StatusCode;
|
|
||||||
use actix_web::middleware::errhandlers::{ErrorHandlerResponse, ErrorHandlers};
|
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
delete, error, get, guard, http, middleware, post, web, App, Error, HttpRequest, HttpResponse,
|
error, get, middleware, post, web, App, Error, HttpRequest, HttpResponse,
|
||||||
HttpServer, Result,
|
HttpServer, Result,
|
||||||
};
|
};
|
||||||
use listenfd::ListenFd;
|
use listenfd::ListenFd;
|
||||||
use sea_orm::entity::*;
|
use sea_orm::entity::*;
|
||||||
use sea_orm::query::*;
|
|
||||||
use sea_orm::{DatabaseConnection, EntityTrait};
|
use sea_orm::{DatabaseConnection, EntityTrait};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::env;
|
use std::env;
|
||||||
@ -29,6 +24,7 @@ struct AppState {
|
|||||||
templates: tera::Tera,
|
templates: tera::Tera,
|
||||||
conn: Arc<DatabaseConnection>,
|
conn: Arc<DatabaseConnection>,
|
||||||
}
|
}
|
||||||
|
type SharedState = Arc<AppState>;
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct Params {
|
pub struct Params {
|
||||||
@ -45,7 +41,7 @@ struct FlashData {
|
|||||||
#[get("/")]
|
#[get("/")]
|
||||||
async fn list(
|
async fn list(
|
||||||
req: HttpRequest,
|
req: HttpRequest,
|
||||||
data: web::Data<Arc<AppState>>,
|
data: web::Data<SharedState>,
|
||||||
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;
|
||||||
@ -81,7 +77,7 @@ async fn list(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[get("/new")]
|
#[get("/new")]
|
||||||
async fn new(data: web::Data<Arc<AppState>>) -> Result<HttpResponse, Error> {
|
async fn new(data: web::Data<SharedState>) -> 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
|
||||||
@ -92,7 +88,7 @@ async fn new(data: web::Data<Arc<AppState>>) -> Result<HttpResponse, Error> {
|
|||||||
|
|
||||||
#[post("/")]
|
#[post("/")]
|
||||||
async fn create(
|
async fn create(
|
||||||
data: web::Data<Arc<AppState>>,
|
data: web::Data<SharedState>,
|
||||||
post_form: web::Form<post::Model>,
|
post_form: web::Form<post::Model>,
|
||||||
) -> actix_flash::Response<HttpResponse, FlashData> {
|
) -> actix_flash::Response<HttpResponse, FlashData> {
|
||||||
let conn = &data.conn;
|
let conn = &data.conn;
|
||||||
@ -117,7 +113,7 @@ async fn create(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[get("/{id}")]
|
#[get("/{id}")]
|
||||||
async fn edit(data: web::Data<Arc<AppState>>, id: web::Path<i32>) -> Result<HttpResponse, Error> {
|
async fn edit(data: web::Data<SharedState>, id: web::Path<i32>) -> Result<HttpResponse, Error> {
|
||||||
let conn = &data.conn;
|
let conn = &data.conn;
|
||||||
let template = &data.templates;
|
let template = &data.templates;
|
||||||
|
|
||||||
@ -138,7 +134,7 @@ async fn edit(data: web::Data<Arc<AppState>>, id: web::Path<i32>) -> Result<Http
|
|||||||
|
|
||||||
#[post("/{id}")]
|
#[post("/{id}")]
|
||||||
async fn update(
|
async fn update(
|
||||||
data: web::Data<Arc<AppState>>,
|
data: web::Data<SharedState>,
|
||||||
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> {
|
||||||
@ -164,7 +160,7 @@ async fn update(
|
|||||||
|
|
||||||
#[post("/delete/{id}")]
|
#[post("/delete/{id}")]
|
||||||
async fn delete(
|
async fn delete(
|
||||||
data: web::Data<Arc<AppState>>,
|
data: web::Data<SharedState>,
|
||||||
id: web::Path<i32>,
|
id: web::Path<i32>,
|
||||||
) -> actix_flash::Response<HttpResponse, FlashData> {
|
) -> actix_flash::Response<HttpResponse, FlashData> {
|
||||||
let conn = &data.conn;
|
let conn = &data.conn;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user