Skip to content

Commit

Permalink
feat: manage configurations through config.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobLinCool committed May 16, 2023
1 parent 788edfc commit f811713
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
26 changes: 26 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use std::env;

pub fn max_cost() -> u64 {
env::var("MAX_COST")
.unwrap_or("1000000000".to_owned())
.parse::<u64>()
.unwrap_or(1000000000)
}

pub fn max_memory() -> u32 {
env::var("MAX_MEMORY")
.unwrap_or("4096".to_owned())
.parse::<u32>()
.unwrap_or(4096)
}

pub fn server_port() -> u16 {
env::var("PORT")
.unwrap_or("33000".to_owned())
.parse::<u16>()
.unwrap_or(33000)
}

pub fn app_secret() -> String {
env::var("APP_SECRET").unwrap_or("APP_SECRET".to_owned())
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod cli;
mod config;
mod cost;
mod memory;
mod read;
Expand Down
6 changes: 2 additions & 4 deletions src/server/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use rocket::Build;
use rocket::Config;
use rocket::Rocket;

use crate::config::*;
use crate::server::execute;
use crate::server::jwt;

Expand Down Expand Up @@ -37,10 +38,7 @@ pub fn rocket() -> Rocket<Build> {
rocket::build()
.configure(Config {
address: Ipv4Addr::new(0, 0, 0, 0).into(),
port: std::env::var("PORT")
.unwrap_or_else(|_| "33000".to_string())
.parse()
.unwrap(),
port: server_port(),
..Config::default()
})
.mount("/", routes![index, info, jwt::validate, execute::execute])
Expand Down
5 changes: 3 additions & 2 deletions src/server/execute.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::config::*;
use crate::run;
use crate::server::jwt;
use base64::{engine::general_purpose, Engine as _};
Expand Down Expand Up @@ -50,7 +51,7 @@ pub async fn execute(
}
};

if submission.cost > 1000000000 {
if submission.cost > max_cost() {
return Json(ExecutionResult {
success: false,
cost: None,
Expand All @@ -61,7 +62,7 @@ pub async fn execute(
});
}

if submission.memory > 1024 {
if submission.memory > max_memory() {
return Json(ExecutionResult {
success: false,
cost: None,
Expand Down
8 changes: 3 additions & 5 deletions src/server/jwt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};
use rocket::request::{self, FromRequest, Request};
use rocket::serde::json::Json;
use rocket::serde::{Deserialize, Serialize};
use std::env;

use crate::config::*;

#[derive(Debug, Serialize, Deserialize)]
#[serde(crate = "rocket::serde")]
Expand All @@ -11,10 +12,7 @@ struct Claims {
}

pub fn is_valid_token(token: &str) -> bool {
let secret = match env::var("JWT_SECRET") {
Ok(secret) => secret,
Err(_) => "SECRET_TOKEN".to_string(),
};
let secret = app_secret();

let validation = Validation::new(Algorithm::HS256);

Expand Down

0 comments on commit f811713

Please sign in to comment.