File size: 2,746 Bytes
66dfbca 674f578 c132d47 674f578 889cc2f 66dfbca bf3a830 7283635 f51e055 674f578 c132d47 bf3a830 66dfbca bf3a830 7283635 bf3a830 66dfbca 674f578 738b98d 674f578 738b98d bf3a830 674f578 bf3a830 889cc2f 7283635 674f578 bf3a830 66dfbca 3016e51 674f578 3016e51 674f578 bf3a830 66dfbca bf3a830 66dfbca df559cf 1a922b2 bf3a830 66dfbca 1e9fd2b c93d8f2 3016e51 66dfbca f16e764 bf3a830 27d52a0 bf3a830 66dfbca bf3a830 66dfbca bf3a830 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | 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?))
}
#[actix_web::main]
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
}
|