From 53fa07ba042012ee40bbe88cd484f1ec54f8b57c Mon Sep 17 00:00:00 2001 From: Julian Date: Sat, 11 Dec 2021 00:20:35 +0100 Subject: [PATCH] Hello World --- Cargo.toml | 11 ++++++++++- src/main.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 79f1e41..ecf4520 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,17 @@ [package] -name = "RestApi" +name = "ChaotenRestApi" version = "0.1.0" edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +actix-web = "4.0.0-beta.13" +serde = "1.0.106" +serde_json = "1.0.51" +sqlx = { version = "0.5.9", features = ["runtime-tokio-native-tls","mssql"] } +dotenv = "0.15.0" +env_logger = "0.7.1" +log = "0.4.8" +anyhow = "1.0.28" +futures = "0.3.13" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index e7a11a9..aeb2c67 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,51 @@ -fn main() { - println!("Hello, world!"); +use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder}; +use sqlx::MssqlPool; +use sqlx::mssql::MssqlRow; +use sqlx::Row; +use sqlx::Column; +use sqlx::TypeInfo; +use std::sync::Mutex; +use sqlx::Pool; +use sqlx::Mssql; + +#[get("/")] +async fn hello() -> impl Responder { + HttpResponse::Ok().body("Hello world!") +} + +#[get("/renner")] +async fn renner(data: web::Data>>) -> impl Responder { + let mut data = data.lock().unwrap(); + let rows = sqlx::query("SELECT * FROM renner") + .fetch_optional(&(*data)) + .await + .expect("no connection to database"); + let res : String = rows.unwrap().try_get("Name").unwrap(); + HttpResponse::Ok().body(res) +} + +#[post("/echo")] +async fn echo(req_body: String) -> impl Responder { + HttpResponse::Ok().body(req_body) +} + +async fn manual_hello() -> impl Responder { + HttpResponse::Ok().body("Hey there!") +} + +#[actix_web::main] +async fn main() -> std::io::Result<()> { + let db_pool = MssqlPool::connect("mssql://SA:RenneR2021@192.168.178.145:12433/Chaoten").await.unwrap(); + let data = web::Data::new(Mutex::new(db_pool)); + HttpServer::new(move || { + App::new() + .app_data(data.clone()) + .service(hello) + .service(echo) + .service(renner) + .route("/hey", web::get().to(manual_hello)) + }) + .bind("127.0.0.1:8080")? + .run() + .await }