adapt majsite to serve cetacean.club

This commit is contained in:
Cadey Ratio 2020-08-01 16:47:34 -04:00
parent 0ffd86c9f6
commit 4b7374b39f
4 changed files with 47 additions and 101 deletions

View File

@ -15,5 +15,6 @@ env_logger = "0"
percent-encoding = "2" percent-encoding = "2"
rustls = { version = "0.18", features = ["dangerous_configuration"] } rustls = { version = "0.18", features = ["dangerous_configuration"] }
structopt = "0.3" structopt = "0.3"
dnd_dice_roller = "0.3"
maj = { path = "..", features = ["server"], default-features = false } maj = { path = "..", features = ["server"], default-features = false }

View File

@ -1,21 +0,0 @@
# maj
```
__
_____ _____ |__|
/ \ \__ \ | |
| Y Y \ / __ \_ | |
|__|_| /(____ //\__| |
\/ \/ \______|
```
A gemini ecosystem for Rust
=> gemini://gemini.circumlunar.space/ Gemini homepage
## Homepage
The main homepage for maj is on tulpa.dev:
=> https://tulpa.dev/cadey/maj
## Projects
=> /majc majc, an interactive curses client
=> /majd majd, a high-power gemini server with Lua capabilities

View File

@ -1,4 +1,5 @@
use async_std::task; use async_std::task;
use dnd_dice_roller::{dice::Dice, error::DiceError};
use maj::{ use maj::{
gemini::{Builder, Node}, gemini::{Builder, Node},
route, seg, route, seg,
@ -10,10 +11,13 @@ use rustls::{
internal::pemfile::{certs, rsa_private_keys}, internal::pemfile::{certs, rsa_private_keys},
AllowAnyAnonymousOrAuthenticatedClient, Certificate, PrivateKey, RootCertStore, ServerConfig, AllowAnyAnonymousOrAuthenticatedClient, Certificate, PrivateKey, RootCertStore, ServerConfig,
}; };
use std::fs::File; use std::{
use std::io::{self, BufReader}; fs::File,
use std::path::{Path, PathBuf}; io::{self, BufReader},
use std::sync::Arc; path::{Path, PathBuf},
str::FromStr,
sync::Arc,
};
use structopt::StructOpt; use structopt::StructOpt;
#[derive(StructOpt, Debug)] #[derive(StructOpt, Debug)]
@ -42,7 +46,7 @@ struct Options {
#[structopt( #[structopt(
long = "hostname", long = "hostname",
env = "SERVER_HOSTNAME", env = "SERVER_HOSTNAME",
default_value = "maj.kahless.cetacean.club" default_value = "cetacean.club"
)] )]
hostname: String, hostname: String,
} }
@ -95,63 +99,53 @@ struct Handler {
files: maj::server::files::Handler, files: maj::server::files::Handler,
} }
async fn index() -> Result<maj::Response, maj::server::Error> { async fn dice(req: Request) -> Result<Response, Error> {
let msg = include_bytes!("index.gmi"); fn dice_roll<T: Into<String>>(roll: T) -> Result<String, DiceError> {
Ok(Response::gemini(msg.to_vec())) let mut dice = Dice::from_str(&roll.into())?;
if dice.number_of_dice_to_roll > 100 {
dice.number_of_dice_to_roll = 100;
} }
async fn majc() -> Result<maj::Response, maj::server::Error> { if dice.sides > 100 {
let msg = include_bytes!("majc.gmi"); dice.sides = 100
Ok(Response::gemini(msg.to_vec()))
} }
async fn need_cert(req: Request) -> Result<Response, Error> { if dice.sides == 0 {
match req.certs { dice.sides = 6;
None => Ok(Response::need_cert("test")),
Some(certs) => Ok(Response::render(
Builder::new()
.heading(1, "Cert test")
.text(format!("{:?}", certs))
.build(),
)),
}
} }
async fn input(req: Request) -> Result<Response, Error> { let res = dice.roll_dice();
match req.url.query() { let reply = format!(
None => Ok(Response::input("test")), "{}{} = {}\n",
Some(q) => Ok({ res.dice_results,
use Node::*; match dice.modifier {
let result = vec![ Some(amt) => format!(" + {}", amt),
Heading { None => "".into(),
level: 1,
body: "Input test".to_string(),
}, },
Node::blank(), res.final_result[0]
Text("You gave me:".to_string()), );
Preformatted(format!("{}", percent_decode_str(q).decode_utf8()?)), Ok(reply)
]; }
Response::render(result) match req.url.query() {
None => Ok(Response::input(
"What do you want to roll? [n]dn[+n] [adv|dadv]",
)),
Some(q) => Ok({
let dice = percent_decode_str(q).decode_utf8()?;
let b = Builder::new()
.heading(1, "Dice Results")
.text("")
.text(format!("You rolled {} and you got:", dice))
.text("")
.preformatted(format!("{}", dice_roll(dice)?));
Response::render(b.build())
}), }),
} }
} }
async fn user(name: String) -> Result<Response, Error> {
Ok(Response::render({
use Node::*;
vec![
Heading {
level: 1,
body: format!("{}'s user page", name),
},
Node::blank(),
Text(format!("this is a test page for {}", name)),
]
}))
}
#[async_trait::async_trait] #[async_trait::async_trait]
impl MajHandler for Handler { impl MajHandler for Handler {
async fn handle(&self, req: Request) -> Result<Response, Error> { async fn handle(&self, req: Request) -> Result<Response, Error> {
@ -167,11 +161,7 @@ impl MajHandler for Handler {
} }
route!(req.url.path(), { route!(req.url.path(), {
(/) => index().await; (/"dice") => dice(req).await;
(/"cert") => need_cert(req).await;
(/"input") => input(req).await;
(/"majc") => majc().await;
(/"~"/[name: String][/rest..]) => user(name).await;
}); });
self.files.handle(req).await self.files.handle(req).await

View File

@ -1,24 +0,0 @@
```
__
_____ _____ |__| ____
/ \ \__ \ | |_/ ___\
| Y Y \ / __ \_ | |\ \___
|__|_| /(____ //\__| | \___ >
\/ \/ \______| \/
```
A curses client for Gemini!
## Homepage
The main homepage for majc is on tulpa.dev:
=> https://tulpa.dev/cadey/maj
## Important Keys
<esc>: opens the menubar
c: closes the active window
o: prompts to open a URL
h: shows history
l: shows active links in the page
q: quits majc
?: shows this screen
~: toggles the debug logging pane