1
crates/forge-enforce/.gitignore
vendored
Normal file
1
crates/forge-enforce/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
17
crates/forge-enforce/Cargo.toml
Normal file
17
crates/forge-enforce/Cargo.toml
Normal file
@@ -0,0 +1,17 @@
|
||||
[package]
|
||||
name = "forge-enforce"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
anyhow.workspace = true
|
||||
tokio.workspace = true
|
||||
tracing.workspace = true
|
||||
tracing-subscriber.workspace = true
|
||||
clap.workspace = true
|
||||
dotenv.workspace = true
|
||||
axum.workspace = true
|
||||
serde.workspace = true
|
||||
uuid.workspace = true
|
||||
tower-http.workspace = true
|
||||
notmad.workspace = true
|
||||
30
crates/forge-enforce/src/cli.rs
Normal file
30
crates/forge-enforce/src/cli.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
use clap::{Parser, Subcommand};
|
||||
|
||||
use crate::{Config, State, cli::serve::ServeCommand};
|
||||
|
||||
mod serve;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about, long_about = None, subcommand_required = true)]
|
||||
struct Command {
|
||||
#[command(subcommand)]
|
||||
command: Option<Commands>,
|
||||
|
||||
#[command(flatten)]
|
||||
config: Config,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum Commands {
|
||||
Serve(ServeCommand),
|
||||
}
|
||||
|
||||
pub async fn execute() -> anyhow::Result<()> {
|
||||
let cli = Command::parse();
|
||||
|
||||
let state = State::new(cli.config).await?;
|
||||
|
||||
match cli.command.expect("a subcommand") {
|
||||
Commands::Serve(cmd) => cmd.execute(&state).await,
|
||||
}
|
||||
}
|
||||
49
crates/forge-enforce/src/cli/serve.rs
Normal file
49
crates/forge-enforce/src/cli/serve.rs
Normal file
@@ -0,0 +1,49 @@
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use axum::{Router, extract::MatchedPath, http::Request, routing::get};
|
||||
use tower_http::trace::TraceLayer;
|
||||
|
||||
use crate::State;
|
||||
|
||||
#[derive(clap::Parser)]
|
||||
pub struct ServeCommand {
|
||||
#[arg(env = "SERVICE_HOST", long, default_value = "127.0.0.1:3000")]
|
||||
host: SocketAddr,
|
||||
}
|
||||
|
||||
impl ServeCommand {
|
||||
pub async fn execute(&self, state: &State) -> anyhow::Result<()> {
|
||||
let app = Router::new()
|
||||
.route("/", get(root))
|
||||
.with_state(state.clone())
|
||||
.layer(
|
||||
TraceLayer::new_for_http().make_span_with(|request: &Request<_>| {
|
||||
// Log the matched route's path (with placeholders not filled in).
|
||||
// Use request.uri() or OriginalUri if you want the real path.
|
||||
let matched_path = request
|
||||
.extensions()
|
||||
.get::<MatchedPath>()
|
||||
.map(MatchedPath::as_str);
|
||||
|
||||
tracing::info_span!(
|
||||
"http_request",
|
||||
method = ?request.method(),
|
||||
matched_path,
|
||||
some_other_field = tracing::field::Empty,
|
||||
)
|
||||
}), // ...
|
||||
);
|
||||
|
||||
tracing::info!("listening on {}", self.host);
|
||||
let listener = tokio::net::TcpListener::bind(self.host).await.unwrap();
|
||||
axum::serve(listener, app.into_make_service())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
async fn root() -> &'static str {
|
||||
"Hello, nostore!"
|
||||
}
|
||||
14
crates/forge-enforce/src/main.rs
Normal file
14
crates/forge-enforce/src/main.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
mod state;
|
||||
pub use state::*;
|
||||
|
||||
pub mod cli;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
dotenv::dotenv().ok();
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
cli::execute().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
13
crates/forge-enforce/src/state.rs
Normal file
13
crates/forge-enforce/src/state.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
#[derive(clap::Parser, Clone)]
|
||||
pub struct Config {}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct State {
|
||||
pub config: Config,
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub async fn new(config: Config) -> anyhow::Result<Self> {
|
||||
Ok(Self { config })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user