mygit

[UNMAINTAINED] A cgit/webgit alternative, written in Rust
Log | Files | Refs | README | LICENSE

commit e22915c559b449c4ffb309ac230262e44ac67ebf
parent 386d6c8967c11231d7aed4d8a1dcb0e98f4638be
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Tue,  9 Mar 2021 20:31:05 -0800

Add basic configuration

Diffstat:
MCargo.lock | 2++
MCargo.toml | 2++
MREADME.md | 6+++---
Msrc/main.rs | 44++++++++++++++++++++++++++++++++++++++++++--
Asrc/templates.rs | 32++++++++++++++++++++++++++++++++
5 files changed, 81 insertions(+), 5 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock @@ -1625,11 +1625,13 @@ dependencies = [ "askama", "async-std", "git2", + "once_cell", "pico-args", "pulldown-cmark", "serde", "syntect", "tide", + "toml", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml @@ -15,3 +15,5 @@ tide = "0.16" syntect = {version="4.5", default-features = false, features=["html", "regex-onig"]} async-std = { version = "1.8.0", features = ["attributes"] } serde = { version = "1.0", features = ["derive"] } +once_cell = "1.7.2" +toml = "0.5.8" diff --git a/README.md b/README.md @@ -1,7 +1,7 @@ # Mygit -- the world's smallest Git host -tbd -## Why Rust? +https://public-inbox.org/README.html -There is IMO no compelling engineering reason to write this in Rust instead of Go. I'm doing so primarily as an exercise and as an opportunity to improve some of the Go ecosystem. +## Why Rust? +There is IMO no compelling engineering reason to write this in Rust instead of Go. I'm doing so primarily as an exercise and as an opportunity to improve some of the Rust ecosystem. diff --git a/src/main.rs b/src/main.rs @@ -1,17 +1,57 @@ use tide::Request; use tide::prelude::*; use std::time::Instant; +use once_cell::sync::OnceCell; +use std::fs; +use serde::{Serialize, Deserialize}; +use pico_args; -async fn hello(req: Request<()>) -> tide::Result<String> { +#[derive(Deserialize, Debug)] +pub struct Config { + port: i32 // should be u8 +} + +static CONFIG: OnceCell<Config> = OnceCell::new(); + +impl Config { + pub fn global() -> &'static Config { + CONFIG.get().expect("Config is not initialized") + } + +} + +async fn index(req: Request<()>) -> tide::Result<String> { let res = "Hello world!".to_owned(); Ok(res) } +const HELP: &str = "\ +mygit + +FLAGS: + -h, --help Prints help information +OPTIONS: + -c Path to config file +"; + #[async_std::main] async fn main() -> Result<(), std::io::Error> { + let mut pargs = pico_args::Arguments::from_env(); + + if pargs.contains(["-h", "--help"]) { + print!("{}", HELP); + std::process::exit(0); + } + + // TODO cli + + let toml_text = fs::read_to_string("mygit.toml")?; + let config: Config = toml::from_str(&toml_text)?; + CONFIG.set(config).unwrap(); + tide::log::start(); let mut app = tide::new(); - app.at("/").get(hello); + app.at("/").get(index); app.listen("127.0.0.1:8081").await?; Ok(()) } diff --git a/src/templates.rs b/src/templates.rs @@ -0,0 +1,32 @@ +pub use askama; +pub use tide; + +use askama::*; +use tide::{http::Mime, Body, Response}; + +pub fn try_into_body<T: Template>(t: &T, ext: &str) -> Result<Body> { + let string = t.render()?; + let mut body = Body::from_string(string); + + if let Some(mime) = Mime::from_extension(ext) { + body.set_mime(mime); + } + + Ok(body) +} + +pub fn into_response<T: Template>(t: &T, ext: &str) -> Response { + match try_into_body(t, ext) { + Ok(body) => { + let mut response = Response::new(200); + response.set_body(body); + response + } + + Err(error) => { + let mut response = Response::new(500); + response.set_error(error); + response + } + } +}