Skip to content

Commit

Permalink
Merge pull request #20 from vances/main
Browse files Browse the repository at this point in the history
add redis connection in tmf634_server
  • Loading branch information
vances authored Nov 23, 2023
2 parents f38c352 + ae47876 commit 3e98137
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
1 change: 1 addition & 0 deletions tmf634/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,5 @@ clap = "2.25"
env_logger = "0.7"
native-tls = "0.2"
tokio-openssl = "0.6"
redis = { version = "0.23.3", features = ["tokio-comp"] }

23 changes: 20 additions & 3 deletions tmf634/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use clap::{App, Arg};

mod server;


/// Create custom server, wire it to the autogenerated router,
/// and pass it to the web server.
#[tokio::main]
Expand All @@ -17,9 +16,27 @@ async fn main() {
.arg(Arg::with_name("https")
.long("https")
.help("Whether to use HTTPS or not"))
.arg(Arg::with_name("redis")
.long("redis")
.value_name("URI")
.takes_value(true)
.help("Redis server URI"))
.arg(Arg::with_name("port")
.long("port")
.value_name("port")
.takes_value(true)
.help("Port number to listen on"))
.arg(Arg::with_name("interface")
.long("interface")
.value_name("address")
.takes_value(true)
.help("Address of local interface to bind"))
.get_matches();

let addr = "127.0.0.1:8080";
let bind = matches.value_of("address").unwrap_or("0.0.0.0");
let port = matches.value_of("port").unwrap_or("8080");
let redis = matches.value_of("redis").unwrap_or("redis://127.0.0.1/");
let https = matches.is_present("https");

server::create(addr, matches.is_present("https")).await;
server::create(redis, bind, port, https).await;
}
23 changes: 20 additions & 3 deletions tmf634/server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,18 @@ use openssl::ssl::{Ssl, SslAcceptor, SslAcceptorBuilder, SslFiletype, SslMethod}
use oda_sdk_tmf634::models;

/// Builds an SSL implementation for Simple HTTPS from some hard-coded file names
pub async fn create(addr: &str, https: bool) {
let addr = addr.parse().expect("Failed to parse bind address");
pub async fn create(redis_uri: &str, bind: &str, port: &str, https: bool) {

let redis_client = match redis::Client::open(redis_uri) {
Ok(client) => client,
Err(error) => panic!("error opening redis: {:?}", error),
};
let mut redis_connection = match redis_client.get_connection() {
Ok(connection) => connection,
Err(error) => panic!("error connecting redis: {:?}", error),
};

let addr = format!("{}:{}", bind, port).parse().expect("Failed to parse bind address");

let server = Server::new();

Expand Down Expand Up @@ -76,7 +86,14 @@ pub async fn create(addr: &str, https: bool) {
}
} else {
// Using HTTP
hyper::server::Server::bind(&addr).serve(service).await.unwrap()
let server = match hyper::server::Server::try_bind(&addr) {
Ok(builder) => builder.serve(service),
Err(error) => panic!("error binding server: {:?}", error),
};
match server.await {
Ok(_) => (),
Err(error) => panic!("server error: {:?}", error),
};
}
}

Expand Down

0 comments on commit 3e98137

Please sign in to comment.