update authorization middleware with actix-web-macros

This commit is contained in:
Quentin de Quelen
2020-04-22 17:43:51 +02:00
committed by qdequele
parent e74d2c1872
commit bc8ff49de3
13 changed files with 323 additions and 196 deletions

View File

@ -1,13 +1,20 @@
use crate::error::ResponseError;
use crate::Data;
use actix_web::{get, put, web, HttpResponse};
use actix_web::{web, HttpResponse};
use actix_web_macros::{get, put};
use heed::types::{Str, Unit};
use serde::Deserialize;
use crate::error::ResponseError;
use crate::helpers::Authentication;
use crate::Data;
const UNHEALTHY_KEY: &str = "_is_unhealthy";
#[get("/health")]
pub async fn get_health(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(get_health).service(change_healthyness);
}
#[get("/health", wrap = "Authentication::Private")]
async fn get_health(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
let reader = data.db.main_read_txn()?;
let common_store = data.db.common_store();
@ -19,7 +26,7 @@ pub async fn get_health(data: web::Data<Data>) -> Result<HttpResponse, ResponseE
Ok(HttpResponse::Ok().finish())
}
pub async fn set_healthy(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
async fn set_healthy(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
let mut writer = data.db.main_write_txn()?;
let common_store = data.db.common_store();
common_store.delete::<_, Str>(&mut writer, UNHEALTHY_KEY)?;
@ -28,7 +35,7 @@ pub async fn set_healthy(data: web::Data<Data>) -> Result<HttpResponse, Response
Ok(HttpResponse::Ok().finish())
}
pub async fn set_unhealthy(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
async fn set_unhealthy(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> {
let mut writer = data.db.main_write_txn()?;
let common_store = data.db.common_store();
common_store.put::<_, Str, Unit>(&mut writer, UNHEALTHY_KEY, &())?;
@ -38,12 +45,12 @@ pub async fn set_unhealthy(data: web::Data<Data>) -> Result<HttpResponse, Respon
}
#[derive(Deserialize, Clone)]
pub struct HealtBody {
struct HealtBody {
health: bool,
}
#[put("/health")]
pub async fn change_healthyness(
#[put("/health", wrap = "Authentication::Private")]
async fn change_healthyness(
data: web::Data<Data>,
body: web::Json<HealtBody>,
) -> Result<HttpResponse, ResponseError> {