From 3b8798e1dd52601f7837c30e97c6a7274344e446 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Wed, 3 Apr 2024 20:10:26 +0200 Subject: make server port configurable --- src/example.rs | 13 ++++++++----- src/main.rs | 26 +++++++++++++++++++------- src/server.rs | 8 +++++--- 3 files changed, 32 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/example.rs b/src/example.rs index 494ab73..5881ebd 100644 --- a/src/example.rs +++ b/src/example.rs @@ -4,7 +4,11 @@ use crate::charset::Charset; use crate::*; impl Example { - pub fn gen_furigana(&mut self, dict_idx: &DictIndex, overrides: &HashMap) -> bool { + pub fn gen_furigana( + &mut self, + dict_idx: &DictIndex, + overrides: &HashMap, + ) -> bool { use std::fmt::Write; if let Some(v) = overrides.get(&self.ja) { @@ -49,10 +53,9 @@ impl Example { let reb = match reb { Some(reb) if reb.starts_with('#') => { let ents = dict_idx.get(keb).map(|x| &x[..]).unwrap_or_default(); - if let Some(ent) = ents - .iter() - .find(|ent| ent.ent_seq == reb.strip_prefix('#').unwrap().parse::().unwrap()) - { + if let Some(ent) = ents.iter().find(|ent| { + ent.ent_seq == reb.strip_prefix('#').unwrap().parse::().unwrap() + }) { ent.reb.as_str() } else { is_good = false; diff --git a/src/main.rs b/src/main.rs index c09d045..197ff24 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ extern crate log; use std::collections::HashMap; use std::fs; use std::io; +use std::net::SocketAddr; //use anyhow::Result; use rand::prelude::*; @@ -46,14 +47,17 @@ enum Cmd { Cleanup, AddVocab, Format, - Server, + Server { + #[structopt(default_value = "127.0.0.1:8080")] + bind_addr: SocketAddr, + }, } #[async_std::main] async fn main() { - if std::env::var("RUST_LOG").is_err() { - std::env::set_var("RUST_LOG", "datagengo=info") - } + if std::env::var("RUST_LOG").is_err() { + std::env::set_var("RUST_LOG", "datagengo=info") + } pretty_env_logger::init(); let opt = Opt::from_args(); @@ -82,7 +86,13 @@ async fn main() { ) .expect("parse_jmdict"); let jmdict_idx = index_jmdict(&jmdict); - fs::write("data/jmdict_idx.json", serde_json::to_string_pretty(&jmdict_idx).expect("to_json").as_bytes()).expect("write"); + fs::write( + "data/jmdict_idx.json", + serde_json::to_string_pretty(&jmdict_idx) + .expect("to_json") + .as_bytes(), + ) + .expect("write"); } Cmd::New { truncate, count } => { let kanji_levels = read_kanji_levels().expect("read_kanji_levels"); @@ -159,8 +169,10 @@ async fn main() { format_index(&batches, &kanji_levels).expect("format_index"); format_about().expect("format_about"); } - Cmd::Server => { - server::server_main().await.expect("error in server"); + Cmd::Server { bind_addr } => { + server::server_main(bind_addr) + .await + .expect("error in server"); } } } diff --git a/src/server.rs b/src/server.rs index 51191f1..985d3db 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,3 +1,5 @@ +use std::net::SocketAddr; + use anyhow::{anyhow, Result}; use futures::stream::TryStreamExt; use rand::prelude::*; @@ -11,7 +13,7 @@ use crate::example::*; use crate::format::*; use crate::*; -pub async fn server_main() -> tide::Result<()> { +pub async fn server_main(bind_addr: SocketAddr) -> tide::Result<()> { // ---- load data files ---- info!("Loading kanji levels..."); @@ -73,9 +75,9 @@ pub async fn server_main() -> tide::Result<()> { // ---- serve actual http ---- - info!("Server listening on 127.0.0.1:8080"); + info!("Server listening on {}", bind_addr); debug!("RAM: {}", ALLOCATOR.allocated() / 1024); - app.listen("127.0.0.1:8080").await?; + app.listen(bind_addr).await?; Ok(()) } -- cgit v1.2.3