mirror of
https://github.com/agersant/polaris
synced 2024-11-10 10:14:12 +00:00
Added API endpoint for initial setup info
This commit is contained in:
parent
a35c20fe06
commit
f90a98d231
2 changed files with 49 additions and 18 deletions
56
src/api.rs
56
src/api.rs
|
@ -30,20 +30,6 @@ use vfs::VFSSource;
|
|||
const CURRENT_MAJOR_VERSION: i32 = 2;
|
||||
const CURRENT_MINOR_VERSION: i32 = 1;
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Version {
|
||||
major: i32,
|
||||
minor: i32,
|
||||
}
|
||||
|
||||
impl Version {
|
||||
fn new(major: i32, minor: i32) -> Version {
|
||||
Version {
|
||||
major: major,
|
||||
minor: minor,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
struct Session {
|
||||
|
@ -89,10 +75,18 @@ fn get_endpoints(db: Arc<DB>) -> Mount {
|
|||
let mut api_handler = Mount::new();
|
||||
|
||||
{
|
||||
let db = db.clone();
|
||||
api_handler.mount("/version/", self::version);
|
||||
api_handler.mount("/auth/",
|
||||
move |request: &mut Request| self::auth(request, db.deref()));
|
||||
{
|
||||
let db = db.clone();
|
||||
api_handler.mount("/auth/",
|
||||
move |request: &mut Request| self::auth(request, db.deref()));
|
||||
}
|
||||
{
|
||||
let db = db.clone();
|
||||
api_handler.mount("/initial_setup/", move |request: &mut Request| {
|
||||
self::initial_setup(request, db.deref())
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
|
@ -207,13 +201,39 @@ impl Handler for AuthHandler {
|
|||
}
|
||||
|
||||
fn version(_: &mut Request) -> IronResult<Response> {
|
||||
let current_version = Version::new(CURRENT_MAJOR_VERSION, CURRENT_MINOR_VERSION);
|
||||
#[derive(Serialize)]
|
||||
struct Version {
|
||||
major: i32,
|
||||
minor: i32,
|
||||
}
|
||||
|
||||
let current_version = Version {
|
||||
major: CURRENT_MAJOR_VERSION,
|
||||
minor: CURRENT_MINOR_VERSION,
|
||||
};
|
||||
|
||||
match serde_json::to_string(¤t_version) {
|
||||
Ok(result_json) => Ok(Response::with((status::Ok, result_json))),
|
||||
Err(e) => Err(IronError::new(e, status::InternalServerError)),
|
||||
}
|
||||
}
|
||||
|
||||
fn initial_setup(_: &mut Request, db: &DB) -> IronResult<Response> {
|
||||
#[derive(Serialize)]
|
||||
struct InitialSetup {
|
||||
has_any_users: bool,
|
||||
};
|
||||
|
||||
let initial_setup = InitialSetup {
|
||||
has_any_users: user::count(db)? > 0,
|
||||
};
|
||||
|
||||
match serde_json::to_string(&initial_setup) {
|
||||
Ok(result_json) => Ok(Response::with((status::Ok, result_json))),
|
||||
Err(e) => Err(IronError::new(e, status::InternalServerError)),
|
||||
}
|
||||
}
|
||||
|
||||
fn auth(request: &mut Request, db: &DB) -> IronResult<Response> {
|
||||
let username;
|
||||
let password;
|
||||
|
|
11
src/user.rs
11
src/user.rs
|
@ -1,5 +1,6 @@
|
|||
use core::ops::Deref;
|
||||
use diesel;
|
||||
use diesel::expression;
|
||||
use diesel::prelude::*;
|
||||
use rand;
|
||||
use ring::{digest, pbkdf2};
|
||||
|
@ -69,3 +70,13 @@ pub fn auth<T>(db: &T, username: &str, password: &str) -> Result<bool>
|
|||
Err(e) => Err(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn count<T>(db: &T) -> Result<i64>
|
||||
where T: ConnectionSource
|
||||
{
|
||||
use db::users::dsl::*;
|
||||
let connection = db.get_connection();
|
||||
let connection = connection.lock().unwrap();
|
||||
let connection = connection.deref();
|
||||
Ok(users.select(expression::count(name)).first(connection)?)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue