glote 0.2.0

A fast Rust web library
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
use std::io::{ BufRead, BufReader, Read };
use std::net::TcpListener;
use std::sync::{ Arc, RwLock };
use std::time::Instant;

use crate::request::{ parse_path_params, Request };
use crate::response::Response;
use crate::workerpool::WorkerPool;

// Atomically reference counter with safely travale though thread with own lifetime/ownership
pub type Next<'a> = &'a mut dyn FnMut();

pub type Handler = dyn Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>) + Send + Sync + 'static;
pub type Middleware = dyn Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>, Next) +
    Send +
    Sync +
    'static;

// Metadata of routes
#[derive(Clone)]
struct Route {
    method: String,
    path: String,
    middleware: Vec<Arc<Middleware>>,
    handler: Arc<Handler>,
}

pub struct Glote {
    routes: Arc<RwLock<Vec<Route>>>,
    middleware: Arc<RwLock<Vec<Arc<Middleware>>>>,
    pool: WorkerPool,
    static_path: Option<String>,
}

impl Glote {
    // Returns Arc self
    pub fn new() -> Arc<Self> {
        // Number of core in our cpu
        let num_cores = std::thread
            ::available_parallelism()
            .map(|n| n.get())
            .unwrap_or(1);
        // Total worker Defualt (total core * 4) or 4
        Arc::new(Self {
            routes: Arc::new(RwLock::new(Vec::new())),
            middleware: Arc::new(RwLock::new(Vec::new())),
            pool: WorkerPool::new(num_cores * 4),
            static_path: None,
        })
    }
    // Manually set number of workers
    pub fn set_warkers(&mut self, size: usize) {
        self.pool = WorkerPool::new(size);
    }

    // Runs Global+route middleware and final handler
    fn run_handlers(
        &self,
        req: Arc<RwLock<Request>>,
        res: Arc<RwLock<Response>>,
        middlewares: &[Arc<Middleware>],
        final_handler: impl FnMut() + Send + 'static
    ) {
        let req = Arc::clone(&req);
        let res = Arc::clone(&res);
        let middlewares = middlewares.to_vec();

        // Store final handler into box for linklist fashion with multiple middlewares
        let mut final_handler = Some(Box::new(final_handler) as Box<dyn FnMut()>);
        // makes chain in reverse way
        for mw in middlewares.into_iter().rev() {
            let req = req.clone();
            let res = res.clone();
            let mut next = final_handler.take().unwrap();
            final_handler = Some(
                Box::new(move || {
                    // case we send response and don't want to go deeper
                    if !res.read().unwrap().is_stopped() {
                        mw(req.clone(), res.clone(), &mut next);
                    }
                })
            );
        }

        if let Some(mut chain) = final_handler {
            // case we send response and don't want to go deeper
            if !res.read().unwrap().is_stopped() {
                chain();
            }
        }
    }

