Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ WEBHOOK_PINGS="<@&role_id> <@user_id>"
ADMIN_PASSWORD=supersecret

# used for kept video access
PUBLIC_URL=https://vertd.your-domain.here
PUBLIC_URL=https://vertd.your-domain.here

# CORS origins setup
# Can either be "*" or comma separated origins: "https://origin1.com,https://origin2.com"
# If not defined, fall back automatically to *
CORS_ORIGINS=*
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ services:
- WEBHOOK_PINGS=${WEBHOOK_PINGS}
- ADMIN_PASSWORD=${ADMIN_PASSWORD}
- PUBLIC_URL=${PUBLIC_URL}
- CORS_ORIGINS=${CORS_ORIGINS:-*}
ports:
- "${PORT:-24153}:24153"

Expand Down
72 changes: 63 additions & 9 deletions src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,78 @@ use crate::http::services::keep::keep;
mod response;
mod services;

#[derive(Clone)]
enum CorsConfig {
Any,
Specific(Vec<String>),
}

fn parse_cors(origins_raw: &str) -> CorsConfig {
let raw = origins_raw.trim();

if raw.is_empty() || raw == "*" {
return CorsConfig::Any;
}

let origins = raw
.split(',')
.map(str::trim)
.filter(|s| !s.is_empty())
.map(String::from)
.collect::<Vec<_>>();

CorsConfig::Specific(origins)
}

fn build_cors(config: &CorsConfig) -> Cors {
match config {
CorsConfig::Any => Cors::default()
.allow_any_origin()
.allow_any_method()
.allow_any_header(),

CorsConfig::Specific(origins) => {
let mut cors = Cors::default().allow_any_method().allow_any_header();

for origin in origins {
cors = cors.allowed_origin(origin);
}

cors
}
}
}

pub async fn start_http() -> anyhow::Result<()> {
let server = HttpServer::new(|| {
App::new()
.wrap(
Cors::default()
.allow_any_origin()
.allow_any_method()
.allow_any_header(),
)
.service(
let cors_origins = std::env::var("CORS_ORIGINS").unwrap_or_else(|_| "*".to_string());
let cors_config = parse_cors(&cors_origins);

match &cors_config {
CorsConfig::Any => info!("CORS: allow any origin (*)"),
CorsConfig::Specific(origins) => {
info!("CORS: allowed origins:");
for origin in origins {
info!(" - {}", origin);
}
}
}

let server = HttpServer::new({
let cors_config = cors_config.clone();
move || {
let cors = build_cors(&cors_config);

App::new().wrap(cors).service(
web::scope("/api")
.service(upload)
.service(download)
.service(websocket)
.service(version)
.service(keep),
)
}
});

let port = std::env::var("PORT").unwrap_or_else(|_| "24153".to_string());
if !port.chars().all(char::is_numeric) {
anyhow::bail!("PORT must be a number");
Expand Down