Skip to content

Commit

Permalink
feat: changed build on mine to be release not debug and added CONTENT…
Browse files Browse the repository at this point in the history
…_BASE_DIR env var
  • Loading branch information
Sampiiiii committed May 26, 2024
1 parent 70729c9 commit f708873
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
3 changes: 2 additions & 1 deletion apps/mine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"license": "GPL-3.0-or-later",
"private": true,
"scripts": {
"build": "cargo build",
"build": "cargo build --release",
"build:dev": "cargo build",
"format": "rustfmt src/*.rs --edition=2021",
"start": "cargo run",
"dev": "OP_ACCOUNT=iforge.1password.com op run --env-file=../../config/mine/.env.development.tpl -- cargo watch -x run",
Expand Down
58 changes: 31 additions & 27 deletions apps/mine/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,31 @@ use actix_web::{
use jsonwebtoken::{get_current_timestamp, DecodingKey, Validation};
use resvg::{tiny_skia, usvg, usvg::TreeParsing};
use serde::{Deserialize, Serialize};
use std::{ffi::OsStr, fs, path::Path};
use std::{ffi::OsStr, fs, path::{Path, PathBuf}};
use uuid::Uuid;

lazy_static::lazy_static! {
static ref JWT_SECRET: String =
std::env::var("JWT_SECRET").unwrap();
static ref KEY: DecodingKey = DecodingKey::from_secret(JWT_SECRET.as_bytes());

static ref ALLOWED_TO_UPLOAD: Vec<String> =
std::env::var("ALLOWED_TO_UPLOAD")
.unwrap()
.split(',')
.map(str::to_string)
.collect();

static ref MINE_PORT: u16 =
std::env::var("MINE_PORT")
.unwrap_or_else(|_| "4000".to_string())
.parse()
.expect("MINE_PORT must be a valid number");

static ref CONTENT_BASE_DIR: PathBuf =
PathBuf::from(std::env::var("CONTENT_BASE_DIR").unwrap_or_else(|_| "./".to_string()));
}

fn convert_svg(content: String, format: &OsStr) -> actix_web::Result<impl Responder> {
let extension = format.to_str().expect("format wasn't valid utf-8");
if extension == "svg" {
Expand Down Expand Up @@ -57,9 +79,9 @@ fn convert_svg(content: String, format: &OsStr) -> actix_web::Result<impl Respon
#[actix_web::get("icons/{name}")] // TODO params for size
async fn icons(name: web::Path<String>) -> actix_web::Result<impl Responder> {
let filename = Path::new(name.as_str());
let svg = Path::new("icons").join(filename.with_extension("svg"));
let svg = CONTENT_BASE_DIR.join("icons").join(filename.with_extension("svg"));

let read = match fs::read_to_string(svg) {
let read = match fs::read_to_string(&svg) {
Ok(content) => content,
_ => return Err(error::ErrorNotFound(format!("file {filename:?} not found"))),
};
Expand All @@ -82,25 +104,6 @@ struct Claims {
roles: Vec<String>,
}

lazy_static::lazy_static! {
static ref JWT_SECRET: String =
std::env::var("JWT_SECRET").unwrap();
static ref KEY: DecodingKey = DecodingKey::from_secret(JWT_SECRET.as_bytes());

static ref ALLOWED_TO_UPLOAD: Vec<String> =
std::env::var("ALLOWED_TO_UPLOAD")
.unwrap()
.split(',')
.map(str::to_string)
.collect();

static ref MINE_PORT: u16 =
std::env::var("MINE_PORT")
.unwrap_or_else(|_| "4000".to_string())
.parse()
.expect("MINE_PORT must be a valid number");
}

#[actix_web::post("/upload")]
async fn files_upload(
MultipartForm(multipart): MultipartForm<FileUpload>,
Expand Down Expand Up @@ -137,18 +140,19 @@ async fn files_upload(

#[actix_web::main]
async fn main() -> std::io::Result<()> {
// TODO if not present build caches images
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));

log::info!("starting HTTP server at http://localhost:{}", *MINE_PORT);
log::info!("Allowed to upload: {:?}", *ALLOWED_TO_UPLOAD);
HttpServer::new(|| {
log::info!("Content base directory: {:?}", *CONTENT_BASE_DIR);

HttpServer::new(move || {
App::new()
.service(Files::new("/files", "./files/"))
.service(Files::new("/fonts", "./fonts/").show_files_listing())
.service(Files::new("/files", CONTENT_BASE_DIR.join("files")))
.service(Files::new("/fonts", CONTENT_BASE_DIR.join("fonts")).show_files_listing())
.service(files_upload)
.service(icons)
.service(Files::new("/logos", "./logos/").show_files_listing())
.service(Files::new("/logos", CONTENT_BASE_DIR.join("logos")).show_files_listing())
// .service(logos)
.wrap(Logger::default())
.wrap(Cors::permissive())
Expand Down

0 comments on commit f708873

Please sign in to comment.