    // Set Global Middleware
    pub fn use_middleware<F>(&self, middleware: F)
        where F: Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>, Next) + Send + Sync + 'static
    {
        let mut middlewares = self.middleware.write().unwrap();
        middlewares.push(Arc::new(middleware));
    }

    pub fn static_file(&mut self, path: &str) {
        self.static_path = Some(path.into());
    }

    /**
     * Start our server at specific port
     */
    pub fn listen(self: Arc<Self>, port: u16) {
        let listener = TcpListener::bind(("127.0.0.1", port)).unwrap();

        println!("\n---------------------\nServer running on port {}", port);

        // Listening incoming request
        for stream in listener.incoming() {
            // Filter out raw stream from inconging request
            let stream = match stream {
                Ok(s) => s,
                Err(e) => {
                    eprintln!("Connection failed: {}", e);
                    continue;
                }
            };
            // Clone of our Routes
            let routers_clone = {
                let guard = self.routes.read().unwrap();
                guard.clone()
            };
            // Clone of our Middleware
            let middleware_clone = {
                let guard = self.middleware.read().unwrap();
                guard.clone()
            };
            // static file not used
            let _static_file = self.static_path.clone();

            let this = self.clone();
            // Assign a Worker though warkerpool
            self.pool.execute(move || {
                // Current time for time takes to fullfill the request
                let now = Instant::now();
                // Shadowing make mutable
                let mut stream = stream;
                // TcpStream to buffer stream
                let mut reader = BufReader::new(&mut stream);
                // Request data Header and Body
                let mut lines = Vec::new();
                // Buffer stream store as Chunk of string
                let mut buffer = String::new();

                loop {
                    // Clean string
                    buffer.clear();
                    // Read line if it 0 then break the loop
                    if reader.read_line(&mut buffer).unwrap() == 0 {
                        break;
                    }
                    // Remove whitespaces from end
                    let line = buffer.trim_end().to_string();
                    // case line empty break the loop
                    if line.is_empty() {
                        break;
                    }
                    // Push into lines of vec
                    lines.push(line);
                }
                // Length of request content
                let content_length = lines
                    .iter()
                    .find(|line| line.to_ascii_lowercase().starts_with("content-length:"))
                    .and_then(|line| line.split(": ").nth(1))
                    .and_then(|len| len.parse::<usize>().ok());
                // Store body as Vec line
                let mut body_lines = Vec::new();
                // Case have length
                if let Some(len) = content_length {
                    // Make buffer to store full content
                    let mut buf = vec![0u8; len];
                    // Store data into buf
                    reader.read_exact(&mut buf).unwrap();
                    // Parse into UTF_8
                    let body = String::from_utf8_lossy(&buf).to_string();
                    // Concat it in body_lines
                    body_lines.extend(body.lines().map(|s| s.to_string()));
                }

                lines.push(String::new()); // Empty string before body
                lines.extend(body_lines);

                // Parse metadata into Request struct
                let req = Request::new(&lines);
                // Parse stream into Response struct
                let mut res_opt = Some(Arc::new(RwLock::new(Response::new(stream))));
                // Check is Route have or not
                let mut matched = false;
                // Iterate in Routes
                for route in routers_clone.into_iter() {
                    // Case method same
                    if route.method == req.method {
                        // Parse params
                        if let Some(params) = parse_path_params(&route.path, &req.path) {
                            // CLone req inside have params
                            let mut req_with_params = req.clone();
                            req_with_params.path_params = params;
                            let req_with_params = Arc::new(RwLock::new(req_with_params));

                            // Combined Global Middleware and Routes Middleware
                            let combined_middleware: Vec<_> = middleware_clone
                                .iter()
                                .chain(route.middleware.iter())
                                .cloned()
                                .collect();

                            if let Some(res_actual) = res_opt.take() {
                                // Move ownership
                                let req_for_handler = Arc::clone(&req_with_params);
                                let res_for_handler = Arc::clone(&res_actual);
                                // Call run_handler
                                this.run_handlers(
                                    Arc::clone(&req_for_handler),
                                    Arc::clone(&res_for_handler),
                                    &combined_middleware,
                                    {
                                        let req_inner = req_for_handler.clone();
                                        let res_inner = res_for_handler.clone();
                                        move || {
                                            (route.handler)(req_inner.clone(), res_inner.clone());
                                        }
                                    }
                                );

                                matched = true;
                                break;
                            }
                        }
                    }
                }
                // Duration to fullfill the request
                let duration = now.elapsed();
                // Case route not matched
                if !matched {
                    if let Some(res) = res_opt {
                        let mut res = res.write().unwrap();
                        res.status(404);
                        res.send("404 Not Found");
                    }
                    println!("\x1b[31m{} {}: {:?}\x1b[0m ", req.method, req.path, duration);
                } else {
                    println!("\x1b[32m{} {}: {:?}\x1b[0m ", req.method, req.path, duration);
                }
            });
        }
    }

    // ========== Get Method ============

    // Get routes without middleware
    pub fn get<F>(&self, path: &str, handler: F)
        where F: Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>) + Send + Sync + 'static
    {
        // Make empty middleware
        let empty_middleware: Vec<fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>, Next)> = vec![];

        self.get_with_middleware(path, empty_middleware, handler);
    }

    // Get routes with middleware
    pub fn get_with_middleware<F>(
        &self,
        path: &str,
        middleware: Vec<fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>, Next)>,
        handler: F
    )
        where F: Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>) + Send + Sync + 'static
    {
        // Warp all middleware with Arc
        let wrapped: Vec<Arc<Middleware>> = middleware
            .into_iter()
            .map(|m| Arc::new(m) as Arc<Middleware>)
            .collect();

        self.get_with_middleware_run(path, wrapped, handler);
    }

    // Responsible for runing both Get without middleware or with middleware
    fn get_with_middleware_run<F>(&self, path: &str, middleware: Vec<Arc<Middleware>>, handler: F)
        where F: Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>) + Send + Sync + 'static
    {
        // Sets route Metadata
        let route = Route {
            method: "GET".to_string(),
            path: path.to_string(),
            middleware,
            handler: Arc::new(handler),
        };
        // Push with Global routes
        self.routes.write().unwrap().push(route);
    }

    // ========== Post Method ============
    // Post routes without middleware
    pub fn post<F>(&self, path: &str, handler: F)
        where F: Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>) + Send + Sync + 'static
    {
        // Make empty middleware
        let empty_middleware: Vec<fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>, Next)> = vec![];

        self.post_with_middleware(path, empty_middleware, handler);
    }

    // Post routes with middleware
    pub fn post_with_middleware<F>(
        &self,
        path: &str,
        middleware: Vec<fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>, Next)>,
        handler: F
    )
        where F: Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>) + Send + Sync + 'static
    {
        // Warp all middleware with Arc
        let wrapped: Vec<Arc<Middleware>> = middleware
            .into_iter()
            .map(|m| Arc::new(m) as Arc<Middleware>)
            .collect();

        self.post_with_middleware_run(path, wrapped, handler);
    }

    // Responsible for running both Post without middleware or with middleware
    fn post_with_middleware_run<F>(&self, path: &str, middleware: Vec<Arc<Middleware>>, handler: F)
        where F: Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>) + Send + Sync + 'static
    {
        let route = Route {
            method: "POST".to_string(),
            path: path.to_string(),
            middleware,
            handler: Arc::new(handler),
        };
        self.routes.write().unwrap().push(route);
    }

    // ========== Put Method ============
    // Put routes without middleware
    pub fn put<F>(&self, path: &str, handler: F)
        where F: Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>) + Send + Sync + 'static
    {
        // Make empty middleware
        let empty_middleware: Vec<fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>, Next)> = vec![];

        self.put_with_middleware(path, empty_middleware, handler);
    }

    // Put routes with middleware
    pub fn put_with_middleware<F>(
        &self,
        path: &str,
        middleware: Vec<fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>, Next)>,
        handler: F
    )
        where F: Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>) + Send + Sync + 'static
    {
        // Warp all middleware with Arc
        let wrapped: Vec<Arc<Middleware>> = middleware
            .into_iter()
            .map(|m| Arc::new(m) as Arc<Middleware>)
            .collect();

        self.put_with_middleware_run(path, wrapped, handler);
    }

    // Responsible for running both Put without middleware or with middleware
    fn put_with_middleware_run<F>(&self, path: &str, middleware: Vec<Arc<Middleware>>, handler: F)
        where F: Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>) + Send + Sync + 'static
    {
        let route = Route {
            method: "PUT".to_string(),
            path: path.to_string(),
            middleware,
            handler: Arc::new(handler),
        };
        self.routes.write().unwrap().push(route);
    }

    // ========== Delete Method ============

    // Delete routes without middleware
    pub fn delete<F>(&self, path: &str, handler: F)
        where F: Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>) + Send + Sync + 'static
    {
        // Make empty middleware
        let empty_middleware: Vec<fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>, Next)> = vec![];

        self.delete_with_middleware(path, empty_middleware, handler);
    }

    // Delete routes with middleware
    pub fn delete_with_middleware<F>(
        &self,
        path: &str,
        middleware: Vec<fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>, Next)>,
        handler: F
    )
        where F: Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>) + Send + Sync + 'static
    {
        // Warp all middleware with Arc
        let wrapped: Vec<Arc<Middleware>> = middleware
            .into_iter()
            .map(|m| Arc::new(m) as Arc<Middleware>)
            .collect();

        self.delete_with_middleware_run(path, wrapped, handler);
    }

    // Responsible for running both Delete without middleware or with middleware
    fn delete_with_middleware_run<F>(
        &self,
        path: &str,
        middleware: Vec<Arc<Middleware>>,
        handler: F
    )
        where F: Fn(Arc<RwLock<Request>>, Arc<RwLock<Response>>) + Send + Sync + 'static
    {
        let route = Route {
            method: "DELETE".to_string(),
            path: path.to_string(),
            middleware,
            handler: Arc::new(handler),
        };
        self.routes.write().unwrap().push(route);
    }
}