Hello World
This commit is contained in:
11
Cargo.toml
11
Cargo.toml
@@ -1,8 +1,17 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "RestApi"
|
name = "ChaotenRestApi"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[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"
|
||||||
52
src/main.rs
52
src/main.rs
@@ -1,3 +1,51 @@
|
|||||||
fn main() {
|
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};
|
||||||
println!("Hello, world!");
|
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