Skip to content

Instantly share code, notes, and snippets.

@FlakM
Last active August 11, 2023 07:57
Show Gist options
  • Save FlakM/668ff2d5e2195f998c96db0cf353b5d8 to your computer and use it in GitHub Desktop.
Save FlakM/668ff2d5e2195f998c96db0cf353b5d8 to your computer and use it in GitHub Desktop.
//! ```cargo
//! [dependencies]
//!
//! axum = "0.6.20"
//! lazy_static = "1.4.0"
//! serde = "1.0.183"
//! serde_json = "1.0.104"
//! tokio = { version = "1.30.0", features = ["full"] }
//! ```
use axum::response::IntoResponse;
use axum::Json;
use axum::{routing::get, Router};
use serde_json::{json, Value};
use std::net::SocketAddr;
lazy_static::lazy_static! {
static ref CONFIG_DB: String = std::env::var("CONFIG_DB").unwrap_or("PG_URL".to_string());
}
#[tokio::main]
async fn main() {
let app = Router::new().route("/", get(handler));
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}
async fn handler() -> impl IntoResponse {
db_query()
}
/// This is a slow running query that will query multiple databases
fn db_query() -> Json<Value> {
let _config = &*CONFIG_DB;
// simulate issuing slow query using FFI
// THIS SHOULD NOT BE MODIFIED
std::thread::sleep(std::time::Duration::from_secs(1));
Json(json!({
"rsp": "hello",
"database_source": "db"
}))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment