diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index e504d5e67..7d544aaf7 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -293,7 +293,7 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest] - path: [basic, actix_example, actix4_example, axum_example, axum-graphql_example, rocket_example, poem_example, jsonrpsee_example] + path: [basic, actix_example, actix3_example, axum_example, axum-graphql_example, rocket_example, poem_example, jsonrpsee_example] steps: - uses: actions/checkout@v2 diff --git a/examples/actix3_example/.env b/examples/actix3_example/.env new file mode 100644 index 000000000..1a12a183a --- /dev/null +++ b/examples/actix3_example/.env @@ -0,0 +1,3 @@ +HOST=127.0.0.1 +PORT=8000 +DATABASE_URL="mysql://root:root@localhost/actix_example" \ No newline at end of file diff --git a/examples/actix4_example/Cargo.toml b/examples/actix3_example/Cargo.toml similarity index 67% rename from examples/actix4_example/Cargo.toml rename to examples/actix3_example/Cargo.toml index bbb75c967..11d2915a9 100644 --- a/examples/actix4_example/Cargo.toml +++ b/examples/actix3_example/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "sea-orm-actix-4-beta-example" +name = "sea-orm-actix-example" version = "0.1.0" authors = ["Sam Samai "] edition = "2021" @@ -9,12 +9,12 @@ publish = false members = [".", "entity", "migration"] [dependencies] -actix-files = "0.6.0-beta.4" -actix-http = "=3.0.0-beta.5" -actix-rt = "2.2.0" -actix-service = "=2.0.0-beta.5" -actix-web = "=4.0.0-beta.5" - +actix-http = "2" +actix-web = "3" +actix-flash = "0.2" +actix-files = "0.5" +futures = { version = "^0.3" } +futures-util = { version = "^0.3" } tera = "1.8.0" dotenv = "0.15" listenfd = "0.3.3" diff --git a/examples/actix4_example/README.md b/examples/actix3_example/README.md similarity index 78% rename from examples/actix4_example/README.md rename to examples/actix3_example/README.md index f754d132a..ec9e1fee6 100644 --- a/examples/actix4_example/README.md +++ b/examples/actix3_example/README.md @@ -1,6 +1,8 @@ ![screenshot](Screenshot.png) -# Actix 4 Beta with SeaORM example app +# Actix 3 with SeaORM example app + +> Actix Web 3 has been superseeded by [Actix Web 4](https://github.com/actix/actix-web). 1. Modify the `DATABASE_URL` var in `.env` to point to your chosen database diff --git a/examples/actix4_example/Screenshot.png b/examples/actix3_example/Screenshot.png similarity index 100% rename from examples/actix4_example/Screenshot.png rename to examples/actix3_example/Screenshot.png diff --git a/examples/actix4_example/entity/Cargo.toml b/examples/actix3_example/entity/Cargo.toml similarity index 92% rename from examples/actix4_example/entity/Cargo.toml rename to examples/actix3_example/entity/Cargo.toml index 0edd2d98d..314e31331 100644 --- a/examples/actix4_example/entity/Cargo.toml +++ b/examples/actix3_example/entity/Cargo.toml @@ -17,7 +17,7 @@ version = "^0.7.0" features = [ "macros", "debug-print", - "runtime-actix-native-tls", + "runtime-async-std-native-tls", "sqlx-mysql", # "sqlx-postgres", # "sqlx-sqlite", diff --git a/examples/actix4_example/entity/src/lib.rs b/examples/actix3_example/entity/src/lib.rs similarity index 100% rename from examples/actix4_example/entity/src/lib.rs rename to examples/actix3_example/entity/src/lib.rs diff --git a/examples/actix4_example/entity/src/post.rs b/examples/actix3_example/entity/src/post.rs similarity index 100% rename from examples/actix4_example/entity/src/post.rs rename to examples/actix3_example/entity/src/post.rs diff --git a/examples/actix4_example/migration/Cargo.toml b/examples/actix3_example/migration/Cargo.toml similarity index 100% rename from examples/actix4_example/migration/Cargo.toml rename to examples/actix3_example/migration/Cargo.toml diff --git a/examples/actix4_example/migration/README.md b/examples/actix3_example/migration/README.md similarity index 100% rename from examples/actix4_example/migration/README.md rename to examples/actix3_example/migration/README.md diff --git a/examples/actix4_example/migration/src/lib.rs b/examples/actix3_example/migration/src/lib.rs similarity index 100% rename from examples/actix4_example/migration/src/lib.rs rename to examples/actix3_example/migration/src/lib.rs diff --git a/examples/actix4_example/migration/src/m20220120_000001_create_post_table.rs b/examples/actix3_example/migration/src/m20220120_000001_create_post_table.rs similarity index 100% rename from examples/actix4_example/migration/src/m20220120_000001_create_post_table.rs rename to examples/actix3_example/migration/src/m20220120_000001_create_post_table.rs diff --git a/examples/actix4_example/migration/src/main.rs b/examples/actix3_example/migration/src/main.rs similarity index 100% rename from examples/actix4_example/migration/src/main.rs rename to examples/actix3_example/migration/src/main.rs diff --git a/examples/actix4_example/src/main.rs b/examples/actix3_example/src/main.rs similarity index 81% rename from examples/actix4_example/src/main.rs rename to examples/actix3_example/src/main.rs index 201d22d97..b3f44a782 100644 --- a/examples/actix4_example/src/main.rs +++ b/examples/actix3_example/src/main.rs @@ -1,4 +1,4 @@ -use actix_files::Files as Fs; +use actix_files as fs; use actix_web::{ error, get, middleware, post, web, App, Error, HttpRequest, HttpResponse, HttpServer, Result, }; @@ -34,7 +34,11 @@ struct FlashData { } #[get("/")] -async fn list(req: HttpRequest, data: web::Data) -> Result { +async fn list( + req: HttpRequest, + data: web::Data, + opt_flash: Option>, +) -> Result { let template = &data.templates; let conn = &data.conn; @@ -58,6 +62,11 @@ async fn list(req: HttpRequest, data: web::Data) -> Result) -> Result { async fn create( data: web::Data, post_form: web::Form, -) -> Result { +) -> actix_flash::Response { let conn = &data.conn; let form = post_form.into_inner(); @@ -92,9 +101,12 @@ async fn create( .await .expect("could not insert post"); - Ok(HttpResponse::Found() - .append_header(("location", "/")) - .finish()) + let flash = FlashData { + kind: "success".to_owned(), + message: "Post successfully added.".to_owned(), + }; + + actix_flash::Response::with_redirect(flash, "/") } #[get("/{id}")] @@ -122,7 +134,7 @@ async fn update( data: web::Data, id: web::Path, post_form: web::Form, -) -> Result { +) -> actix_flash::Response { let conn = &data.conn; let form = post_form.into_inner(); @@ -135,13 +147,19 @@ async fn update( .await .expect("could not edit post"); - Ok(HttpResponse::Found() - .append_header(("location", "/")) - .finish()) + let flash = FlashData { + kind: "success".to_owned(), + message: "Post successfully updated.".to_owned(), + }; + + actix_flash::Response::with_redirect(flash, "/") } #[post("/delete/{id}")] -async fn delete(data: web::Data, id: web::Path) -> Result { +async fn delete( + data: web::Data, + id: web::Path, +) -> actix_flash::Response { let conn = &data.conn; let post: post::ActiveModel = Post::find_by_id(id.into_inner()) @@ -153,9 +171,12 @@ async fn delete(data: web::Data, id: web::Path) -> Result std::io::Result<()> { let mut listenfd = ListenFd::from_env(); let mut server = HttpServer::new(move || { App::new() - .service(Fs::new("/static", "./static")) .data(state.clone()) .wrap(middleware::Logger::default()) // enable logger + .wrap(actix_flash::Flash::default()) .configure(init) + .service(fs::Files::new("/static", "./static").show_files_listing()) }); server = match listenfd.take_tcp_listener(0)? { diff --git a/examples/actix4_example/static/css/normalize.css b/examples/actix3_example/static/css/normalize.css similarity index 100% rename from examples/actix4_example/static/css/normalize.css rename to examples/actix3_example/static/css/normalize.css diff --git a/examples/actix4_example/static/css/skeleton.css b/examples/actix3_example/static/css/skeleton.css similarity index 100% rename from examples/actix4_example/static/css/skeleton.css rename to examples/actix3_example/static/css/skeleton.css diff --git a/examples/actix4_example/static/css/style.css b/examples/actix3_example/static/css/style.css similarity index 100% rename from examples/actix4_example/static/css/style.css rename to examples/actix3_example/static/css/style.css diff --git a/examples/actix4_example/static/images/favicon.png b/examples/actix3_example/static/images/favicon.png similarity index 100% rename from examples/actix4_example/static/images/favicon.png rename to examples/actix3_example/static/images/favicon.png diff --git a/examples/actix4_example/templates/edit.html.tera b/examples/actix3_example/templates/edit.html.tera similarity index 100% rename from examples/actix4_example/templates/edit.html.tera rename to examples/actix3_example/templates/edit.html.tera diff --git a/examples/actix4_example/templates/error/404.html.tera b/examples/actix3_example/templates/error/404.html.tera similarity index 100% rename from examples/actix4_example/templates/error/404.html.tera rename to examples/actix3_example/templates/error/404.html.tera diff --git a/examples/actix4_example/templates/index.html.tera b/examples/actix3_example/templates/index.html.tera similarity index 100% rename from examples/actix4_example/templates/index.html.tera rename to examples/actix3_example/templates/index.html.tera diff --git a/examples/actix4_example/templates/layout.html.tera b/examples/actix3_example/templates/layout.html.tera similarity index 100% rename from examples/actix4_example/templates/layout.html.tera rename to examples/actix3_example/templates/layout.html.tera diff --git a/examples/actix4_example/templates/new.html.tera b/examples/actix3_example/templates/new.html.tera similarity index 100% rename from examples/actix4_example/templates/new.html.tera rename to examples/actix3_example/templates/new.html.tera diff --git a/examples/actix4_example/.env b/examples/actix4_example/.env deleted file mode 100644 index 010295bc7..000000000 --- a/examples/actix4_example/.env +++ /dev/null @@ -1,3 +0,0 @@ -HOST=127.0.0.1 -PORT=8000 -DATABASE_URL="mysql://root:root@localhost/actix_example" diff --git a/examples/actix_example/.env b/examples/actix_example/.env index 1a12a183a..010295bc7 100644 --- a/examples/actix_example/.env +++ b/examples/actix_example/.env @@ -1,3 +1,3 @@ HOST=127.0.0.1 PORT=8000 -DATABASE_URL="mysql://root:root@localhost/actix_example" \ No newline at end of file +DATABASE_URL="mysql://root:root@localhost/actix_example" diff --git a/examples/actix_example/Cargo.toml b/examples/actix_example/Cargo.toml index 11d2915a9..578d72af8 100644 --- a/examples/actix_example/Cargo.toml +++ b/examples/actix_example/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "sea-orm-actix-example" +name = "sea-orm-actix-4-beta-example" version = "0.1.0" authors = ["Sam Samai "] edition = "2021" @@ -9,15 +9,15 @@ publish = false members = [".", "entity", "migration"] [dependencies] -actix-http = "2" -actix-web = "3" -actix-flash = "0.2" -actix-files = "0.5" -futures = { version = "^0.3" } -futures-util = { version = "^0.3" } -tera = "1.8.0" +actix-files = "0.6" +actix-http = "3" +actix-rt = "2.7" +actix-service = "2" +actix-web = "4" + +tera = "1.15.0" dotenv = "0.15" -listenfd = "0.3.3" +listenfd = "0.5" serde = "1" tracing-subscriber = { version = "0.3", features = ["env-filter"] } entity = { path = "entity" } diff --git a/examples/actix_example/README.md b/examples/actix_example/README.md index 71b4722a8..c37fdfe06 100644 --- a/examples/actix_example/README.md +++ b/examples/actix_example/README.md @@ -1,6 +1,6 @@ ![screenshot](Screenshot.png) -# Actix with SeaORM example app +# Actix 4 with SeaORM example app 1. Modify the `DATABASE_URL` var in `.env` to point to your chosen database diff --git a/examples/actix_example/entity/Cargo.toml b/examples/actix_example/entity/Cargo.toml index 314e31331..0edd2d98d 100644 --- a/examples/actix_example/entity/Cargo.toml +++ b/examples/actix_example/entity/Cargo.toml @@ -17,7 +17,7 @@ version = "^0.7.0" features = [ "macros", "debug-print", - "runtime-async-std-native-tls", + "runtime-actix-native-tls", "sqlx-mysql", # "sqlx-postgres", # "sqlx-sqlite", diff --git a/examples/actix_example/src/main.rs b/examples/actix_example/src/main.rs index b3f44a782..5855bbed5 100644 --- a/examples/actix_example/src/main.rs +++ b/examples/actix_example/src/main.rs @@ -1,6 +1,6 @@ -use actix_files as fs; +use actix_files::Files as Fs; use actix_web::{ - error, get, middleware, post, web, App, Error, HttpRequest, HttpResponse, HttpServer, Result, + error, get, middleware, post, web, App, Error, HttpRequest, HttpResponse, HttpServer, Result }; use entity::post; @@ -21,6 +21,7 @@ struct AppState { templates: tera::Tera, conn: DatabaseConnection, } + #[derive(Debug, Deserialize)] pub struct Params { page: Option, @@ -34,11 +35,7 @@ struct FlashData { } #[get("/")] -async fn list( - req: HttpRequest, - data: web::Data, - opt_flash: Option>, -) -> Result { +async fn list(req: HttpRequest, data: web::Data) -> Result { let template = &data.templates; let conn = &data.conn; @@ -62,11 +59,6 @@ async fn list( ctx.insert("posts_per_page", &posts_per_page); ctx.insert("num_pages", &num_pages); - if let Some(flash) = opt_flash { - let flash_inner = flash.into_inner(); - ctx.insert("flash", &flash_inner); - } - let body = template .render("index.html.tera", &ctx) .map_err(|_| error::ErrorInternalServerError("Template error"))?; @@ -87,7 +79,7 @@ async fn new(data: web::Data) -> Result { async fn create( data: web::Data, post_form: web::Form, -) -> actix_flash::Response { +) -> Result { let conn = &data.conn; let form = post_form.into_inner(); @@ -101,12 +93,9 @@ async fn create( .await .expect("could not insert post"); - let flash = FlashData { - kind: "success".to_owned(), - message: "Post successfully added.".to_owned(), - }; - - actix_flash::Response::with_redirect(flash, "/") + Ok(HttpResponse::Found() + .append_header(("location", "/")) + .finish()) } #[get("/{id}")] @@ -134,7 +123,7 @@ async fn update( data: web::Data, id: web::Path, post_form: web::Form, -) -> actix_flash::Response { +) -> Result { let conn = &data.conn; let form = post_form.into_inner(); @@ -147,19 +136,13 @@ async fn update( .await .expect("could not edit post"); - let flash = FlashData { - kind: "success".to_owned(), - message: "Post successfully updated.".to_owned(), - }; - - actix_flash::Response::with_redirect(flash, "/") + Ok(HttpResponse::Found() + .append_header(("location", "/")) + .finish()) } #[post("/delete/{id}")] -async fn delete( - data: web::Data, - id: web::Path, -) -> actix_flash::Response { +async fn delete(data: web::Data, id: web::Path) -> Result { let conn = &data.conn; let post: post::ActiveModel = Post::find_by_id(id.into_inner()) @@ -171,12 +154,9 @@ async fn delete( post.delete(conn).await.unwrap(); - let flash = FlashData { - kind: "success".to_owned(), - message: "Post successfully deleted.".to_owned(), - }; - - actix_flash::Response::with_redirect(flash, "/") + Ok(HttpResponse::Found() + .append_header(("location", "/")) + .finish()) } #[actix_web::main] @@ -191,20 +171,23 @@ async fn main() -> std::io::Result<()> { let port = env::var("PORT").expect("PORT is not set in .env file"); let server_url = format!("{}:{}", host, port); - // create post table if not exists + // establish connection to database and apply migrations + // -> create post table if not exists let conn = sea_orm::Database::connect(&db_url).await.unwrap(); Migrator::up(&conn, None).await.unwrap(); + + // load tera templates and build app state let templates = Tera::new(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*")).unwrap(); let state = AppState { templates, conn }; + // create server and try to serve over socket if possible let mut listenfd = ListenFd::from_env(); let mut server = HttpServer::new(move || { App::new() - .data(state.clone()) + .service(Fs::new("/static", "./static")) + .app_data(web::Data::new(state.clone())) .wrap(middleware::Logger::default()) // enable logger - .wrap(actix_flash::Flash::default()) .configure(init) - .service(fs::Files::new("/static", "./static").show_files_listing()) }); server = match listenfd.take_tcp_listener(0)? {