Plume/src/routes/instance.rs

50 lines
1.2 KiB
Rust
Raw Normal View History

use rocket::request::Form;
2018-04-24 11:21:39 +02:00
use rocket::response::Redirect;
use rocket_contrib::Template;
use std::collections::HashMap;
use BASE_URL;
use db_conn::DbConn;
use models::instance::*;
2018-04-29 19:50:46 +02:00
#[get("/")]
2018-05-09 21:09:52 +02:00
fn index(conn: DbConn) -> Template {
2018-04-29 19:50:46 +02:00
match Instance::get_local(&*conn) {
Some(inst) => {
2018-05-09 21:09:52 +02:00
Template::render("instance/index", json!({
"instance": inst
}))
2018-04-29 19:50:46 +02:00
}
None => {
2018-05-09 21:09:52 +02:00
Template::render("errors/500", json!({
"error_message": "You need to configure your instance before using it."
}))
2018-04-29 19:50:46 +02:00
}
}
}
#[get("/configure")]
fn configure() -> Template {
Template::render("instance/configure", HashMap::<String, i32>::new())
}
#[derive(FromForm)]
struct NewInstanceForm {
name: String
}
#[post("/configure", data = "<data>")]
fn post_config(conn: DbConn, data: Form<NewInstanceForm>) -> Redirect {
let form = data.get();
let inst = Instance::insert(
&*conn,
BASE_URL.as_str().to_string(),
form.name.to_string(),
true);
if inst.has_admin(&*conn) {
Redirect::to("/")
} else {
Redirect::to("/users/new")
}
}