Lindenii Project Forge
Login

server

Lindenii Forge’s main backend daemon
Commit info
ID
cb2517bee240c592c07e77c1507877cf47ace553
Author
Runxi Yu <me@runxiyu.org>
Author date
Sat, 15 Mar 2025 01:16:39 +0800
Committer
Runxi Yu <me@runxiyu.org>
Committer date
Sat, 15 Mar 2025 01:16:39 +0800
Actions
Separate paths into segments
forge: main.ha templates.ha
forge: templates.ha *.ha
	hare build -o $@ .

templates.ha: templates/*.htmpl
	htmplgen -o $@ $^
// SPDX-License-Identifier: AGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
// Adapted from template by Willow Barraco <contact@willowbarraco.fr>

use getopt;
use htmpl;
use log;
use net;
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;

const usage: [_]getopt::help = [
	"Lindenii Forge Server",
	('c', "config", "path to configuration file")
];

export fn main() void = {
	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 opt .. cmd.opts) {
		switch (opt.0) {
		case 'c' => yield; // TODO: actually handle the config
		case => abort(); // unreachable
		};
	};

	const server = match (http::listen(ip_addr, port, net::tcp::reuseport, net::tcp::reuseaddr)) {
	case let this: *http::server =>
		yield this;
	case => abort("failure while listening");
	};
	defer http::server_finish(server);

	for (true) {
		const serv_req = match (http::serve(server)) {
		case let this: *http::server_request =>
			yield this;
		case net::error => abort("failure while serving");
		};
		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");
		};
	};
};

export fn handlereq(conn: io::handle, request: *http::request) (void | io::error | nomem) = {
export fn handlereq(conn: io::handle, request: *http::request) (void | io::error | nomem | net::uri::invalid) = {
	htmpl::write(conn, "HTTP/1.1 200 OK\r\n")?;
	htmpl::write(conn, "Content-Type: text/html\r\n\r\n")?;
	tp_index(conn)?;
	let segments = segments_from_path(request.target.raw_path)?;
	defer free_segments(segments);
	tp_index(conn, segments)?;
};
{{ define tp_index(handle: io::handle) (void | io::error | nomem) }}
{{ define tp_index(handle: io::handle, segments: []str) (void | io::error | nomem) }}
<!DOCTYPE html>
<html lang="en">
<head>
{{ render _tp_head_common(handle) }}
<title>Index &ndash; {{ global.title }}</title>
</head>
<body>
{{ render _tp_header(handle, "test", "test") }}
<h2>Path segments</h2>
<ul>
	{{ for let s .. segments }}
	<li>{{ s }}</li>
	{{ end }}
</ul>
<div class="padding-wrapper">
<table class="wide rounded">
	<thead>
		<tr>
			<th colspan="2" class="title-row">Groups</th>
		</tr>
		<tr>
			<th scope="col">Name</th>
			<th scope="col">Description</th>
		</tr>
	</thead>
	<tbody>
	</tbody>
</table>
<div class="padding-wrapper">
	<table class="wide rounded">
		<thead>
			<tr>
				<th colspan="2" class="title-row">
					Info
				</th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<th scope="row">SSH public key</th>
				<td><code>{{ global.ssh_pubkey }}</code></td>
			</tr>
			<tr>
				<th scope="row">SSH fingerprint</th>
				<td><code>{{ global.ssh_fp }}</code></td>
			</tr>
		</tbody>
	</table>
</div>
<footer>
	{{ render _tp_footer(handle) }}
</footer>
</body>
</html>
{{ end }}
use strings;
use net::uri;

fn segments_from_path(s: str) ([]str | nomem | net::uri::invalid) = {
	let sp: []str = strings::split(s, "/")?;
	for (let i = 1z; i < len(sp); i += 1)
		sp[i - 1] = net::uri::percent_decode(sp[i])?;
	return sp[.. len(sp) - 1];
};

fn free_segments(ss: []str) void = {
	for (let s .. ss) {
		free(s);
	};
};