This commit is contained in:
zoe 2022-01-20 13:18:23 +01:00
parent 295411d8fa
commit 32e72eb2e2
8 changed files with 178 additions and 44 deletions

View file

@ -12,15 +12,24 @@ use std::{
};
lazy_static! {
static ref CHAIN: Chain<String> = build_chain();
static ref FOOD_CHAIN: Chain<String> = build_chain("cooking.txt");
static ref FRAKES_CHAIN: Chain<String> = build_chain("frakes.txt");
static ref ARGS: Args = Args::parse();
}
#[get("/")]
fn index() -> Template {
let context: HashMap<&str, String> =
[("wisdom", CHAIN.generate_str())].iter().cloned().collect();
Template::render("index", context)
fn index() -> String {
"Hello".to_string()
}
#[get("/cooking")]
fn cooking() -> Template {
get_wisdom_template_from(FOOD_CHAIN.generate_str())
}
#[get("/frakes")]
fn frakes() -> Template {
get_wisdom_template_from(FRAKES_CHAIN.generate_str())
}
#[launch]
@ -29,14 +38,19 @@ fn rocket() -> _ {
config.port = ARGS.port;
rocket::custom(config)
.attach(Template::fairing())
.mount("/", routes![index])
.mount("/", routes![index, cooking, frakes])
}
fn build_chain() -> Chain<String> {
println!("Building Chain from \"{}\"...", ARGS.input);
fn get_wisdom_template_from(string: String) -> Template {
let context: HashMap<&str, String> = [("wisdom", string)].iter().cloned().collect();
Template::render("wisdom", context)
}
fn build_chain(input: &str) -> Chain<String> {
println!("Building Chain from \"{}\"...", input);
let mut new_chain = Chain::new();
// get input from input file
let file = File::open(&ARGS.input).expect("No input file!");
let file = File::open(input).expect("No input file!");
let reader = BufReader::new(file);
for line in reader.lines() {
new_chain.feed_str(&line.unwrap());
@ -47,10 +61,6 @@ fn build_chain() -> Chain<String> {
#[derive(Parser, Debug)]
#[clap(about, version, author)]
struct Args {
// Input file to make markov chain from
#[clap(short, long)]
input: String,
// Port number for server
#[clap(short, long, default_value_t = 8000)]
port: u16,