Update src/main.rs
Browse files- src/main.rs +27 -81
src/main.rs
CHANGED
|
@@ -1,115 +1,61 @@
|
|
| 1 |
use actix_cors::Cors;
|
| 2 |
-
use actix_web::{App, HttpResponse, HttpServer
|
| 3 |
-
use config::AppConfig;
|
| 4 |
use confik::{Configuration as _, EnvSource};
|
| 5 |
-
use deadpool_postgres::{Pool, Runtime};
|
| 6 |
use dotenvy::dotenv;
|
| 7 |
-
use std::thread;
|
| 8 |
-
|
| 9 |
-
// Import untuk RustTLS
|
| 10 |
-
use rustls::ClientConfig;
|
| 11 |
-
use rustls_native_certs::load_native_certs;
|
| 12 |
-
use tokio_postgres_rustls::MakeRustlsConnect;
|
| 13 |
|
| 14 |
mod config;
|
| 15 |
mod db;
|
| 16 |
mod errors;
|
| 17 |
mod models;
|
| 18 |
|
| 19 |
-
use
|
| 20 |
-
use
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
let client = pool.get().await.map_err(MyError::PoolError)?;
|
| 24 |
-
let stmt = client.prepare("SELECT 1").await.map_err(MyError::PGError)?;
|
| 25 |
-
client
|
| 26 |
-
.query_one(&stmt, &[])
|
| 27 |
-
.await
|
| 28 |
-
.map_err(MyError::PGError)?;
|
| 29 |
|
| 30 |
-
|
|
|
|
| 31 |
}
|
| 32 |
|
| 33 |
-
async fn
|
| 34 |
-
|
| 35 |
-
|
| 36 |
) -> Result<HttpResponse, MyError> {
|
| 37 |
-
|
| 38 |
-
let slug = path.into_inner();
|
| 39 |
-
let guest = db::get_guest(&client, &slug).await?;
|
| 40 |
-
Ok(HttpResponse::Ok().json(guest))
|
| 41 |
}
|
| 42 |
|
| 43 |
-
async fn
|
| 44 |
-
|
| 45 |
form: web::Json<RsvpRequest>,
|
| 46 |
) -> Result<HttpResponse, MyError> {
|
| 47 |
-
|
| 48 |
-
let updated_guest = db::submit_rsvp(&client, form.into_inner()).await?;
|
| 49 |
-
Ok(HttpResponse::Ok().json(updated_guest))
|
| 50 |
}
|
| 51 |
|
| 52 |
-
async fn
|
| 53 |
-
|
| 54 |
-
let messages = db::get_messages(&client).await?;
|
| 55 |
-
Ok(HttpResponse::Ok().json(messages))
|
| 56 |
}
|
| 57 |
|
| 58 |
#[actix_web::main]
|
| 59 |
async fn main() -> std::io::Result<()> {
|
| 60 |
-
// ⭐ TAMBAHKAN INI: Install default crypto provider
|
| 61 |
-
rustls::crypto::ring::default_provider()
|
| 62 |
-
.install_default()
|
| 63 |
-
.expect("Failed to install rustls crypto provider");
|
| 64 |
-
|
| 65 |
dotenv().ok();
|
| 66 |
|
| 67 |
-
let
|
| 68 |
.override_with(EnvSource::new())
|
| 69 |
.try_build()
|
| 70 |
-
.
|
| 71 |
-
|
| 72 |
-
// Setup RustTLS
|
| 73 |
-
let mut root_store = rustls::RootCertStore::empty();
|
| 74 |
-
|
| 75 |
-
// Load system certificates
|
| 76 |
-
for cert in load_native_certs().expect("could not load platform certs") {
|
| 77 |
-
root_store.add(cert).ok();
|
| 78 |
-
}
|
| 79 |
|
| 80 |
-
let
|
| 81 |
-
.with_root_certificates(root_store)
|
| 82 |
-
.with_no_client_auth();
|
| 83 |
-
|
| 84 |
-
let tls = MakeRustlsConnect::new(tls_config);
|
| 85 |
-
|
| 86 |
-
let pool = config
|
| 87 |
-
.pg
|
| 88 |
-
.create_pool(Some(Runtime::Tokio1), tls)
|
| 89 |
-
.expect("Failed to create pool");
|
| 90 |
-
|
| 91 |
-
let available_cores = thread::available_parallelism().unwrap().get();
|
| 92 |
-
println!("--------------------------------------------------");
|
| 93 |
-
println!("🖥️ CPU Cores Terdeteksi : {}", available_cores);
|
| 94 |
-
println!("👷 Actix Workers (Threads): {}", available_cores);
|
| 95 |
-
println!("🚀 Server berjalan di : http://{}", config.server_addr);
|
| 96 |
-
println!("--------------------------------------------------");
|
| 97 |
|
| 98 |
HttpServer::new(move || {
|
| 99 |
-
let cors = Cors::default()
|
| 100 |
-
.allow_any_origin()
|
| 101 |
-
.allow_any_method()
|
| 102 |
-
.allow_any_header();
|
| 103 |
-
|
| 104 |
App::new()
|
| 105 |
-
.wrap(
|
| 106 |
-
.app_data(web::Data::new(
|
| 107 |
-
.route("/health", web::get().to(
|
| 108 |
-
.route("/guests/{slug}", web::get().to(
|
| 109 |
-
.route("/rsvp", web::post().to(
|
| 110 |
-
.route("/messages", web::get().to(
|
| 111 |
})
|
| 112 |
-
.bind(
|
| 113 |
.run()
|
| 114 |
.await
|
| 115 |
-
}
|
|
|
|
| 1 |
use actix_cors::Cors;
|
| 2 |
+
use actix_web::{web, App, HttpResponse, HttpServer};
|
|
|
|
| 3 |
use confik::{Configuration as _, EnvSource};
|
|
|
|
| 4 |
use dotenvy::dotenv;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
mod config;
|
| 7 |
mod db;
|
| 8 |
mod errors;
|
| 9 |
mod models;
|
| 10 |
|
| 11 |
+
use config::AppConfig;
|
| 12 |
+
use db::Supabase;
|
| 13 |
+
use errors::MyError;
|
| 14 |
+
use models::RsvpRequest;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
async fn health() -> HttpResponse {
|
| 17 |
+
HttpResponse::Ok().body("OK")
|
| 18 |
}
|
| 19 |
|
| 20 |
+
async fn get_guest(
|
| 21 |
+
sb: web::Data<Supabase>,
|
| 22 |
+
slug: web::Path<String>,
|
| 23 |
) -> Result<HttpResponse, MyError> {
|
| 24 |
+
Ok(HttpResponse::Ok().json(sb.get_guest(&slug).await?))
|
|
|
|
|
|
|
|
|
|
| 25 |
}
|
| 26 |
|
| 27 |
+
async fn submit_rsvp(
|
| 28 |
+
sb: web::Data<Supabase>,
|
| 29 |
form: web::Json<RsvpRequest>,
|
| 30 |
) -> Result<HttpResponse, MyError> {
|
| 31 |
+
Ok(HttpResponse::Ok().json(sb.submit_rsvp(form.into_inner()).await?))
|
|
|
|
|
|
|
| 32 |
}
|
| 33 |
|
| 34 |
+
async fn get_messages(sb: web::Data<Supabase>) -> Result<HttpResponse, MyError> {
|
| 35 |
+
Ok(HttpResponse::Ok().json(sb.get_messages().await?))
|
|
|
|
|
|
|
| 36 |
}
|
| 37 |
|
| 38 |
#[actix_web::main]
|
| 39 |
async fn main() -> std::io::Result<()> {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
dotenv().ok();
|
| 41 |
|
| 42 |
+
let cfg = AppConfig::builder()
|
| 43 |
.override_with(EnvSource::new())
|
| 44 |
.try_build()
|
| 45 |
+
.unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
let supabase = Supabase::new(cfg.supabase_url, cfg.supabase_anon_key);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
HttpServer::new(move || {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
App::new()
|
| 51 |
+
.wrap(Cors::permissive())
|
| 52 |
+
.app_data(web::Data::new(supabase.clone()))
|
| 53 |
+
.route("/health", web::get().to(health))
|
| 54 |
+
.route("/guests/{slug}", web::get().to(get_guest))
|
| 55 |
+
.route("/rsvp", web::post().to(submit_rsvp))
|
| 56 |
+
.route("/messages", web::get().to(get_messages))
|
| 57 |
})
|
| 58 |
+
.bind(cfg.server_addr)?
|
| 59 |
.run()
|
| 60 |
.await
|
| 61 |
+
}
|