Skip to content

Commit

Permalink
feat: remove dictionary relay and add human-friendly dictionary name …
Browse files Browse the repository at this point in the history
…in wikit desktop app
  • Loading branch information
ikey4u committed Feb 19, 2022
1 parent d1f9c53 commit 230c3ae
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 58 deletions.
2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wikit"
version = "0.3.1"
version = "0.3.2"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
15 changes: 8 additions & 7 deletions core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,27 @@
/// [srvcfg]
/// uris = [
/// "file://<filesystem path>",
/// "https://[user@token]<wikit api router>",
/// "http://[user@token]<wikit api router>",
/// ]
/// port = 8888
/// host = "0.0.0.0"
///
/// `[cltcfg]` is used for wikit desktop client, and `[srvcfg]` is used for serving dictionary.
/// `[cltcfg]` is used for wikit desktop client, and `[srvcfg]` is used for serving dictionaries.
///
/// `uris` are a list of [URI](https://en.wikipedia.org/wiki/Uniform_Resource_Identifier) which
/// refers directory resource path, supported URI are
/// refers directory resource path, supported URIs are
///
/// - file://
/// - `file://`
///
/// This refers your local file on your system, such as `file:///home/user/Downloads/awesome.wikit`
/// where `/home/user/Downloads/awesome.wikit` is the full path to your dictionary
/// `awesome.wikit` on your system.
///
/// - https:// and http://
/// - `https://` and `http://`
///
/// Wikit allows client access remote hosted dictionary by network. However, this type URI is
/// only allowed in `[cltcfg]` section, if them does appear in `[srvcfg]`, then will be ignored.
///
/// wikit allows you access remote hosted dictionary by network.
/// Only IP address is allowed for now, domain will be added in future.
///
/// Assuming the base url of the remote server is `https://example.com`, then there must exist following API
///
Expand Down
54 changes: 20 additions & 34 deletions core/src/router.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use crate::config;
use crate::wikit;
use crate::crypto;
use crate::util;

use std::net::{IpAddr, Ipv4Addr};
use std::{sync::Mutex, collections::HashMap};
use std::path::Path;
use std::sync::Arc;

use sqlx::postgres::PgPoolOptions;
use rocket::{Build, Request, response::content, catch, get, catchers, routes};
use regex::Regex;
use rocket::serde::{Serialize, Deserialize, json::Json};
use rocket::{Build, Request, catch, get, catchers, routes};
use rocket::serde::json::Json;
use once_cell::sync::Lazy;

