56 lines
1.5 KiB
Rust
56 lines
1.5 KiB
Rust
|
use serde_derive::{Serialize, Deserialize};
|
||
|
use mysql::*;
|
||
|
use mysql::prelude::*;
|
||
|
use std::convert::Infallible;
|
||
|
use std::net::SocketAddr;
|
||
|
use hyper::{Body, Request, Response, Server};
|
||
|
use hyper::service::{make_service_fn, service_fn};
|
||
|
#[derive(Serialize, Deserialize)]
|
||
|
struct dane {
|
||
|
id:i32,
|
||
|
data:String
|
||
|
}
|
||
|
|
||
|
fn getJson() -> Vec<dane> {
|
||
|
let url = "mysql://inz:HaLzqw68CbabS8Smz3Vx!@localhost:3306/inz";
|
||
|
|
||
|
let opts = Opts::from_url(url).unwrap();
|
||
|
let pool = Pool::new(opts).unwrap();
|
||
|
let mut conn = pool.get_conn().unwrap();
|
||
|
let res = conn
|
||
|
.query_map(
|
||
|
"SELECT id, data from dane",
|
||
|
|(id,data)| {
|
||
|
dane { id,data }
|
||
|
},
|
||
|
);
|
||
|
return res.unwrap();
|
||
|
}
|
||
|
async fn hello_world(_req: Request<Body>) -> Result<Response<Body>> {
|
||
|
let path :String = _req.uri().path().to_string();
|
||
|
if path != "/mysql"{
|
||
|
return Ok(Response::new(path.into()));
|
||
|
}else{
|
||
|
return Ok(Response::new(serde_json::to_string(&getJson()).unwrap().into()))
|
||
|
}
|
||
|
}
|
||
|
#[tokio::main]
|
||
|
async fn main() {
|
||
|
let addr = SocketAddr::from(([0, 0, 0, 0], 1000));
|
||
|
|
||
|
// A `Service` is needed for every connection, so this
|
||
|
// creates one from our `hello_world` function.
|
||
|
let make_svc = make_service_fn(|_conn| async {
|
||
|
// service_fn converts our function into a `Service`
|
||
|
Ok::<_, Infallible>(service_fn(hello_world))
|
||
|
});
|
||
|
|
||
|
let server = Server::bind(&addr).serve(make_svc);
|
||
|
|
||
|
// Run this server for... forever!
|
||
|
if let Err(e) = server.await {
|
||
|
eprintln!("server error: {}", e);
|
||
|
}
|
||
|
|
||
|
}
|