forge
Explore
Log in

Don't you dare go Hollow

hezarfen_mobile

Clone
★ 0

0 watching0 forksRust5.5 MiB3424bc6Burak Boduroğlu3 months ago13 commits

lib.rs77 lines, 2.9 KiBHistoryRawDownload
1mod proxy;
2
3use tauri::{Manager, WebviewUrl, WebviewWindowBuilder};
4
5/// Where the API lives. Baked in at build time so release artifacts point at
6/// production without any runtime configuration:
7///
8/// ```sh
9/// HEZARFEN_BACKEND_ORIGIN=https://api.hezarfen.co cargo tauri android build
10/// ```
11const BUILD_TIME_BACKEND: Option<&str> = option_env!("HEZARFEN_BACKEND_ORIGIN");
12
13/// Dev default. On an emulator or a USB-attached device this is the developer's
14/// own machine, reached through `adb reverse tcp:8080 tcp:8080` (see README) —
15/// the same address the web app's Vite proxy targets, so one backend serves
16/// both.
17const DEFAULT_BACKEND: &str = "http://127.0.0.1:8080";
18
19/// A QA override, read once at startup from `<app-config>/backend.txt`. Lets a
20/// tester repoint a signed build at staging without a rebuild.
21fn runtime_backend<R: tauri::Runtime>(app: &tauri::AppHandle<R>) -> Option<String> {
22 let path = app.path().app_config_dir().ok()?.join("backend.txt");
23 let raw = std::fs::read_to_string(path).ok()?;
24 let trimmed = raw.trim();
25 if trimmed.is_empty() {
26 return None;
27 }
28 Some(trimmed.to_string())
29}
30
31fn backend_origin<R: tauri::Runtime>(app: &tauri::AppHandle<R>) -> String {
32 runtime_backend(app)
33 .or_else(|| BUILD_TIME_BACKEND.map(str::to_string))
34 .unwrap_or_else(|| DEFAULT_BACKEND.to_string())
35}
36
37#[cfg_attr(mobile, tauri::mobile_entry_point)]
38pub fn run() {
39 tauri::Builder::default()
40 .plugin(tauri_plugin_os::init())
41 .setup(|app| {
42 let handle = app.handle().clone();
43 let backend = backend_origin(&handle);
44
45 // Under `tauri dev` the bundle is empty and Vite serves the app; we
46 // proxy to it so dev and release share one origin (and one cookie
47 // jar). Tauri sets up `adb reverse` for this port on Android, so
48 // the device reaches the host's dev server on localhost.
49 let dev_url = if tauri::is_dev() {
50 app.config()
51 .build
52 .dev_url
53 .as_ref()
54 .map(|url| url.to_string())
55 } else {
56 None
57 };
58
59 let port = proxy::start(handle, backend.clone(), dev_url)?;
60 log::info!("origin server on 127.0.0.1:{port}, backend {backend}");
61
62 let url = format!("http://127.0.0.1:{port}/")
63 .parse()
64 .expect("the loopback URL is always valid");
65
66 // The window is built here rather than declared in tauri.conf.json
67 // because its URL is only known once the server has a port.
68 WebviewWindowBuilder::new(app, "main", WebviewUrl::External(url))
69 .title("Hezarfen")
70 .inner_size(430.0, 932.0)
71 .build()?;
72
73 Ok(())
74 })
75 .run(tauri::generate_context!())
76 .expect("error while running the Hezarfen app");
77}