Download src/main.rs from arami21/rms-app: direct link, hf CLI and curl.
- Browser
- Download file 2.75 kB
-
https://huggingface.co/spaces/arami21/rms-app/resolve/main/src/main.rs
- Command line
-
hf download hf://spaces/arami21/rms-app/src/main.rs
-
curl -L -o main.rs https://huggingface.co/spaces/arami21/rms-app/resolve/main/src/main.rs
2.75 kB
| use actix_cors::Cors; | |
| use actix_web::{web, App, HttpResponse, HttpServer, HttpRequest}; | |
| use confik::{Configuration as _, EnvSource}; | |
| use dotenvy::dotenv; | |
| mod config; | |
| mod db; | |
| mod errors; | |
| mod models; | |
| use config::AppConfig; | |
| use db::Supabase; | |
| use errors::MyError; | |
| use models::RsvpRequest; | |
| const ALLOWED_ORIGIN: &[&str] = &[ | |
| "https://arami21-space.github.io", | |
| "http://localhost:5173", | |
| ]; | |
| async fn health() -> HttpResponse { | |
| HttpResponse::Ok().body("OK") | |
| } | |
| async fn get_guest( | |
| sb: web::Data<Supabase>, | |
| slug: web::Path<String>, | |
| ) -> Result<HttpResponse, MyError> { | |
| Ok(HttpResponse::Ok().json(sb.get_guest(&slug).await?)) | |
| } | |
| // VALIDASI ORIGIN DI SERVER | |
| fn check_origin(req: &HttpRequest) -> bool { | |
| if let Some(origin) = req.headers().get("Origin") { | |
| if let Ok(origin_str) = origin.to_str() { | |
| // Cek apakah origin ada di ALLOWED_ORIGIN | |
| return ALLOWED_ORIGIN.contains(&origin_str); | |
| } | |
| } | |
| false | |
| } | |
| async fn submit_rsvp( | |
| req: HttpRequest, | |
| sb: web::Data<Supabase>, | |
| form: web::Json<RsvpRequest>, | |
| ) -> Result<HttpResponse, MyError> { | |
| if !check_origin(&req) { | |
| return Ok(HttpResponse::Forbidden().body("Origin tidak diizinkan")); | |
| } | |
| Ok(HttpResponse::Ok().json(sb.submit_rsvp(form.into_inner()).await?)) | |
| } | |
| async fn get_messages( | |
| req: HttpRequest, | |
| sb: web::Data<Supabase>, | |
| ) -> Result<HttpResponse, MyError> { | |
| if !check_origin(&req) { | |
| return Ok(HttpResponse::Forbidden().body("Origin tidak diizinkan")); | |
| } | |
| Ok(HttpResponse::Ok().json(sb.get_messages().await?)) | |
| } | |
| async fn main() -> std::io::Result<()> { | |
| dotenv().ok(); | |
| let cfg = AppConfig::builder() | |
| .override_with(EnvSource::new()) | |
| .try_build() | |
| .expect("Invalid configuration"); | |
| let supabase = Supabase::new(cfg.supabase_url, cfg.supabase_anon_key); | |
| HttpServer::new(move || { | |
| let cors = Cors::default() | |
| .allowed_origin_fn(|origin, _req_head| { | |
| // origin: &HeaderValue | |
| origin.to_str().map(|s| ALLOWED_ORIGIN.contains(&s)).unwrap_or(false) | |
| }) | |
| .allowed_methods(vec!["HEAD","GET","POST"]) | |
| .allowed_headers(vec!["Content-Type", "Authorization"]) | |
| .max_age(3600); | |
| App::new() | |
| .wrap(cors) | |
| .app_data(web::Data::new(supabase.clone())) | |
| .route("/cek", web::get().to(health)) | |
| .route("/health", web::head().to(health)) | |
| .route("/guests/{slug}", web::get().to(get_guest)) | |
| .route("/rsvp", web::post().to(submit_rsvp)) | |
| .route("/messages", web::get().to(get_messages)) | |
| }) | |
| .bind(cfg.server_addr)? | |
| .run() | |
| .await | |
| } | |