ferret/frontend/src/main.rs

65 lines
1.7 KiB
Rust

use axum::{
body::Bytes,
extract::State,
http::{header, HeaderMap, StatusCode},
response::{Html, IntoResponse},
routing::{get, post},
Json, Router,
};
use chrono::{DateTime, NaiveDateTime, Utc};
use ramhorns::{Content, Template};
use std::fs::File;
use std::io::prelude::*;
use std::net::SocketAddr;
use url::Url;
#[derive(Content)]
struct HomePage {
title: String,
description: String,
source: String,
version: String,
}
#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
let app = Router::new()
.route("/", get(root))
.route("/style.css", get(stylesheet));
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
tracing::debug!("listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}
async fn stylesheet() -> impl IntoResponse {
let mut headers = HeaderMap::new();
headers.insert(header::CONTENT_TYPE, "text/css".parse().unwrap());
let mut file = File::open("static/style.css").unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
(headers, contents)
}
async fn root() -> Html<String> {
let mut source = File::open("static/home.html").unwrap();
let mut contents = String::new();
source.read_to_string(&mut contents).unwrap();
let tpl = Template::new(contents).unwrap();
let rendered = tpl.render(&HomePage {
title: "Ferret".to_string(),
description: "A small independent search engine".to_string(),
source: "https://git.lavender.software/erin/ferret".to_string(),
version: "v0.2.0".to_string(),
});
Html(rendered)
}