~lthms/ogmios

ref: a8bfc771ff9a575ccea360f4d8e3ba389be9e848 ogmios/src/main.rs -rw-r--r-- 3.4 KiB View raw
a8bfc771Thomas Letan refactor: Rely on the newly introduced StaticFiles 1 year, 8 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// This needs to be removed after diesel-1.4
#![allow(proc_macro_derive_resolution_fallback)]

// for the error_chain crate
#![recursion_limit = "1024"]

// for the rocket and maud crates
#![feature(proc_macro_hygiene, decl_macro, custom_derive)]

// for dealing with error_chain boilerplate
#![feature(type_ascription)]

#[macro_use] extern crate diesel;
#[macro_use] extern crate error_chain;
#[macro_use] extern crate serde_derive;
extern crate serde;
extern crate serde_json;
extern crate pretty_env_logger;
#[macro_use] extern crate log;
#[macro_use] extern crate rocket;
#[macro_use] extern crate rocket_contrib;
extern crate ogmarkup;
extern crate base64;

use diesel::prelude::*;

use rocket::request::FlashMessage;
use rocket::response::Content;
use rocket::http::{ContentType, MediaType};
use rocket_contrib::templates::Template;
use rocket_contrib::serve::StaticFiles;

pub mod errors;
pub mod db;
pub mod models;
pub mod schema;
pub mod routes;

use db::PgConn;

use ::schema::users;
use ::errors::{Error, ResultExt};
use ::routes::shared;
use ::models::user::User;
use ::models::image::{ImageId, Image};

#[get("/", rank=1)]
fn index_auth(
    conn: PgConn,
    msg: Option<FlashMessage>,
    user: User,
) -> Result<Template, Error> {

    let nb = users::table
        .count()
        .get_result::<i64>(conn.get())
        .chain_err(|| "Could not query users table")?;

    shared::render_auth_template(
        user,
        msg,
        "index",
        json!({
            "nb": nb,
        }),
        &conn
    )
}

#[get("/", rank=2)]
fn index_noauth(
    conn: PgConn,
    msg: Option<FlashMessage>
) -> Result<Template, Error> {
    let nb = users::table
        .count()
        .get_result::<i64>(conn.get())
        .chain_err(|| "Could not query users table")?;

    shared::render_noauth_template(
        msg,
        "index",
        json!({
            "nb": nb,
        }),
        &conn
    )
}

#[get("/img/<id>")]
fn images(
    id: i32,
    conn: PgConn,
) -> Result<Content<Vec<u8>>, Error> {
    let img = Image::get(ImageId::from(id), &conn)?;

    Ok(Content(ContentType(<MediaType>::PNG), img.content))
}

fn run() -> Result <(), Error> {
    rocket::ignite()
        .mount("/", routes![
            index_auth,
            index_noauth,
            images,
            routes::session::login_noauth,
            routes::session::logout,
            routes::session::select_character_auth,
            routes::document::submit_document,
            routes::document::render_document,
            routes::account::account_index,
            routes::characters::get_current_sheet,
            routes::characters::get_future_sheet,
            routes::characters::validate_future_sheet,
            routes::characters::new_future_sheet,
            routes::characters::edit_future_sheet,
            routes::characters::edit_future_sheet_post,
        ])
        .mount("/static", StaticFiles::from("static"))
        .attach(PgConn::fairing())
        .attach(Template::fairing())
        .launch();

    bail!("We could not properly start the web server")
}

fn main() {
    pretty_env_logger::init();

    if let Err(ref e) = run() {
        error!("error: {}", e);

        for e in e.iter().skip(1) {
            error!("caused by: {}", e);
        }

        if let Some(backtrace) = e.backtrace() {
            error!("backtrace: {:?}", backtrace);
        }

        ::std::process::exit(1);
    }
}