65 lines
1.7 KiB
Rust
65 lines
1.7 KiB
Rust
#[macro_use]
|
|
extern crate clap;
|
|
extern crate tokio;
|
|
|
|
mod configuration;
|
|
|
|
use crate::configuration::Settings;
|
|
use tokio::io;
|
|
use tokio::net::TcpListener;
|
|
use tokio::prelude::*;
|
|
|
|
/**
|
|
* Initiates an instance of the gopherbridge server and
|
|
* starts listening for connections on the given port.
|
|
*/
|
|
fn main() {
|
|
let settings = Settings::create();
|
|
|
|
println!("Program listens on {}", settings.listen);
|
|
println!("Loading plugins {:?}", settings.plugins);
|
|
println!("Listening on port {}", settings.port);
|
|
|
|
let addr = format!("{}:{}", settings.listen, settings.port)
|
|
.parse()
|
|
.unwrap();
|
|
|
|
let listener = TcpListener::bind(&addr).unwrap();
|
|
let server = listener
|
|
.incoming()
|
|
.for_each(|socket| {
|
|
let (reader, writer) = socket.split();
|
|
let amount = io::copy(reader, writer);
|
|
|
|
let msg = amount.then(|result| {
|
|
match result {
|
|
Ok((amount, _, _)) => println!("wrote {} bytes", amount),
|
|
Err(e) => println!("error: {}", e),
|
|
}
|
|
Ok(())
|
|
});
|
|
|
|
tokio::spawn(msg);
|
|
Ok(())
|
|
})
|
|
.map_err(|err| {
|
|
// Handle error by stdout
|
|
println!("accept error = {:?}", err);
|
|
});
|
|
|
|
println!(
|
|
"Started the server on {}:{}",
|
|
settings.listen, settings.port
|
|
);
|
|
|
|
// Start the server
|
|
//
|
|
// This does a few things:
|
|
//
|
|
// * Start the Tokio runtime
|
|
// * Spawns the `server` task onto the runtime.
|
|
// * Blocks the current thread until the runtime becomes idle, i.e. all
|
|
// spawned tasks have completed.
|
|
tokio::run(server);
|
|
}
|