rustbasic-core 0.1.21

Core framework logic for RustBasic - A modern web framework for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use crate::app::Config;
use crate::tracing;
use crate::session_manager::RustBasicSessionStore;
use crate::router::{Router, Response};
use crate::requests::Request;
use std::net::SocketAddr;
use sqlx::AnyPool;
use std::sync::Arc;
use std::process::Command;
use std::convert::Infallible;
use tokio::net::TcpListener;
use hyper::service::service_fn;
use hyper_util::rt::TokioIo;
use hyper::server::conn::http1;
use crate::rand::distr::SampleString;

#[derive(Clone)]
pub struct AppState {
    pub db: AnyPool,
    pub config: Arc<Config>,
}

static EMBEDDED_PUBLIC_GET: std::sync::OnceLock<fn(&str) -> Option<rust_embed::EmbeddedFile>> = std::sync::OnceLock::new();

pub fn set_embedded_public(f: fn(&str) -> Option<rust_embed::EmbeddedFile>) {
    EMBEDDED_PUBLIC_GET.set(f).ok();
}

fn guess_mime(path: &str) -> &'static str {
    if path.ends_with(".js") {
        "application/javascript"
    } else if path.ends_with(".css") {
        "text/css"
    } else if path.ends_with(".html") {
        "text/html"
    } else if path.ends_with(".png") {
        "image/png"
    } else if path.ends_with(".jpg") || path.ends_with(".jpeg") {
        "image/jpeg"
    } else if path.ends_with(".svg") {
        "image/svg+xml"
    } else if path.ends_with(".ico") {
        "image/x-icon"
    } else if path.ends_with(".json") {
        "application/json"
    } else if path.ends_with(".woff") {
        "font/woff"
    } else if path.ends_with(".woff2") {
        "font/woff2"
    } else {
        "application/octet-stream"
    }
}

pub async fn start_server(
    cfg: Config, 
    session_store: RustBasicSessionStore,
    db: AnyPool,
    app_router: Router<AppState>,
) {
    // 0. Kill port jika sedang digunakan (Force Restart)
    kill_port_if_in_use(cfg.app_port);

    // 0.5 Set Timezone Global
    unsafe {
        std::env::set_var("TZ", &cfg.app_timezone);
    }

    // 1. Inisialisasi State
    let state = AppState {
        db,
        config: Arc::new(cfg.clone()),
    };

    // 3. Tentukan Alamat
    let addr_str = format!("{}:{}", cfg.app_host, cfg.app_port);
    let addr: SocketAddr = addr_str.parse().expect("Alamat server tidak valid");
    
    tracing::info!("{} berjalan di: http://{}", cfg.app_name, addr);
    
    // 4. Jalankan Server
    let listener = TcpListener::bind(addr).await.unwrap();
    
    loop {
        let (stream, peer_addr) = match listener.accept().await {
            Ok(ok) => ok,
            Err(_) => continue,
        };
        
        let io = TokioIo::new(stream);
        let state = state.clone();
        let router = app_router.clone();
        let peer_ip = peer_addr.ip().to_string();
        let session_store = session_store.clone();

        tokio::task::spawn(async move {
            let service = service_fn(move |req: hyper::Request<hyper::body::Incoming>| {
                let state = state.clone();
                let router = router.clone();
                let peer_ip = peer_ip.clone();
                let session_store = session_store.clone();
                async move {
                    let res = handle_http_request(req, peer_ip, state, router, session_store).await;
                    Ok::<_, Infallible>(res)
                }
            });

            if let Err(err) = http1::Builder::new()
                .serve_connection(io, service)
                .await
            {
                tracing::debug!("Error serving connection: {:?}", err);
            }
        });
    }
}

pub(crate) fn match_path(route_path: &str, req_path: &str) -> bool {
    let r_parts: Vec<&str> = route_path.split('/').filter(|s| !s.is_empty()).collect();
    let q_parts: Vec<&str> = req_path.split('/').filter(|s| !s.is_empty()).collect();
    
    if r_parts.len() != q_parts.len() {
        return false;
    }
    
    for (r, q) in r_parts.iter().zip(q_parts.iter()) {
        if r.starts_with(':') || (r.starts_with('{') && r.ends_with('}')) {
            continue;
        }
        if r != q {
            return false;
        }
    }
    true
}

