adapt majsite to serve cetacean.club
continuous-integration/drone/push Build is passing Details

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