~jplatte/turbo.fish

ref: 69f909f0f8a30fb3c748fecac68ae9fa139e4bdd turbo.fish/src/main.rs -rw-r--r-- 1.8 KiB View raw
69f909f0 — Jonas Platte Implement random_reverse 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#![feature(proc_macro_hygiene, decl_macro)]

mod random;
mod reverse_turbofish;
mod turbofish;

use std::{
    collections::HashMap,
    path::{Path, PathBuf},
};

use rocket::{
    get,
    response::{NamedFile, Redirect},
    routes, uri,
};
use rocket_contrib::templates::Template;

use self::{random::random_type, reverse_turbofish::ReverseTurboFish, turbofish::TurboFish};

#[get("/")]
fn index() -> Template {
    let mut context = HashMap::new();
    context.insert("guts", "");

    Template::render("turbofish", context)
}

#[get("/random")]
fn random() -> Redirect {
    Redirect::to(uri!(turbofish: TurboFish::new(random_type())))
}

#[get("/random_reverse")]
fn random_reverse() -> Redirect {
    Redirect::to(uri!(reverse_turbofish: ReverseTurboFish::new(random_type())))
}

#[get("/<turbofish>", rank = 1)]
fn turbofish(turbofish: TurboFish) -> Template {
    let mut context = HashMap::new();
    context.insert("guts", turbofish.gut());

    Template::render("turbofish", context)
}

#[get("/<reverse_turbofish>", rank = 2)]
fn reverse_turbofish(reverse_turbofish: ReverseTurboFish) -> Template {
    let mut context = HashMap::new();
    context.insert("guts", reverse_turbofish.gut());

    Template::render("reverse_turbofish", context)
}

// From https://github.com/SergioBenitez/Rocket/blob/master/examples/static_files/src/main.rs
#[get("/<file..>", rank = 100)]
fn files(file: PathBuf) -> Option<NamedFile> {
    NamedFile::open(Path::new("static/").join(file)).ok()
}

fn main() {
    // TODO: Custom 404
    rocket::ignite()
        .mount(
            "/",
            routes![
                index,
                random,
                random_reverse,
                turbofish,
                reverse_turbofish,
                files
            ],
        )
        .attach(Template::fairing())
        .launch();
}