/// Ekstrak nilai route parameter dari URL request.
/// Contoh: route="/user/{id}", path="/user/42" → {"id": "42"}
pub(crate) fn extract_params(route_path: &str, req_path: &str) -> std::collections::HashMap<String, String> {
    let mut params = std::collections::HashMap::new();
    let r_parts: Vec<&str> = route_path.split('/').filter(|s| !s.is_empty()).collect();
    let q_parts: Vec<&str> = req_path.split('/').filter(|s| !s.is_empty()).collect();

    for (r, q) in r_parts.iter().zip(q_parts.iter()) {
        if r.starts_with('{') && r.ends_with('}') {
            // Sintaks {param}
            let key = &r[1..r.len() - 1];
            params.insert(key.to_string(), q.to_string());
        } else if r.starts_with(':') {
            // Sintaks :param
            let key = &r[1..];
            params.insert(key.to_string(), q.to_string());
        }
    }
    params
}

async fn serve_static_or_404(path: &str, state: &AppState) -> Response {
    let clean_path = path.trim_start_matches('/');
    let file_path = if clean_path.is_empty() { "index.html" } else { clean_path };

    if state.config.app_debug {
        let disk_path = std::path::Path::new("public").join(file_path);
        if disk_path.exists() && disk_path.is_file() {
            if let Ok(content) = std::fs::read(&disk_path) {
                let mime = guess_mime(file_path);
                return http::Response::builder()
                    .header(http::header::CONTENT_TYPE, mime)
                    .body(content)
                    .unwrap();
            }
        }
    } else {
        if let Some(file) = EMBEDDED_PUBLIC_GET.get().and_then(|f| f(file_path)) {
            let mime = guess_mime(file_path);
            return http::Response::builder()
                .header(http::header::CONTENT_TYPE, mime)
                .body(file.data.to_vec())
                .unwrap();
        }
    }

    crate::errors::ErrorController::not_found().await
}

async fn handle_http_request(
    hyper_req: hyper::Request<hyper::body::Incoming>,
    peer_ip: String,
    state: AppState,
    router: Router<AppState>,
    session_store: RustBasicSessionStore,
) -> hyper::Response<http_body_util::Full<hyper::body::Bytes>> {
    use http_body_util::BodyExt;
    
    let (parts, body) = hyper_req.into_parts();
    let method = parts.method.clone();
    let uri = parts.uri.clone();
    let path = uri.path().to_string();
    
    let mut headers = std::collections::HashMap::new();
    for (name, val) in parts.headers.iter() {
        if let Ok(val_str) = val.to_str() {
            headers.insert(name.as_str().to_lowercase(), val_str.to_string());
        }
    }
    
    let mut inputs = serde_json::json!({});
    if let Some(query) = uri.query() {
        if let Ok(params) = crate::serde_urlencoded::from_str::<std::collections::HashMap<String, String>>(query) {
            for (k, v) in params {
                inputs[k] = serde_json::json!(v);
            }
        }
    }
    
    let body_bytes = body.collect().await.map(|c| c.to_bytes()).unwrap_or_default();
    let content_type = headers.get("content-type").map(|s| s.as_str()).unwrap_or("");
    if content_type.starts_with("application/json") {
        if let Ok(json_val) = serde_json::from_slice::<serde_json::Value>(&body_bytes) {
            if let serde_json::Value::Object(obj) = json_val {
                for (k, v) in obj {
                    inputs[k] = v;
                }
            }
        }
    } else if content_type.starts_with("application/x-www-form-urlencoded") {
        if let Ok(params) = crate::serde_urlencoded::from_bytes::<std::collections::HashMap<String, String>>(&body_bytes) {
            for (k, v) in params {
                inputs[k] = serde_json::json!(v);
            }
        }
    }
    
    let mut session_id = None;
    if let Some(cookie_header) = headers.get("cookie") {
        for cookie in cookie_header.split(';') {
            let parts: Vec<&str> = cookie.split('=').map(|s| s.trim()).collect();
            if parts.len() == 2 && parts[0] == "rustbasic_session" {
                session_id = Some(parts[1].to_string());
                break;
            }
        }
    }
    
    let id = session_id.unwrap_or_else(|| {
        crate::rand::distr::Alphanumeric.sample_string(&mut crate::rand::rng(), 40)
    });
    
    let session_data = if let Some(payload_str) = session_store.load(&id).await {
        serde_json::from_str::<serde_json::Map<String, serde_json::Value>>(&payload_str).unwrap_or_default()
    } else {
        serde_json::Map::new()
    };
    
    let session = crate::session::Session::new(id.clone());
    *session.data.lock().unwrap() = session_data;
    
    if session.get::<String>("_token").is_none() {
        let new_token = crate::rand::distr::Alphanumeric.sample_string(&mut crate::rand::rng(), 40);
        session.set("_token", new_token);
    }
    
    let req = Request {
        inputs,
        method: method.clone(),
        path: path.clone(),
        headers,
        session: session.clone(),
        state: state.clone(),
        ip_address: peer_ip,
        params: std::collections::HashMap::new(), // diisi oleh RouteDispatcher saat match
    };
    
    struct RouteDispatcher {
        router: Router<AppState>,
        state: AppState,
    }

    #[crate::async_trait]
    impl crate::router::ErasedHandler for RouteDispatcher {
        async fn call(&self, req: Request) -> Response {
            let method = req.method.clone();
            let path = req.path.clone();
            
            let mut matched_handler = None;
            let mut matched_params = std::collections::HashMap::new();
            for route in &self.router.routes {
                if match_path(&route.path, &path) {
                    for (m, h) in &route.handlers {
                        if m == &method {
                            matched_handler = Some(h.clone());
                            matched_params = extract_params(&route.path, &path);
                            break;
                        }
                    }
                }
                if matched_handler.is_some() {
                    break;
                }
            }
            
            if let Some(handler) = matched_handler {
                // Inject route params ke request
                let mut req = req;
                req.params = matched_params;
                let mut chain = std::sync::Arc::new(crate::middleware::MiddlewareChain::End(handler));
                for mw in self.router.middlewares.iter().rev() {
                    chain = std::sync::Arc::new(crate::middleware::MiddlewareChain::Next(mw.clone(), chain));
                }
                chain.next(req).await
            } else {
                serve_static_or_404(&path, &self.state).await
            }
        }
    }

    let dispatcher = std::sync::Arc::new(RouteDispatcher {
        router,
        state: state.clone(),
    });

    let mut chain = std::sync::Arc::new(crate::middleware::MiddlewareChain::End(dispatcher));
    chain = std::sync::Arc::new(crate::middleware::MiddlewareChain::Next(
        crate::middleware::from_fn(crate::middleware::security_headers::security_headers_middleware),
        chain,
    ));
    chain = std::sync::Arc::new(crate::middleware::MiddlewareChain::Next(
        crate::middleware::from_fn(crate::middleware::logging::logging_middleware),
        chain,
    ));

    let ip = req.ip_address.clone();
    let res = chain.next(req).await;
    
    let final_session_data = session.data.lock().unwrap().clone();
    if let Ok(session_json) = serde_json::to_string(&final_session_data) {
        session_store.store(&id, &session_json, &ip).await;
    }
    
    let (mut res_parts, res_body) = res.into_parts();
    let cookie_val = format!("rustbasic_session={}; Path=/; HttpOnly; SameSite=Lax", id);
    res_parts.headers.insert(
        http::header::SET_COOKIE,
        http::HeaderValue::from_str(&cookie_val).unwrap(),
    );
    
    hyper::Response::from_parts(res_parts, http_body_util::Full::new(hyper::body::Bytes::from(res_body)))
}

