1+ // Yes i regret using rust for this
2+
13use actix_cors:: Cors ;
24use actix_web:: { App , HttpResponse , HttpServer , Responder , web} ;
35use dotenvy:: from_path;
4- use sqlx:: PgPool ;
5- use std:: env;
6+ use sqlx:: { Column , PgPool , Row } ;
7+ use std:: sync:: LazyLock ;
8+ use std:: { collections:: BTreeMap , env} ;
9+
10+ static IS_DEV : LazyLock < bool > = LazyLock :: new ( || std:: env:: args ( ) . any ( |arg| arg == "--dev" ) ) ;
611
712async fn index ( db_pool : web:: Data < PgPool > ) -> impl Responder {
813 let row: ( String , ) = sqlx:: query_as ( "SELECT 'Hello from the database!'" )
@@ -13,18 +18,51 @@ async fn index(db_pool: web::Data<PgPool>) -> impl Responder {
1318 HttpResponse :: Ok ( ) . body ( row. 0 )
1419}
1520
21+ pub async fn get_all_data ( db_pool : web:: Data < PgPool > ) -> impl Responder {
22+ if !* IS_DEV {
23+ return HttpResponse :: Ok ( ) . body ( "This endpoint is not available in production mode." ) ;
24+ }
25+
26+ let tables_query =
27+ "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'" ;
28+ let tables: Vec < ( String , ) > = sqlx:: query_as ( tables_query)
29+ . fetch_all ( & * * db_pool)
30+ . await
31+ . unwrap ( ) ;
32+
33+ let mut all_data = BTreeMap :: new ( ) ;
34+
35+ for ( table_name, ) in tables {
36+ let query = format ! ( "SELECT * FROM {}" , table_name) ;
37+ let rows = sqlx:: query ( & query) . fetch_all ( & * * db_pool) . await . unwrap ( ) ;
38+
39+ let table_data: Vec < BTreeMap < String , Option < String > > > = rows
40+ . iter ( )
41+ . map ( |row| {
42+ let mut map = BTreeMap :: new ( ) ;
43+ for ( i, column) in row. columns ( ) . iter ( ) . enumerate ( ) {
44+ let value: Option < String > = row. try_get ( i) . unwrap_or ( None ) ;
45+ map. insert ( column. name ( ) . to_string ( ) , value) ;
46+ }
47+ map
48+ } )
49+ . collect ( ) ;
50+
51+ all_data. insert ( table_name, table_data) ;
52+ }
53+
54+ HttpResponse :: Ok ( ) . json ( all_data)
55+ }
56+
1657#[ actix_web:: main]
1758async fn main ( ) -> std:: io:: Result < ( ) > {
18- // Get --dev flag
19- let is_dev = std:: env:: args ( ) . any ( |arg| arg == "--dev" ) ;
20-
2159 // Load .env from root
2260 let cwd = env:: current_dir ( ) . expect ( "Could not get current directory" ) ;
2361 let dotenv_path = cwd. join ( "../.env" ) ;
2462 from_path ( & dotenv_path) . expect ( & format ! ( "Failed to load .env from {:?}" , dotenv_path) ) ;
2563
2664 // Choose DB connection credentials based on mode
27- let ( host, port, user, password, db) = if is_dev {
65+ let ( host, port, user, password, db) = if * IS_DEV {
2866 (
2967 env:: var ( "POSTGRES_DEV_HOST" ) . unwrap ( ) ,
3068 env:: var ( "POSTGRES_DEV_PORT" ) . unwrap ( ) ,
@@ -53,7 +91,7 @@ async fn main() -> std::io::Result<()> {
5391 // Start the server
5492 println ! (
5593 "Running in {} mode" ,
56- if is_dev { "development" } else { "production" }
94+ if * IS_DEV { "development" } else { "production" }
5795 ) ;
5896 println ! ( "Server listening on http://127.0.0.1:8080" ) ;
5997
@@ -67,6 +105,7 @@ async fn main() -> std::io::Result<()> {
67105 . allow_any_header ( ) ,
68106 )
69107 . route ( "/" , web:: get ( ) . to ( index) )
108+ . route ( "/getall" , web:: get ( ) . to ( get_all_data) )
70109 } )
71110 . bind ( "0.0.0.0:8080" ) ?
72111 . run ( )
0 commit comments