-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add actix postgres example (#8)
- Loading branch information
Showing
6 changed files
with
96 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Actix Web with shuttle | ||
|
||
Normally one would configure an application with [Actix Web](https://docs.rs/actix-web/latest/actix_web/index.html) using the [App](https://docs.rs/actix-web/latest/actix_web/struct.App.html) struct. However, shuttle needs to move the users configuration across threads to start the server on our backend, and the `App` struct is `!Send` and `!Sync`. | ||
|
||
That means that for shuttle to support Actix Web, we need to use the [ServiceConfig](https://docs.rs/actix-web/latest/actix_web/web/struct.ServiceConfig.html) struct. You should be able to configure your application like you normally would, but some steps may be a bit different. If you do you find something that you would expect to be possible not working, please reach out and let us know. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
use actix_web::web::{resource, ServiceConfig}; | ||
use actix_web::{get, web::ServiceConfig}; | ||
use shuttle_service::ShuttleActixWeb; | ||
|
||
#[get("/hello")] | ||
async fn hello_world() -> &'static str { | ||
"Hello World!" | ||
} | ||
|
||
#[shuttle_service::main] | ||
async fn actix_web( | ||
) -> ShuttleActixWeb<impl FnOnce(&mut ServiceConfig) + Sync + Send + Copy + Clone + 'static> { | ||
) -> ShuttleActixWeb<impl FnOnce(&mut ServiceConfig) + Sync + Send + Clone + 'static> { | ||
Ok(move |cfg: &mut ServiceConfig| { | ||
cfg.service(resource("/hello").to(hello_world)); | ||
cfg.service(hello_world); | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "postgres" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
|
||
[dependencies] | ||
actix-web = "4.2.1" | ||
serde = "1.0.148" | ||
shuttle-service = { version = "0.8.0", features = ["web-actix-web"] } | ||
shuttle-shared-db = { version = "0.8.0", features = ["postgres"] } | ||
sqlx = { version = "0.6.2", features = ["runtime-tokio-native-tls", "postgres"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
DROP TABLE IF EXISTS todos; | ||
|
||
CREATE TABLE todos ( | ||
id serial PRIMARY KEY, | ||
note TEXT NOT NULL | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use actix_web::middleware::Logger; | ||
use actix_web::{ | ||
error, get, post, | ||
web::{self, Json, ServiceConfig}, | ||
Result, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
use shuttle_service::{error::CustomError, ShuttleActixWeb}; | ||
use sqlx::{Executor, FromRow, PgPool}; | ||
|
||
#[get("/{id}")] | ||
async fn retrieve(path: web::Path<i32>, state: web::Data<AppState>) -> Result<Json<Todo>> { | ||
let todo = sqlx::query_as("SELECT * FROM todos WHERE id = $1") | ||
.bind(*path) | ||
.fetch_one(&state.pool) | ||
.await | ||
.map_err(|e| error::ErrorBadRequest(e.to_string()))?; | ||
|
||
Ok(Json(todo)) | ||
} | ||
|
||
#[post("")] | ||
async fn add(todo: web::Json<TodoNew>, state: web::Data<AppState>) -> Result<Json<Todo>> { | ||
let todo = sqlx::query_as("INSERT INTO todos(note) VALUES ($1) RETURNING id, note") | ||
.bind(&todo.note) | ||
.fetch_one(&state.pool) | ||
.await | ||
.map_err(|e| error::ErrorBadRequest(e.to_string()))?; | ||
|
||
Ok(Json(todo)) | ||
} | ||
|
||
#[derive(Clone)] | ||
struct AppState { | ||
pool: PgPool, | ||
} | ||
|
||
#[shuttle_service::main] | ||
async fn actix_web( | ||
#[shuttle_shared_db::Postgres] pool: PgPool, | ||
) -> ShuttleActixWeb<impl FnOnce(&mut ServiceConfig) + Sync + Send + Clone + 'static> { | ||
pool.execute(include_str!("../schema.sql")) | ||
.await | ||
.map_err(CustomError::new)?; | ||
|
||
let state = web::Data::new(AppState { pool }); | ||
|
||
Ok(move |cfg: &mut ServiceConfig| { | ||
cfg.service( | ||
web::scope("/todos") | ||
.wrap(Logger::default()) | ||
.service(retrieve) | ||
.service(add) | ||
.app_data(state), | ||
); | ||
}) | ||
} | ||
|
||
#[derive(Deserialize)] | ||
struct TodoNew { | ||
pub note: String, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, FromRow)] | ||
struct Todo { | ||
pub id: i32, | ||
pub note: String, | ||
} |