aboutsummaryrefslogtreecommitdiff
path: root/main.ha
diff options
context:
space:
mode:
Diffstat (limited to 'main.ha')
-rw-r--r--main.ha59
1 files changed, 31 insertions, 28 deletions
diff --git a/main.ha b/main.ha
index 4359c46..d133c7b 100644
--- a/main.ha
+++ b/main.ha
@@ -2,71 +2,74 @@
// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
// Adapted from template by Willow Barraco <contact@willowbarraco.fr>
+use fs;
use getopt;
use log;
use net;
-use net::ip;
-use net::http;
use net::dial;
+use net::http;
+use net::ip;
+use net::tcp;
+use net::uri;
use os;
use memio;
use io;
use fmt;
use bufio;
-use strings;
+use unix::signal;
const usage: [_]getopt::help = [
- "HTTP server",
- ('a', "address", "listened address (ex: 127.0.0.1:8080)")
+ "Lindenii Forge Server",
+ ('c', "config", "path to configuration file")
];
+let static_fs: nullable *fs::fs = null;
+
+let running: bool = true;
+
+export fn sigint_handler(sig: signal::sig, info: *signal::siginfo, ucontext: *opaque) void = {
+ running = false;
+};
+
export fn main() void = {
+ signal::handle(signal::sig::INT, &sigint_handler, signal::flag::NONE, null);
+
const cmd = getopt::parse(os::args, usage...);
defer getopt::finish(&cmd);
let port: u16 = 8080;
let ip_addr: ip::addr4 = [127, 0, 0, 1];
- for (let i = 0z; i < len(cmd.opts); i += 1) {
- const opt = cmd.opts[i];
+ for (let opt .. cmd.opts) {
switch (opt.0) {
- case 'a' =>
- match (dial::splitaddr(opt.1, "")) {
- case let value: (str, u16) =>
- ip_addr = ip::parsev4(value.0)!;
- port = value.1;
- case dial::invalid_address =>
- abort("Invalid address");
- };
- case => abort(); // unreachable
+ case 'c' => yield; // TODO: actually handle the config
+ case => abort("unreachable");
};
};
- const server = match (http::listen(ip_addr, port)) {
+ static_fs = os::diropen("static")!;
+ defer fs::close(static_fs as *fs::fs);
+
+ const server = match (http::listen(ip_addr, port, net::tcp::reuseport, net::tcp::reuseaddr)) {
case let this: *http::server =>
yield this;
- case net::error => abort("failure while listening");
+ case => abort("failure while listening");
};
defer http::server_finish(server);
- for (true) {
+ for (running) {
const serv_req = match (http::serve(server)) {
case let this: *http::server_request =>
yield this;
- case net::error => abort("failure while serving");
+ case =>
+ log::println("failure while serving");
+ continue;
};
defer http::serve_finish(serv_req);
match (handlereq(serv_req.socket, &serv_req.request)) {
case void => yield;
- case io::error => log::println("error while handling request");
+ case => log::println("error while handling request");
};
};
};
-
-export fn handlereq(conn: io::handle, request: *http::request) (void | io::error) = {
- fmt::fprint(conn, "HTTP/1.1 200 OK\r\n")?;
- fmt::fprint(conn, "Content-Type: text/plain\r\n\r\n")?;
-
- fmt::fprintln(conn, "Hi there!")?;
-};