fn kill_port_if_in_use(port: u16) {
    #[cfg(target_os = "macos")]
    {
        let output = Command::new("lsof")
            .arg("-t")
            .arg(format!("-i:{}", port))
            .output();

        if let Ok(out) = output {
            let pid_str = String::from_utf8_lossy(&out.stdout).trim().to_string();
            if !pid_str.is_empty() {
                tracing::warn!("Port {} sedang digunakan oleh PID {}. Membunuh proses...", port, pid_str);
                
                for pid in pid_str.split('\n') {
                    if !pid.is_empty() {
                        let _ = Command::new("kill")
                            .arg("-9")
                            .arg(pid)
                            .output();
                    }
                }

                std::thread::sleep(std::time::Duration::from_millis(500));
            }
        }
    }

    #[cfg(target_os = "linux")]
    {
        let _ = Command::new("fuser")
            .arg("-k")
            .arg(format!("{}/tcp", port))
            .output();
    }

    #[cfg(target_os = "windows")]
    {
        let output = Command::new("cmd")
            .args(&["/C", &format!("netstat -ano | findstr :{}", port)])
            .output();

        if let Ok(out) = output {
            let stdout = String::from_utf8_lossy(&out.stdout);
            let mut found = false;
            for line in stdout.lines() {
                let parts: Vec<&str> = line.split_whitespace().collect();
                if let Some(pid) = parts.last() {
                    if pid.parse::<u32>().is_ok() {
                        tracing::warn!("Port {} sedang digunakan oleh PID {}. Membunuh proses...", port, pid);
                        let _ = Command::new("taskkill")
                            .args(&["/F", "/PID", pid])
                            .output();
                        found = true;
                    }
                }
            }
            if found {
                std::thread::sleep(std::time::Duration::from_millis(500));
            }
        }
    }
}