diff options
author | Alex Auvolat <alex@adnab.me> | 2024-04-03 20:10:26 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2024-04-03 20:10:26 +0200 |
commit | 3b8798e1dd52601f7837c30e97c6a7274344e446 (patch) | |
tree | 8d4c8f80841eaefa1a8711c67e80c2233073ae4a /src | |
parent | 62e6641203f6af2c7ad90cace7fff045f867218e (diff) | |
download | datagengo-3b8798e1dd52601f7837c30e97c6a7274344e446.tar.gz datagengo-3b8798e1dd52601f7837c30e97c6a7274344e446.zip |
make server port configurable
Diffstat (limited to 'src')
-rw-r--r-- | src/example.rs | 13 | ||||
-rw-r--r-- | src/main.rs | 26 | ||||
-rw-r--r-- | src/server.rs | 8 |
3 files changed, 32 insertions, 15 deletions
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<String, String>) -> bool { + pub fn gen_furigana( + &mut self, + dict_idx: &DictIndex, + overrides: &HashMap<String, String>, + ) -> 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::<u64>().unwrap()) - { + if let Some(ent) = ents.iter().find(|ent| { + ent.ent_seq == reb.strip_prefix('#').unwrap().parse::<u64>().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(()) } |