Skip to content

Commit

Permalink
Adapter tidy
Browse files Browse the repository at this point in the history
- Add basic logging
- Allow setting upstream API URL and API address
  • Loading branch information
DanNixon committed Mar 31, 2024
1 parent 40c23c5 commit 194b4c7
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 5 deletions.
216 changes: 216 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ edition = "2021"
axum = { version = "0.7.5", features = ["macros"] }
axum-extra = { version = "0.9.3", features = ["query"] }
chrono = { version = "0.4.37", features = ["serde"] }
clap = { version = "~4.4.0", features = ["derive", "env"] }
emfcamp-schedule-api = { path = "./client/" }
reqwest = { version = "0.11.27", default-features = false, features = ["json", "rustls-tls"] }
serde = { version = "1.0.137", features = ["derive"] }
tokio = { version = "1.37.0", features = ["rt-multi-thread", "macros"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
url = { version = "2.2.2", features = ["serde"] }
3 changes: 3 additions & 0 deletions adapter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ edition.workspace = true
axum.workspace = true
axum-extra.workspace = true
chrono.workspace = true
clap.workspace = true
emfcamp-schedule-api.workspace = true
serde.workspace = true
tokio.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
url.workspace = true
1 change: 1 addition & 0 deletions adapter/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
Entrypoint = ["${pkgs.tini}/bin/tini" "--" "${emfcamp-schedule-api-adapter}/bin/emfcamp-schedule-api-adapter"];
Env = [
"SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"
"API_ADDRESS=0.0.0.0:8000"
];
};
};
Expand Down
30 changes: 25 additions & 5 deletions adapter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,25 @@ use crate::queries::now_and_next::now_and_next;
use crate::queries::schedule::schedule;
use crate::queries::venues::venues;
use axum::{routing::get, Router};
use clap::Parser;
use std::net::SocketAddr;
use tokio::net::TcpListener;
use tracing::{info, trace};
use url::Url;

#[derive(Parser)]
#[clap(version, about)]
struct Cli {
#[clap(
long,
env,
default_value = "https://www.emfcamp.org/schedule/2022.json"
)]
upstream_api_url: Url,

#[clap(long, env, default_value = "127.0.0.1:8000")]
api_address: SocketAddr,
}

#[derive(Clone)]
struct State {
Expand All @@ -14,9 +31,12 @@ struct State {

#[tokio::main]
async fn main() {
let client = emfcamp_schedule_api::Client::new(
url::Url::parse("https://www.emfcamp.org/schedule/2022.json").unwrap(),
);
tracing_subscriber::fmt::init();

let args = Cli::parse();

trace!("Creating client");
let client = emfcamp_schedule_api::Client::new(args.upstream_api_url);

let state = State { client };

Expand All @@ -26,7 +46,7 @@ async fn main() {
.route("/venues", get(venues))
.with_state(state);

let addr: SocketAddr = ([127, 0, 0, 1], 8000).into();
let listener = TcpListener::bind(&addr).await.unwrap();
info!("API shim running at {}", args.api_address);
let listener = TcpListener::bind(&args.api_address).await.unwrap();
axum::serve(listener, app).await.unwrap();
}
Loading

0 comments on commit 194b4c7

Please sign in to comment.