Skip to content

Commit

Permalink
[auth] feat: axum with routes (#632)
Browse files Browse the repository at this point in the history
* feat: router

* feat: tracing and propagation

* refactor: request tracing

* tests: TraceLayer

* refactor: clippy suggestion

* refactor: better comments

* Update gateway/src/api/latest.rs

* tests: more deterministic
  • Loading branch information
chesedo authored Feb 15, 2023
1 parent b3e11b5 commit b7bcbe1
Show file tree
Hide file tree
Showing 14 changed files with 432 additions and 114 deletions.
24 changes: 22 additions & 2 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ once_cell = "1.16.0"
opentelemetry = { version = "0.18.0", features = ["rt-tokio"] }
opentelemetry-datadog = { version = "0.6.0", features = ["reqwest-client"] }
opentelemetry-http = "0.7.0"
uuid = "1.2.2"
thiserror = "1.0.37"
serde = "1.0.148"
serde_json = "1.0.89"
thiserror = "1.0.37"
tower-http = { version = "0.3.4", features = ["trace"] }
tracing = "0.1.37"
tracing-opentelemetry = "0.18.0"
tracing-subscriber = "0.3.16"
uuid = "1.2.2"
6 changes: 5 additions & 1 deletion auth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
axum = { workspace = true }
clap = { workspace = true }
opentelemetry = { workspace = true }
opentelemetry-datadog = { workspace = true }
opentelemetry-http = { workspace = true }
tokio = { version = "1.22.0", features = [ "full" ] }
tracing = { workspace = true }
tracing-opentelemetry = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }

[dependencies.shuttle-common]
workspace = true
features = ["backend"]
2 changes: 1 addition & 1 deletion auth/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ pub struct Args {

/// Address to bind to
#[arg(long, default_value = "127.0.0.1:8000")]
pub user: SocketAddr,
pub address: SocketAddr,
}
13 changes: 13 additions & 0 deletions auth/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
mod args;
mod router;

pub use args::Args;
use tracing::info;

pub async fn start(args: Args) {
let router = router::new();

info!(address=%args.address, "Binding to and listening at address");

axum::Server::bind(&args.address)
.serve(router.into_make_service())
.await
.unwrap_or_else(|_| panic!("Failed to bind to address: {}", args.address));
}
4 changes: 3 additions & 1 deletion auth/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::Parser;
use opentelemetry::global;
use shuttle_auth::Args;
use shuttle_auth::{start, Args};
use tracing::trace;
use tracing_subscriber::{fmt, prelude::*, EnvFilter};

Expand Down Expand Up @@ -43,4 +43,6 @@ async fn main() {
// .unwrap()
// .to_string_lossy()
// );

start(args).await;
}
45 changes: 45 additions & 0 deletions auth/src/router.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use axum::{
middleware::from_extractor,
routing::{get, post},
Router,
};
use shuttle_common::{
backends::metrics::{Metrics, TraceLayer},
request_span,
};
use tracing::field;

pub fn new() -> Router {
Router::new()
.route("/login", post(login))
.route("/logout", post(logout))
.route("/auth/session", post(convert_cookie))
.route("/auth/key", post(convert_key))
.route("/auth/refresh", post(refresh_token))
.route("/public-key", get(get_public_key))
.route("/user/:account_name", get(get_user).post(post_user))
.route_layer(from_extractor::<Metrics>())
.layer(
TraceLayer::new(|request| {
request_span!(request, request.params.account_name = field::Empty)
})
.with_propagation()
.build(),
)
}

async fn login() {}

async fn logout() {}

async fn convert_cookie() {}

async fn convert_key() {}

async fn refresh_token() {}

async fn get_public_key() {}

async fn get_user() {}

async fn post_user() {}
14 changes: 13 additions & 1 deletion common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,27 @@ comfy-table = { version = "6.1.3", optional = true }
crossterm = { version = "0.25.0", optional = true }
http = { version = "0.2.8", optional = true }
once_cell = { workspace = true }
opentelemetry = { workspace = true, optional = true }
opentelemetry-http = { workspace = true, optional = true }
reqwest = { version = "0.11.13", optional = true }
rustrict = "0.5.5"
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true, optional = true }
strum = { version = "0.24.1", features = ["derive"] }
tower-http = { workspace = true, optional = true }
tracing = { workspace = true }
tracing-opentelemetry = { workspace = true, optional = true }
uuid = { workspace = true, features = ["v4", "serde"] }

[features]
backend = ["async-trait", "axum"]
backend = ["async-trait", "axum", "opentelemetry", "opentelemetry-http", "tower-http", "tracing-opentelemetry"]
display = ["comfy-table", "crossterm"]
models = ["anyhow", "async-trait", "display", "http", "reqwest", "serde_json"]

[dev-dependencies]
axum = { workspace = true }
hyper = "0.14.23"
tokio = { version = "1.22.0", features = ["macros", "rt-multi-thread"] }
tower = { version = "0.4", features = ["util"] }
tracing-fluent-assertions = "0.3.0"
tracing-subscriber = { version = "0.3", default-features = false }
Loading

0 comments on commit b7bcbe1

Please sign in to comment.