This commit is contained in:
Tomasz Sidoruk 2022-04-22 01:08:54 +02:00
commit 015af82f22
Signed by: s452652
GPG Key ID: 3E83A65E74849A08
4 changed files with 2902 additions and 0 deletions

1804
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

17
Cargo.toml Normal file
View File

@ -0,0 +1,17 @@
[package]
name = "hello_world"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
hyper = { version = "*", features = ["full"] }
tokio = { version = "*", features = ["full"] }
mysql = "*"
serde_json = "*"
serde = "*"
serde_derive = "*"
bcrypt = "0.12"
rand = "0.3.0"

1026
src/main.rs Normal file

File diff suppressed because it is too large Load Diff

55
src/main1.rs Normal file
View File

@ -0,0 +1,55 @@
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);
}
}