1
0
Fork 0
This repository has been archived on 2023-03-27. You can view files and clone it, but cannot push or open issues or pull requests.
fedihub-website/src/main.rs

58 lines
1.2 KiB
Rust
Raw Normal View History

2020-10-14 16:36:24 -04:00
#![feature(decl_macro, proc_macro_hygiene)]
2020-10-13 19:40:03 -04:00
2020-10-14 17:47:16 -04:00
mod database;
2020-10-14 17:28:09 -04:00
mod schema;
#[macro_use] extern crate diesel;
2020-10-13 19:40:03 -04:00
#[macro_use] extern crate rocket;
2020-10-13 20:13:00 -04:00
#[macro_use] extern crate serde_derive;
2020-10-14 16:27:21 -04:00
2020-10-14 17:12:13 -04:00
extern crate dotenv;
2020-10-13 20:13:00 -04:00
extern crate rocket_contrib;
2020-10-13 19:40:03 -04:00
2020-10-14 17:28:09 -04:00
use diesel::prelude::*;
2020-10-13 20:01:25 -04:00
use rocket_contrib::templates::Template;
2020-10-13 20:13:00 -04:00
#[derive(Serialize)]
struct TemplateContext {
parent: &'static str,
2020-10-14 17:28:09 -04:00
users: Vec<String>,
}
#[derive(Debug, Queryable)]
struct User {
pub id: i32,
pub username: String,
2020-10-13 20:13:00 -04:00
}
2020-10-13 20:26:03 -04:00
fn main() {
2020-10-14 17:12:13 -04:00
dotenv::dotenv().ok();
2020-10-13 20:27:00 -04:00
rocket().launch();
}
fn rocket() -> rocket::Rocket {
2020-10-13 20:26:03 -04:00
rocket::ignite()
2020-10-14 17:47:16 -04:00
.manage(database::create_db_pool())
2020-10-13 20:26:03 -04:00
.attach(Template::fairing())
2020-10-13 20:29:50 -04:00
.mount("/", routes())
}
2020-10-14 17:28:09 -04:00
fn routes() -> Vec<rocket::Route> {
routes![index]
}
2020-10-13 19:40:03 -04:00
#[get("/")]
2020-10-14 17:47:16 -04:00
fn index(db_conn: database::DbConn) -> Template {
2020-10-14 17:28:09 -04:00
use schema::users::dsl::*;
let all_users = users.load::<User>(&*db_conn).expect("Error loading users");
let all_user_names = all_users.iter().map(|user| user.username.to_string()).collect();
2020-10-13 20:13:00 -04:00
let template_context = TemplateContext {
parent: "layout",
2020-10-14 17:28:09 -04:00
users: all_user_names,
2020-10-13 20:13:00 -04:00
};
Template::render("index", &template_context)
2020-10-13 19:40:03 -04:00
}