Skip to content

Commit

Permalink
Merge pull request #25 from lajp/filename-fix
Browse files Browse the repository at this point in the history
fix: allow a wider range of filenames
  • Loading branch information
lajp authored Aug 24, 2024
2 parents 07bf0d0 + 879c464 commit baf1088
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 36 deletions.
108 changes: 108 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ axum-valid = { version = "0.14.0", features = [
"typed_multipart",
], default-features = false }
axum_typed_multipart = "0.11.0"
clap = { version = "4.5.16", features = ["env", "derive"] }
comemo = { version = "0.4.0" }
dotenv = "0.15.0"
fontdb = { version = "0.17.0", optional = true }
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ RUN touch src/main.rs
RUN cargo build --release

FROM alpine as runtime

Check warning on line 22 in Dockerfile

View workflow job for this annotation

GitHub Actions / Push Docker image to GitHub Packages (laskugeneraattori)

The 'as' keyword should match the case of the 'from' keyword

FromAsCasing: 'as' and 'FROM' keywords' casing do not match More info: https://docs.docker.com/go/dockerfile/rule/from-as-casing/
ENV BIND_ADDR 0.0.0.0

Check warning on line 23 in Dockerfile

View workflow job for this annotation

GitHub Actions / Push Docker image to GitHub Packages (laskugeneraattori)

Legacy key/value format with whitespace separator should not be used

LegacyKeyValueFormat: "ENV key=value" should be used instead of legacy "ENV key value" format More info: https://docs.docker.com/go/dockerfile/rule/legacy-key-value-format/
WORKDIR /app
COPY --from=builder /app/target/release/laskugeneraattori app
EXPOSE 5237
Expand Down
13 changes: 8 additions & 5 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use axum::{
extract::DefaultBodyLimit,
http::Method,
http::{HeaderValue, Method},
routing::{get, post},
Router,
};
Expand All @@ -12,10 +12,13 @@ use tower_http::{cors::CorsLayer, limit::RequestBodyLimitLayer, trace::TraceLaye
pub mod invoices;

pub fn app() -> Router<crate::state::State> {
let cors_layer = CorsLayer::new().allow_origin([
"https://tietokilta.fi".parse().unwrap(),
"http://localhost:3000".parse().unwrap(),
]);
let cors_layer = CorsLayer::new().allow_origin(
crate::CONFIG
.allowed_origins
.iter()
.map(|c| c.parse::<HeaderValue>().unwrap())
.collect::<Vec<_>>(),
);

let governor_config = Arc::new(
GovernorConfigBuilder::default()
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub enum Error {
MultipartRejection(#[from] axum::extract::multipart::MultipartRejection),
#[error("Missing filename multipart")]
MissingFilename,
#[error("Unsupported file format: {0}")]
#[error("Unsupported file format: {0}. Supported file formats are (jpg|jpeg|png|gif|svg|pdf)")]
UnsupportedFileFormat(String),
#[error("Error in handling json value")]
JsonRejection(#[from] axum::extract::rejection::JsonRejection),
Expand Down
27 changes: 20 additions & 7 deletions src/mailgun/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,27 @@ use axum::{

mod invoices;

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct MailgunClient {
pub client: reqwest::Client,
pub url: String,
pub api_user: String,
pub api_key: String,
pub default_to: String,
pub from: String,
client: reqwest::Client,
url: String,
api_user: String,
api_key: String,
default_to: String,
from: String,
}

impl From<crate::MailgunConfig> for MailgunClient {
fn from(config: crate::MailgunConfig) -> Self {
Self {
client: reqwest::Client::new(),
url: config.url,
api_user: config.user,
api_key: config.password,
default_to: config.to,
from: config.from,
}
}
}

#[async_trait]
Expand Down
53 changes: 40 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};

use clap::Parser;
use std::net::SocketAddr;
use std::sync::LazyLock;

mod api;
mod error;
Expand All @@ -16,6 +18,43 @@ mod tests;
#[macro_use]
extern crate tracing;

#[derive(Parser, Clone, Debug)]
struct MailgunConfig {
/// Url used by mailgun
#[clap(long = "mailgun-url", env = "MAILGUN_URL")]
url: String,
/// Username used by mailgun
#[clap(long = "mailgun-user", env = "MAILGUN_USER")]
user: String,
/// Password used by mailgun
#[clap(long = "mailgun-password", env = "MAILGUN_PASSWORD")]
password: String,
/// Initial To-value used by mailgun
#[clap(long = "mailgun-to", env = "MAILGUN_TO")]
to: String,
/// From-value used by mailgun
#[clap(long = "mailgun-from", env = "MAILGUN_FROM")]
from: String,
}

#[derive(Parser, Clone, Debug)]
#[command(version, about, long_about = None)]
struct LaskugenConfig {
#[clap(flatten)]
mailgun: MailgunConfig,
/// The listen port for the HTTP server
#[clap(long, env, required = false, default_value = "3000")]
port: u16,
/// The ip address to bound by the HTTP server
#[clap(long, env, required = false, default_value = "127.0.0.1")]
bind_addr: std::net::IpAddr,
/// A comma-separated list of allowed origins
#[clap(long, env, required = false, value_delimiter = ',')]
allowed_origins: Vec<String>,
}

static CONFIG: LazyLock<LaskugenConfig> = LazyLock::new(LaskugenConfig::parse);

#[tokio::main]
async fn main() {
dotenv::dotenv().ok();
Expand All @@ -27,19 +66,7 @@ async fn main() {
.init();

let state = state::new().await;

let ip = if std::env::var("EXPOSE").unwrap_or("0".into()) == "1" {
[0, 0, 0, 0]
} else {
[127, 0, 0, 1]
};

let addr = SocketAddr::from((
ip,
std::env::var("PORT")
.map(|p| p.parse::<u16>().unwrap())
.unwrap_or(3000),
));
let addr = SocketAddr::from((CONFIG.bind_addr, CONFIG.port));
debug!("Listening on {addr}");
let listener = tokio::net::TcpListener::bind(addr)
.await
Expand Down
11 changes: 1 addition & 10 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,8 @@ pub struct State {
pub async fn new() -> State {
dotenv::dotenv().ok();

let mailgun_client = MailgunClient {
client: reqwest::Client::new(),
url: std::env::var("MAILGUN_URL").expect("No MAILGUN_URL in env"),
api_user: std::env::var("MAILGUN_USER").expect("No MAILGUN_USER in env"),
api_key: std::env::var("MAILGUN_PASSWORD").expect("No MAILGUN_PASSWORD in env"),
default_to: std::env::var("MAILGUN_TO").expect("No MAILGUN_TO in env"),
from: std::env::var("MAILGUN_FROM").expect("No MAILGUN_FROM in env"),
};

State {
mailgun_client,
mailgun_client: MailgunClient::from(crate::CONFIG.mailgun.clone()),
for_garde: (),
}
}

0 comments on commit baf1088

Please sign in to comment.