Skip to content

Commit 9de03e0

Browse files
committed
서버 타임아웃 60초 + 체크마크 UI + legacy cleanup
- wait_for_server 15초 → 60초, 프로세스 alive 체크 - server.log 파일로 stderr 기록 (디버깅용) - 셋업 UI: 스피너 → ✓/✗ 교체 방식 - cleanup_legacy: 이전 uv init 잔해 자동 정리 - venv 검증: python.exe 없으면 재생성
1 parent 148424f commit 9de03e0

4 files changed

Lines changed: 154 additions & 75 deletions

File tree

‎Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "dartlab-desktop"
3-
version = "0.3.0"
3+
version = "0.3.2"
44
edition = "2024"
55
description = "DartLab AI Desktop Launcher for Windows"
66
license = "MIT"

‎src/main.rs‎

Lines changed: 73 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use tao::window::WindowBuilder;
1616
use wry::WebViewBuilder;
1717

1818
#[derive(Debug)]
19+
#[allow(dead_code)]
1920
enum AppEvent {
2021
Log(String),
2122
Ready,
@@ -42,26 +43,26 @@ const SETUP_HTML: &str = r#"<!DOCTYPE html>
4243
h1 { font-size: 28px; font-weight: 700; margin-bottom: 8px; }
4344
.sub { color: #94a3b8; font-size: 14px; margin-bottom: 40px; }
4445
#log {
45-
width: 400px;
46+
width: 420px;
4647
font-family: 'Consolas', 'Courier New', monospace;
4748
font-size: 13px;
4849
color: #94a3b8;
49-
line-height: 1.8;
50-
text-align: center;
50+
line-height: 2;
51+
text-align: left;
5152
}
52-
.line { opacity: 0; animation: fadeIn 0.3s forwards; }
53-
.line.ok { color: #34d399; }
54-
.line.err { color: #ea4647; }
53+
.step { opacity: 0; animation: fadeIn 0.3s forwards; display: flex; align-items: center; gap: 10px; }
54+
.icon { width: 18px; text-align: center; flex-shrink: 0; }
5555
.spinner {
5656
display: inline-block;
57-
width: 12px; height: 12px;
57+
width: 14px; height: 14px;
5858
border: 2px solid #64748b;
5959
border-top-color: #ea4647;
6060
border-radius: 50%;
6161
animation: spin 0.8s linear infinite;
62-
margin-right: 8px;
63-
vertical-align: middle;
6462
}
63+
.check { color: #34d399; font-weight: bold; }
64+
.fail { color: #ea4647; font-weight: bold; }
65+
.detail { color: #64748b; font-size: 12px; margin-left: 28px; }
6566
@keyframes spin { to { transform: rotate(360deg); } }
6667
@keyframes fadeIn { to { opacity: 1; } }
6768
</style>
@@ -71,13 +72,38 @@ const SETUP_HTML: &str = r#"<!DOCTYPE html>
7172
<p class="sub">AI 기업분석 준비 중</p>
7273
<div id="log"></div>
7374
<script>
74-
function addLog(msg, cls) {
75+
let currentStep = null;
76+
function startStep(label) {
7577
const el = document.getElementById('log');
7678
const div = document.createElement('div');
77-
div.className = 'line' + (cls ? ' ' + cls : '');
78-
div.innerHTML = msg;
79+
div.className = 'step';
80+
div.innerHTML = '<span class="icon"><span class="spinner"></span></span><span>' + label + '</span>';
81+
el.appendChild(div);
82+
currentStep = div;
83+
}
84+
function completeStep() {
85+
if (!currentStep) return;
86+
currentStep.querySelector('.icon').innerHTML = '<span class="check">&#10003;</span>';
87+
currentStep = null;
88+
}
89+
function failStep(msg) {
90+
if (!currentStep) return;
91+
currentStep.querySelector('.icon').innerHTML = '<span class="fail">&#10007;</span>';
92+
if (msg) {
93+
const el = document.getElementById('log');
94+
const detail = document.createElement('div');
95+
detail.className = 'detail';
96+
detail.textContent = msg;
97+
el.appendChild(detail);
98+
}
99+
currentStep = null;
100+
}
101+
function addInfo(msg) {
102+
const el = document.getElementById('log');
103+
const div = document.createElement('div');
104+
div.className = 'detail';
105+
div.textContent = msg;
79106
el.appendChild(div);
80-
el.scrollTop = el.scrollHeight;
81107
}
82108
</script>
83109
</body>
@@ -112,19 +138,17 @@ fn main() {
112138
event_loop.run(move |event, _, control_flow| {
113139
*control_flow = ControlFlow::Wait;
114140

115-
if let Ok(app_event) = rx.try_recv() {
141+
while let Ok(app_event) = rx.try_recv() {
116142
match app_event {
117-
AppEvent::Log(msg) => {
118-
let escaped = msg.replace('\\', "\\\\").replace('\'', "\\'").replace('\n', "\\n");
119-
let js = format!("addLog('{escaped}', '')");
120-
webview.evaluate_script(&js).ok();
143+
AppEvent::Log(script) => {
144+
webview.evaluate_script(&script).ok();
121145
}
122146
AppEvent::Ready => {
123147
let _ = webview.load_url("http://localhost:8400");
124148
}
125149
AppEvent::Error(msg) => {
126150
let escaped = msg.replace('\\', "\\\\").replace('\'', "\\'").replace('\n', "\\n");
127-
let js = format!("addLog('{escaped}', 'err')");
151+
let js = format!("failStep('{escaped}')");
128152
webview.evaluate_script(&js).ok();
129153
}
130154
}
@@ -144,71 +168,66 @@ fn main() {
144168
}
145169

146170
fn run_setup(tx: mpsc::Sender<AppEvent>, proxy: tao::event_loop::EventLoopProxy<AppEvent>) {
147-
let send = |msg: &str| {
148-
let _ = tx.send(AppEvent::Log(msg.to_string()));
149-
let _ = proxy.send_event(AppEvent::Log(msg.to_string()));
150-
};
151-
let send_ok = |msg: &str| {
152-
let formatted = format!("<span class=\"ok\">+ {msg}</span>");
153-
let _ = tx.send(AppEvent::Log(formatted.clone()));
154-
let _ = proxy.send_event(AppEvent::Log(formatted));
155-
};
156-
let send_err = |msg: &str| {
157-
let _ = tx.send(AppEvent::Error(msg.to_string()));
158-
let _ = proxy.send_event(AppEvent::Error(msg.to_string()));
171+
let js = |script: &str| {
172+
let _ = tx.send(AppEvent::Log(script.to_string()));
173+
let _ = proxy.send_event(AppEvent::Log(script.to_string()));
159174
};
160175

161176
let app_dir = paths::app_dir();
162177
if !app_dir.exists() {
163178
std::fs::create_dir_all(&app_dir).ok();
164179
}
165180

166-
send("<span class=\"spinner\"></span> [1/4] uv 설치 확인 중...");
181+
js("startStep('uv 확인 중...')");
167182
if let Err(e) = setup::ensure_uv(&app_dir) {
168-
send_err(&format!("uv 설치 실패: {e}"));
183+
js(&format!("failStep('uv 설치 실패: {e}')"));
169184
return;
170185
}
171-
send_ok("uv 준비 완료");
186+
js("completeStep()");
172187

173-
send("<span class=\"spinner\"></span> [2/4] Python 환경 확인 중...");
188+
js("startStep('Python + DartLab 확인 중...')");
174189
if let Err(e) = setup::ensure_dartlab(&app_dir) {
175-
send_err(&format!("DartLab 설치 실패: {e}"));
190+
js(&format!("failStep('DartLab 설치 실패: {e}')"));
176191
return;
177192
}
178-
send_ok("DartLab 설치 완료");
193+
js("completeStep()");
179194

180195
match updater::check_update(&app_dir) {
181196
Ok(Some(latest)) => {
182-
send(&format!("<span class=\"spinner\"></span> 새 버전 발견: {latest} — 업데이트 중..."));
197+
js(&format!("startStep('새 버전 {latest} 업데이트 중...')"));
183198
if let Err(e) = updater::do_update(&app_dir) {
184-
send(&format!("! 업데이트 실패 (기존 버전으로 진행): {e}"));
199+
js(&format!("failStep('업데이트 실패: {e}')"));
185200
} else {
186-
send_ok("업데이트 완료");
201+
js("completeStep()");
187202
}
188203
}
189204
_ => {}
190205
}
191206

192-
send("<span class=\"spinner\"></span> [3/4] Ollama 확인 중...");
207+
js("startStep('Ollama 확인 중...')");
193208
if let Err(e) = ollama::ensure_ollama() {
194-
send(&format!("! Ollama 건너뜀: {e}"));
209+
js(&format!("failStep('{e}')"));
195210
} else {
196-
send_ok("Ollama 준비 완료");
211+
js("completeStep()");
197212
}
198213

199-
send("<span class=\"spinner\"></span> [4/4] 서버 시작 중...");
214+
js("startStep('서버 시작 중...')");
200215
if let Err(e) = runner::start_server(&app_dir) {
201-
send_err(&format!("서버 시작 실패: {e}"));
216+
js(&format!("failStep('서버 시작 실패: {e}')"));
202217
return;
203218
}
204-
205-
send("<span class=\"spinner\"></span> 서버 응답 대기 중...");
206-
if runner::wait_for_server(15) {
207-
send_ok("준비 완료!");
208-
std::thread::sleep(std::time::Duration::from_millis(500));
209-
let _ = tx.send(AppEvent::Ready);
210-
let _ = proxy.send_event(AppEvent::Ready);
211-
} else {
212-
send_err("서버 응답 시간 초과");
219+
js("completeStep()");
220+
221+
js("startStep('서버 응답 대기 중...')");
222+
match runner::wait_for_server(60) {
223+
Ok(()) => {
224+
js("completeStep()");
225+
std::thread::sleep(std::time::Duration::from_millis(500));
226+
let _ = tx.send(AppEvent::Ready);
227+
let _ = proxy.send_event(AppEvent::Ready);
228+
}
229+
Err(e) => {
230+
js(&format!("failStep('{e}')"));
231+
}
213232
}
214233
}

‎src/runner.rs‎

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ static SERVER_PROCESS: Mutex<Option<Child>> = Mutex::new(None);
1212
pub fn start_server(app_dir: &Path) -> Result<(), String> {
1313
let dartlab = paths::dartlab_bin(app_dir);
1414

15+
let log_file = app_dir.join("server.log");
16+
let stderr_file = std::fs::File::create(&log_file).map_err(|e| e.to_string())?;
17+
let stdout_file = stderr_file.try_clone().map_err(|e| e.to_string())?;
18+
1519
let child = Command::new(&dartlab)
1620
.args(["ai", "--port", &PORT.to_string(), "--no-browser"])
1721
.env("DARTLAB_NO_BROWSER", "1")
1822
.current_dir(app_dir)
19-
.stdout(Stdio::null())
20-
.stderr(Stdio::null())
23+
.stdout(Stdio::from(stdout_file))
24+
.stderr(Stdio::from(stderr_file))
2125
.creation_flags(CREATE_NO_WINDOW)
2226
.spawn()
2327
.map_err(|e| e.to_string())?;
@@ -29,7 +33,7 @@ pub fn start_server(app_dir: &Path) -> Result<(), String> {
2933
Ok(())
3034
}
3135

32-
pub fn wait_for_server(timeout_secs: u64) -> bool {
36+
pub fn wait_for_server(timeout_secs: u64) -> Result<(), String> {
3337
let client = reqwest::blocking::Client::builder()
3438
.timeout(std::time::Duration::from_secs(2))
3539
.build()
@@ -39,13 +43,45 @@ pub fn wait_for_server(timeout_secs: u64) -> bool {
3943
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(timeout_secs);
4044

4145
while std::time::Instant::now() < deadline {
46+
if let Ok(mut lock) = SERVER_PROCESS.lock() {
47+
if let Some(ref mut child) = *lock {
48+
if let Some(exit) = child.try_wait().ok().flatten() {
49+
let app_dir = paths::app_dir();
50+
let log_path = app_dir.join("server.log");
51+
let log_tail = std::fs::read_to_string(&log_path)
52+
.unwrap_or_default()
53+
.lines()
54+
.rev()
55+
.take(5)
56+
.collect::<Vec<_>>()
57+
.into_iter()
58+
.rev()
59+
.collect::<Vec<_>>()
60+
.join("\n");
61+
return Err(format!("서버 프로세스 종료됨 (code: {exit})\n{log_tail}"));
62+
}
63+
}
64+
}
65+
4266
if client.get(&url).send().is_ok_and(|r| r.status().is_success()) {
43-
return true;
67+
return Ok(());
4468
}
4569
std::thread::sleep(std::time::Duration::from_millis(500));
4670
}
4771

48-
false
72+
let app_dir = paths::app_dir();
73+
let log_path = app_dir.join("server.log");
74+
let log_tail = std::fs::read_to_string(&log_path)
75+
.unwrap_or_default()
76+
.lines()
77+
.rev()
78+
.take(5)
79+
.collect::<Vec<_>>()
80+
.into_iter()
81+
.rev()
82+
.collect::<Vec<_>>()
83+
.join("\n");
84+
Err(format!("서버 응답 시간 초과 ({timeout_secs}초)\n{log_tail}"))
4985
}
5086

5187
pub fn stop_server() {

‎src/setup.rs‎

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,20 @@ pub fn ensure_uv(app_dir: &Path) -> Result<(), String> {
5757
pub fn ensure_dartlab(app_dir: &Path) -> Result<(), String> {
5858
let uv = paths::uv_bin(app_dir);
5959
let venv = paths::venv_dir(app_dir);
60+
let dartlab_bin = paths::dartlab_bin(app_dir);
6061

61-
if !venv.exists() {
62-
ui::print_step(2, 4, "Python 환경 생성 중...");
62+
cleanup_legacy(app_dir);
63+
64+
let needs_venv = if venv.exists() {
65+
!venv.join("Scripts").join("python.exe").exists()
66+
} else {
67+
true
68+
};
69+
70+
if needs_venv {
71+
if venv.exists() {
72+
std::fs::remove_dir_all(&venv).ok();
73+
}
6374

6475
let status = Command::new(&uv)
6576
.args(["venv", venv.to_str().unwrap(), "--python", "3.12"])
@@ -71,27 +82,40 @@ pub fn ensure_dartlab(app_dir: &Path) -> Result<(), String> {
7182
if !status.success() {
7283
return Err("uv venv failed".into());
7384
}
74-
75-
ui::print_ok("Python 환경 생성 완료");
7685
}
7786

78-
ui::print_step(3, 4, "DartLab 설치/업데이트 중...");
79-
80-
let status = Command::new(&uv)
81-
.args(["pip", "install", "dartlab[ai]", "--python", venv.join("Scripts").join("python.exe").to_str().unwrap()])
82-
.current_dir(app_dir)
83-
.creation_flags(CREATE_NO_WINDOW)
84-
.status()
85-
.map_err(|e| e.to_string())?;
87+
if !dartlab_bin.exists() {
88+
let python = paths::python_bin(app_dir);
89+
let status = Command::new(&uv)
90+
.args(["pip", "install", "dartlab[ai]", "--python", python.to_str().unwrap()])
91+
.current_dir(app_dir)
92+
.creation_flags(CREATE_NO_WINDOW)
93+
.status()
94+
.map_err(|e| e.to_string())?;
8695

87-
if !status.success() {
88-
return Err("uv pip install dartlab[ai] failed".into());
96+
if !status.success() {
97+
return Err("uv pip install dartlab[ai] failed".into());
98+
}
8999
}
90100

91-
ui::print_ok("DartLab 설치 완료");
92101
Ok(())
93102
}
94103

104+
fn cleanup_legacy(app_dir: &Path) {
105+
let pyproject = app_dir.join("pyproject.toml");
106+
if pyproject.exists() {
107+
std::fs::remove_file(&pyproject).ok();
108+
}
109+
let uv_lock = app_dir.join("uv.lock");
110+
if uv_lock.exists() {
111+
std::fs::remove_file(&uv_lock).ok();
112+
}
113+
let old_venv = app_dir.join("venv");
114+
if old_venv.exists() {
115+
std::fs::remove_dir_all(&old_venv).ok();
116+
}
117+
}
118+
95119
fn download_file(url: &str, dest: &Path) -> Result<(), String> {
96120
let resp = reqwest::blocking::get(url).map_err(|e| format!("Download failed: {e}"))?;
97121

0 commit comments

Comments
 (0)