feat: add server cli configuration

This commit is contained in:
Jindřich Moravec 2024-01-22 11:18:43 +01:00
parent 143dc0e5ce
commit f9fb8f0670
4 changed files with 234 additions and 15 deletions

28
server/src/config.rs Normal file
View file

@ -0,0 +1,28 @@
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::path::PathBuf;
use clap::Parser;
const LOCAL_IPV4: IpAddr = IpAddr::V4(Ipv4Addr::LOCALHOST);
#[derive(Debug, Parser)]
#[command(author, version, about)]
pub struct Configuration {
#[arg(short, long, default_value_t = LOCAL_IPV4, help = "IP address for the server to listen on")]
address: IpAddr,
#[arg(short, long, default_value = "5432", help = "Port for the server to listen on")]
port: u16,
#[arg(short, long, help = "Path to the data file")]
file: PathBuf,
}
impl Configuration {
#[inline]
pub fn get_socket_address(&self) -> SocketAddr {
SocketAddr::new(self.address, self.port)
}
#[inline]
pub fn get_file_path(&self) -> &PathBuf {
&self.file
}
}

View file

@ -1,3 +1,7 @@
mod config;
use std::net::SocketAddr;
use clap::Parser;
use proto::handshake::response::HandshakeResponse;
use proto::handshake::server::do_server_handshake;
use proto::message::backend::{
@ -11,10 +15,13 @@ use proto::writer::backend::BackendProtoWriter;
use proto::writer::protowriter::{ProtoFlush, ProtoWriter};
use tokio::io::{BufReader, BufWriter};
use tokio::net::{TcpListener, TcpStream};
use crate::config::Configuration;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let addr = "0.0.0.0:5432";
let config = Configuration::parse();
let addr = config.get_socket_address();
let listener = TcpListener::bind(&addr).await?;
println!("Server started at {addr}");