pub static DICTMP: Lazy<Arc<Mutex<HashMap<String, String>>>> = Lazy::new(|| {
Expand All @@ -30,47 +28,35 @@ fn not_found(req: &Request) -> String {

#[get("/list")]
async fn list() -> Json<Vec<wikit::DictList>> {
let mut dict = vec![];
let mut dictlist = vec![];
if let Ok(config) = config::load_config() {
for uri in config.srvcfg.uris.iter() {
let dictid = crypto::md5(uri.as_bytes());
if let Some(dict) = wikit::load_dictionary_from_uri(uri) {
let style_key = format!("style[{}]", dictid);
let script_key = format!("script[{}]", dictid);
match dict {
wikit::WikitDictionary::Local(d) => {
if let Ok(mut dictmp) = DICTMP.lock() {
if dictmp.get(&style_key).is_none() {
dictmp.insert(style_key, d.head.style.clone());
}
if dictmp.get(&script_key).is_none() {
dictmp.insert(script_key, d.head.script.clone());
}
if let wikit::WikitDictionary::Local(d) = dict {
if let Ok(mut dictmp) = DICTMP.lock() {
if dictmp.get(&style_key).is_none() {
dictmp.insert(style_key, d.head.style.clone());
}
},
wikit::WikitDictionary::Remote(d) => {
if let Ok(mut dictmp) = DICTMP.lock() {
if dictmp.get(&style_key).is_none() {
dictmp.insert(style_key, d.get_style(&dictid));
}
if dictmp.get(&script_key).is_none() {
dictmp.insert(script_key, d.get_script(&dictid));
}
if dictmp.get(&script_key).is_none() {
dictmp.insert(script_key, d.head.script.clone());
}
},
}
if let Ok(mut dictmp) = DICTMP.lock() {
dictmp.insert(dictid.clone(), uri.to_string());
}

dictlist.push(wikit::DictList {
name: d.head.name,
id: dictid.clone(),
});
}
}
if let Ok(mut dictmp) = DICTMP.lock() {
dictmp.insert(dictid.clone(), uri.to_string());
}
dict.push(wikit::DictList {
// FIXME(2021-11-21): give a nice dictionar name
name: dictid.clone(),
id: dictid.clone(),
});
}
}
Json(dict)
Json(dictlist)
}

#[get("/style?<dictname>")]
Expand Down
1 change: 1 addition & 0 deletions core/src/wikit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const WIKIT_MAGIC: &'static str = "WIKIT516";
// the latest wikit dictionary format version
const LATEST_WIKIT_FMT_VERSION: u32 = 0x00_00_00_01;

// TODO(2022-02-19): select a better type name
#[derive(Debug, Serialize, Deserialize)]
pub struct DictList {
pub name: String,
Expand Down
2 changes: 1 addition & 1 deletion desktop/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wikit-desktop"
version = "0.1.0-beta.2"
version = "0.1.0-beta.3"
description = "Wikit Desktop - A universal dictionary for human"
authors = ["ikey4u <pwnkeeper@gmail.com>"]
license = "MIT"
Expand Down
12 changes: 4 additions & 8 deletions desktop/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,22 @@ fn lookup(dictid: String, word: String) -> LookupResponse {
}

#[tauri::command]
fn get_dict_list() -> Vec<String> {
fn get_dict_list() -> Vec<wikit::DictList> {
let mut dictlist = vec![];
let mut dictdb = DICTDB.lock().unwrap();

if let Ok(dicts) = wikit::load_client_dictionary() {
for dict in dicts {
match dict {
wikit::WikitDictionary::Local(ref ld) => {
let id = format!("{}", ld.path.display());
dictlist.push(wikit::DictList{ name: ld.head.name.clone(), id: id.clone() });
dictdb.insert(id.clone(), dict);
},
wikit::WikitDictionary::Remote(ref rd) => {
if let Ok(ds) = rd.get_dict_list() {
for d in ds {
dictdb.insert(d.id.clone(), dict.clone());
dictlist.push(d);
}
} else {
println!("failed to get remote dictionary list");
Expand All @@ -91,12 +93,6 @@ fn get_dict_list() -> Vec<String> {
}
}
}

let mut dictlist = vec![];
for (k, _) in dictdb.iter() {
dictlist.push(k.to_string());
}

dictlist
}

Expand Down
14 changes: 12 additions & 2 deletions desktop/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
let r = await ffi.get_dict_list();
$dictSettings.dict.all = r;
if (r.length > 0) {
$dictSettings.dict.selected = [r[0]];
$dictSettings.dict.selected = [r[0].id];
}
unlisten = listen("rust-event", (e) => {
Expand Down Expand Up @@ -48,8 +48,18 @@
return;
}
let dictid = $dictSettings.dict.selected[0];
if (!dictid) {
if ($dictSettings.dict.all.length > 0) {
dictid = $dictSettings.dict.all[0].id
} else {
console.log("no dictionary is found, stop lookup");
return;
}
}
input = input.trim().toLowerCase();
let resp = await ffi.lookup($dictSettings.dict.selected[0], input);
let resp = await ffi.lookup(dictid, input);
let meanings = resp["words"];
let meaning = meanings[input]
if (!meaning) {
Expand Down
7 changes: 2 additions & 5 deletions desktop/src/Settings.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
{#each $dictSettings.dict.all as dict, i}
<Item>
<Graphic>
<Radio bind:group={selection} value="{dict}" />
<Radio bind:group={selection} value="{dict.id}" />
</Graphic>
<Text>{dict}</Text>
<Text>{dict.name}</Text>
</Item>
{/each}
</List>
Expand All @@ -56,9 +56,6 @@
async () => {
let r = await ffi.get_dict_list();
$dictSettings.dict.all = r;
if (r.length > 0) {
$dictSettings.dict.selected = [r[0]];
}
open = true
}
}>+</div>
Expand Down
2 changes: 2 additions & 0 deletions desktop/src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { writable } from 'svelte/store';

export const dictSettings = writable({
'dict': {
// array of dictionary, element format is `{ "id:: "<the dict id>", "name": "dictionary name for human" }`
'all': [],
// selected dictionary ID
'selected': [],
},
});

0 comments on commit 230c3ae

Please sign in to comment.