add config for max views, expiration and advanced

This commit is contained in:
2022-03-01 15:24:17 +01:00
parent ef39f9ec0b
commit d112eba8fe
9 changed files with 220 additions and 133 deletions

View File

@@ -1,25 +1,12 @@
use actix_web::{get, web, HttpResponse, Responder};
use serde::{Deserialize, Serialize};
use actix_web::web;
use crate::note;
use crate::size::LIMIT;
#[derive(Serialize, Deserialize)]
struct Status {
version: String,
max_size: usize,
}
#[get("/status")]
async fn status() -> impl Responder {
return HttpResponse::Ok().json(Status {
version: option_env!("CARGO_PKG_VERSION")
.unwrap_or("Unknown")
.to_string(),
max_size: *LIMIT,
});
}
use crate::status;
pub fn init(cfg: &mut web::ServiceConfig) {
cfg.service(web::scope("/api").service(note::service()).service(status));
cfg.service(
web::scope("/api")
.service(note::init())
.service(status::init()),
);
}

23
backend/src/config.rs Normal file
View File

@@ -0,0 +1,23 @@
use byte_unit::Byte;
lazy_static! {
pub static ref VERSION: String = option_env!("CARGO_PKG_VERSION")
.unwrap_or("Unknown")
.to_string();
pub static ref LIMIT: usize =
Byte::from_str(std::env::var("SIZE_LIMIT").unwrap_or("1 KiB".to_string()))
.unwrap()
.get_bytes() as usize;
pub static ref MAX_VIEWS: usize = std::env::var("MAX_VIEWS")
.unwrap_or("100".to_string())
.parse()
.unwrap();
pub static ref MAX_EXPIRATION: usize = std::env::var("MAX_EXPIRATION")
.unwrap_or("360".to_string()) // 6 hours in minutes
.parse()
.unwrap();
pub static ref ALLOW_ADVANCED: bool = std::env::var("ALLOW_ADVANCED")
.unwrap_or("true".to_string())
.parse()
.unwrap();
}

View File

@@ -6,8 +6,10 @@ extern crate lazy_static;
mod api;
mod client;
mod config;
mod note;
mod size;
mod status;
mod store;
#[actix_web::main]

View File

@@ -110,7 +110,7 @@ struct Status {
max_size: usize,
}
pub fn service() -> Scope {
pub fn init() -> Scope {
web::scope("/notes")
.service(one)
.service(create)

View File

@@ -0,0 +1,5 @@
mod model;
mod routes;
pub use model::*;
pub use routes::*;

View File

@@ -0,0 +1,10 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct Status {
pub version: String,
pub max_size: usize,
pub max_views: usize,
pub max_expiration: usize,
pub allow_advanced: bool,
}

View File

@@ -0,0 +1,19 @@
use actix_web::{get, web, HttpResponse, Responder, Scope};
use crate::config;
use crate::status::Status;
#[get("/")]
async fn get_status() -> impl Responder {
return HttpResponse::Ok().json(Status {
version: config::VERSION.to_string(),
max_size: *config::LIMIT,
max_views: *config::MAX_VIEWS,
max_expiration: *config::MAX_EXPIRATION,
allow_advanced: *config::ALLOW_ADVANCED,
});
}
pub fn init() -> Scope {
web::scope("/status").service(get_status)
}