2018-04-22 13:35:37 +00:00
|
|
|
use rocket::request::Form;
|
2018-04-24 09:21:39 +00:00
|
|
|
use rocket::response::Redirect;
|
|
|
|
use rocket_contrib::Template;
|
2018-04-22 13:35:37 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2018-05-02 11:53:42 +00:00
|
|
|
use BASE_URL;
|
2018-04-22 13:35:37 +00:00
|
|
|
use db_conn::DbConn;
|
|
|
|
use models::instance::*;
|
|
|
|
|
2018-04-29 17:50:46 +00:00
|
|
|
#[get("/")]
|
2018-05-09 19:09:52 +00:00
|
|
|
fn index(conn: DbConn) -> Template {
|
2018-04-29 17:50:46 +00:00
|
|
|
match Instance::get_local(&*conn) {
|
|
|
|
Some(inst) => {
|
2018-05-09 19:09:52 +00:00
|
|
|
Template::render("instance/index", json!({
|
|
|
|
"instance": inst
|
|
|
|
}))
|
2018-04-29 17:50:46 +00:00
|
|
|
}
|
|
|
|
None => {
|
2018-05-09 19:09:52 +00:00
|
|
|
Template::render("errors/500", json!({
|
|
|
|
"error_message": "You need to configure your instance before using it."
|
|
|
|
}))
|
2018-04-29 17:50:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-22 13:35:37 +00: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,
|
2018-05-02 11:53:42 +00:00
|
|
|
BASE_URL.as_str().to_string(),
|
2018-04-22 13:35:37 +00:00
|
|
|
form.name.to_string(),
|
|
|
|
true);
|
|
|
|
if inst.has_admin(&*conn) {
|
|
|
|
Redirect::to("/")
|
|
|
|
} else {
|
|
|
|
Redirect::to("/users/new")
|
|
|
|
}
|
|
|
|
}
|