Skip to content

Commit

Permalink
Allow setting the server endpoint root
Browse files Browse the repository at this point in the history
  • Loading branch information
joajfreitas committed Jan 5, 2024
1 parent 9b2a5a4 commit 55a5239
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/bin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ use marcador::server::server;
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
#[arg(long)]
db: Option<String>,
#[arg(long)]
host: Option<String>,
#[arg(long)]
port: Option<u16>,
#[arg(long)]
root: Option<String>,
}

fn main() -> Result<(), String> {
Expand All @@ -24,6 +29,7 @@ fn main() -> Result<(), String> {
server_config.set_db(&cli.db);
server_config.set_host(&cli.host);
server_config.set_port(&cli.port);
server_config.set_root(&cli.root);

server(server_config)?;

Expand Down
32 changes: 32 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub struct ServerConfig {
pub db: Option<String>,
pub host: Option<String>,
pub port: Option<u16>,
pub root: Option<String>,
}

impl ServerConfig {
Expand All @@ -21,11 +22,42 @@ impl ServerConfig {
self.host = host.clone();
}
}

pub fn get_host(&self) -> String {
if let Some(host) = &self.host {
host.clone()
} else {
"127.0.0.1".to_string()
}
}

pub fn set_port(&mut self, port: &Option<u16>) {
if port.is_some() {
self.port = *port;
}
}

pub fn get_port(&self) -> u16 {
if let Some(port) = self.port {
port
} else {
8080
}
}

pub fn set_root(&mut self, root: &Option<String>) {
if root.is_some() {
self.root = root.clone();
}
}

pub fn get_root(&self) -> String {
if let Some(root) = &self.root {
root.clone()
} else {
"/".to_string()
}
}
}

#[derive(Deserialize)]
Expand Down
33 changes: 27 additions & 6 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,48 @@ async fn endpoint_delete(
}

pub fn server(config: ServerConfig) -> Result<(), String> {
println!(
"Running server {}:{}{}",
config.get_host(),
config.get_port(),
config.get_root()
);
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap()
.block_on(async_server(
config.db.ok_or("Expected db path")?,
config.host.ok_or("Expected hostname")?,
config.port.ok_or("Expected port")?,
config.host.unwrap_or("127.0.0.1".to_string()),
config.port.unwrap_or(8080),
config.root.unwrap_or("/".to_string()),
))
.map_err(|err| format!("{:?}", err))
}

async fn async_server(db: String, host: String, port: u16) -> std::io::Result<()> {
async fn async_server(db: String, host: String, port: u16, root: String) -> std::io::Result<()> {
let (list_endpoint, add_endpoint, delete_endpoint) = if root == "/" {
(
"/list".to_string(),
"/add".to_string(),
"/delete".to_string(),
)
} else {
(
root.clone() + "/list",
root.clone() + "/add",
root + "/delete",
)
};

HttpServer::new(move || {
App::new()
.app_data(web::Data::new(State {
local_proxy: LocalProxy::new(&db),
}))
.route("/marcador/list", web::get().to(endpoint_list))
.route("/marcador/add", web::post().to(endpoint_add))
.route("/marcador/delete", web::post().to(endpoint_delete))
.route(&list_endpoint, web::get().to(endpoint_list))
.route(&add_endpoint, web::post().to(endpoint_add))
.route(&delete_endpoint, web::post().to(endpoint_delete))
})
.bind((host, port))?
.run()
Expand Down

0 comments on commit 55a5239

Please sign in to comment.