Hello World
This commit is contained in:
52
src/main.rs
52
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<Mutex<Pool<Mssql>>>) -> 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user