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

100 lines
2.3 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: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:12:13 -04:00
use diesel::pg::PgConnection;
2020-10-14 17:28:09 -04:00
use diesel::prelude::*;
2020-10-14 17:12:13 -04:00
use diesel::r2d2::ConnectionManager;
use r2d2::{Pool, PooledConnection};
use rocket::{Outcome, Request, State};
use rocket::http::Status;
use rocket::request::{self, FromRequest};
2020-10-13 20:01:25 -04:00
use rocket_contrib::templates::Template;
2020-10-14 17:12:13 -04:00
use std::env;
use std::ops::Deref;
2020-10-14 17:42:48 -04:00
struct DbPool(Pool<ConnectionManager<PgConnection>>);
2020-10-14 17:12:13 -04:00
struct DbConn(PooledConnection<ConnectionManager<PgConnection>>);
impl<'a, 'r> FromRequest<'a, 'r> for DbConn {
type Error = ();
fn from_request(request: &'a Request<'r>) -> request::Outcome<DbConn, ()> {
let pool =
2020-10-14 17:42:48 -04:00
request.guard::<State<DbPool>>()?;
2020-10-14 17:12:13 -04:00
2020-10-14 17:42:48 -04:00
match pool.0.get() {
2020-10-14 17:12:13 -04:00
Ok(conn) => Outcome::Success(DbConn(conn)),
Err(_) => Outcome::Failure((Status::ServiceUnavailable, ())),
}
}
}
impl Deref for DbConn {
type Target = PgConnection;
fn deref(&self) -> &Self::Target {
&self.0
}
}
2020-10-13 20:01:25 -04:00
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:28:09 -04:00
.manage(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:42:48 -04:00
fn create_db_pool() -> DbPool {
2020-10-14 17:12:13 -04:00
let credentials = env::var("DATABASE_URL")
.expect("DATABASE_URL must be set");
let manager = ConnectionManager::<PgConnection>::new(credentials);
2020-10-14 17:42:48 -04:00
DbPool(Pool::new(manager).expect("Failed to create database pool"))
2020-10-14 17:12:13 -04:00
}
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:28:09 -04:00
fn index(db_conn: DbConn) -> Template {
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
}