From abad2ebad9c78e3978343aa0fc572a87dbf9fa78 Mon Sep 17 00:00:00 2001 From: niliara-edu Date: Thu, 26 Sep 2024 13:56:04 +0200 Subject: update rust code --- rust/src/main.rs | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) (limited to 'rust/src') diff --git a/rust/src/main.rs b/rust/src/main.rs index 486aa6f..2ccdcee 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -1,5 +1,35 @@ mod structs; -fn main() { - println!("Hello, world!"); +use actix_web::{web, App, HttpServer}; +use sqlx::mysql::{MySqlPool, MySqlPoolOptions}; + +#[derive(Clone)] +struct AppState { + pool: MySqlPool, +} + +#[actix_web::main] +async fn main() -> std::io::Result<()> { + const DB_URL: &str = "mysql://root:@127.0.0.1:3306/balalaika"; + + let pool: MySqlPool = MySqlPoolOptions::new() + .max_connections(10) + .connect(DB_URL) + .await + .unwrap(); + + let app_state = AppState { pool }; + + HttpServer::new(move || { + App::new() + .app_data(web::Data::new(app_state.clone())) + .route("/", web::get().to(root)) + }) + .bind(("127.0.0.1", 8000))? + .run() + .await +} + +async fn root() -> String { + String::from("Server is up and running") } -- cgit v1.2.3