Skip to main content

web_admin/
main.rs

1/*
2 * This file is part of strat9-os.
3 *
4 * strat9-os is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * strat9-os is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with strat9-os. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * Website: https://www.strat9-os.org
18 * Repository: https://github.com/strat9-os/strat9-os
19 *
20 * Copyright (C) 2023-26 Guillaume Gielly
21 */
22
23#![no_std]
24#![no_main]
25#![feature(alloc_error_handler)]
26#![recursion_limit = "512"]
27
28extern crate alloc;
29
30mod executor;
31mod io;
32mod net;
33mod routes;
34mod sysinfo;
35
36use core::{alloc::Layout, fmt::Write, panic::PanicInfo};
37use strat9_syscall::call;
38
39const LISTEN_PORT: u16 = 8080;
40const HTTP_BUF_SIZE: usize = 8192;
41
42alloc_freelist::define_freelist_brk_allocator!(
43    pub struct WebAdminAllocator;
44    brk = strat9_syscall::call::brk;
45    heap_max = 4 * 1024 * 1024;
46);
47
48#[global_allocator]
49static ALLOCATOR: WebAdminAllocator = WebAdminAllocator;
50
51#[alloc_error_handler]
52/// Implements alloc error.
53fn alloc_error(layout: Layout) -> ! {
54    let mut buf = [0u8; 80];
55    let n = {
56        let mut w = BufWriter::new(&mut buf);
57        let _ = write!(
58            w,
59            "[web-admin] OOM: {} bytes align {}\n",
60            layout.size(),
61            layout.align()
62        );
63        w.len()
64    };
65    let _ = call::debug_log(&buf[..n]);
66    call::exit(12)
67}
68
69#[panic_handler]
70fn panic(info: &PanicInfo) -> ! {
71    call::handle_panic("web-admin", info);
72}
73
74struct BufWriter<'a> {
75    buf: &'a mut [u8],
76    pos: usize,
77}
78
79impl<'a> BufWriter<'a> {
80    /// Creates a new instance.
81    fn new(buf: &'a mut [u8]) -> Self {
82        Self { buf, pos: 0 }
83    }
84    /// Implements len.
85    fn len(&self) -> usize {
86        self.pos
87    }
88}
89
90impl core::fmt::Write for BufWriter<'_> {
91    /// Writes str.
92    fn write_str(&mut self, s: &str) -> core::fmt::Result {
93        let bytes = s.as_bytes();
94        let avail = self.buf.len().saturating_sub(self.pos);
95        let n = bytes.len().min(avail);
96        self.buf[self.pos..self.pos + n].copy_from_slice(&bytes[..n]);
97        self.pos += n;
98        Ok(())
99    }
100}
101
102/// Implements log.
103pub fn log(msg: &str) {
104    let _ = call::debug_log(msg.as_bytes());
105}
106
107static CONFIG: picoserve::Config =
108    picoserve::Config::const_default().close_connection_after_response();
109
110#[unsafe(no_mangle)]
111/// Implements start.
112pub extern "C" fn _start() -> ! {
113    log("[web-admin] Strat9 Web Admin starting on port 8080\n");
114
115    let app = routes::build_router();
116    let mut conn_count: u64 = 0;
117
118    loop {
119        let fd = net::open_listener(LISTEN_PORT);
120        conn_count += 1;
121
122        executor::block_on(async {
123            let socket = io::TcpSocket::new(fd);
124            let timer = io::Strat9Timer;
125            let mut http_buf = [0u8; HTTP_BUF_SIZE];
126
127            let server = picoserve::Server::custom(&app, timer, &CONFIG, &mut http_buf);
128
129            match server.serve(socket).await {
130                Ok(info) => {
131                    let msg = alloc::format!(
132                        "[web-admin] conn #{}: {} reqs\n",
133                        conn_count,
134                        info.handled_requests_count
135                    );
136                    log(&msg);
137                }
138                Err(_) => {
139                    log("[web-admin] connection error\n");
140                }
141            }
142        });
143    }
144}