initial commit

This commit is contained in:
emilis 2022-09-10 19:54:50 +01:00
commit 1a3c6f2209
7 changed files with 1381 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

1175
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

14
Cargo.toml Normal file
View File

@ -0,0 +1,14 @@
[package]
name = "flabk"
version = "0.0.1"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.64"
handlebars = "4.3.3"
serde = { version = "1.0.144", features = ["derive", "std", "serde_derive"]}
serde_json = "1.0.85"
tokio = { version = "1", features = ["full"] }
warp = { version = "0.3.2" }

37
src/main.rs Normal file
View File

@ -0,0 +1,37 @@
mod servek;
use serde::{Deserialize, Serialize};
use servek::Server;
#[derive(Clone, Copy, Serialize, Deserialize)]
pub enum ActivityKind {
Create,
Like,
Note,
}
#[derive(Clone, Serialize, Deserialize)]
pub struct ActivityLD {
pub context_uri: String,
pub kind: ActivityKind,
pub actor_uri: String,
pub to_uris: Vec<String>,
pub object: Option<ObjectLD>,
}
#[derive(Clone, Serialize, Deserialize)]
pub struct ObjectLD {
pub context_uri: String,
pub id: Option<String>,
pub kind: Option<ActivityKind>,
pub attributed_to: Option<String>,
pub published: Option<String>,
pub content: Option<String>,
}
#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
Server::new().listen_and_serve(8008).await;
Ok(())
}

116
src/servek.rs Normal file
View File

@ -0,0 +1,116 @@
use core::panic;
use std::{convert::Infallible, fmt::Display};
use handlebars::{Handlebars, RenderError};
use serde::Serialize;
use warp::{hyper::StatusCode, reject::Reject, Filter, Rejection, Reply};
#[derive(Debug, Clone)]
pub struct Server {
hb: Handlebars<'static>,
profiler: Profiler,
}
#[derive(Debug, Clone)]
struct Profiler;
impl Profiler {
fn profile(&self, username: String) -> Result<Profile, Infallible> {
Ok(Profile { username })
}
}
impl Server {
pub fn new() -> Self {
let mut hb = Handlebars::new();
hb.register_template_string("profile", include_str!("../templates/html/profile.html"))
.expect("profile template");
let profiler = Profiler;
Self { hb, profiler }
}
pub async fn listen_and_serve(self, port: u16) -> ! {
println!("starting server on port {}", port);
warp::serve(self.html().recover(Self::handle_rejection))
.run(([127, 0, 0, 1], port))
.await;
panic!("server stopped prematurely")
}
fn html(&self) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
Self::index().or(self.profile())
}
fn index() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
warp::get().and(warp::path::end().map(move || {
warp::reply::html(include_str!("../templates/html/index.html").to_owned())
}))
}
fn with_server(
srv: Server,
) -> impl Filter<Extract = (Server,), Error = std::convert::Infallible> + Clone {
warp::any().map(move || srv.clone())
}
fn profile(&self) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
warp::get().and(
warp::path!("@" / String)
.and(Self::with_server(self.clone()))
.and_then(|username: String, srv: Server| async move {
match srv.hb.render(
"profile",
&serde_json::json!(srv.profiler.profile(username)?),
) {
Ok(html) => Ok(warp::reply::html(html)),
Err(err) => Err(InternalError::reject(err.to_string())),
}
}),
)
}
async fn handle_rejection(err: Rejection) -> Result<impl Reply, Infallible> {
if let Some(internal) = err.find::<InternalError>() {
println!("internal error: {}", internal);
return Ok(warp::reply::with_status(
"internal server error",
StatusCode::INTERNAL_SERVER_ERROR,
));
}
panic!()
}
}
#[derive(Clone, Debug)]
struct InternalError {
inner: String,
}
impl InternalError {
fn reject(err: String) -> Rejection {
warp::reject::custom(Self { inner: err })
}
}
impl Reject for InternalError {}
impl From<RenderError> for InternalError {
fn from(r: RenderError) -> Self {
Self {
inner: r.to_string(),
}
}
}
impl Display for InternalError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.inner)
}
}
#[derive(Serialize)]
struct Profile {
username: String,
}

19
templates/html/index.html Normal file
View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<title>flabk</title>
<style>
body {
background-color: black;
color: rebeccapurple;
}
</style>
</head>
<body>
<h1>hi</h1>
</body>
</html>

View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<title>@{{username}}</title>
<style>
body {
background-color: black;
color: rebeccapurple;
}
</style>
</head>
<body>
<h1>hi {{username}}</h1>
</body>
</html>