1
crates/noil-client/.gitignore
vendored
Normal file
1
crates/noil-client/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
19
crates/noil-client/Cargo.toml
Normal file
19
crates/noil-client/Cargo.toml
Normal file
@@ -0,0 +1,19 @@
|
||||
[package]
|
||||
name = "noil-client"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
anyhow.workspace = true
|
||||
tokio.workspace = true
|
||||
tracing.workspace = true
|
||||
tracing-subscriber.workspace = true
|
||||
clap.workspace = true
|
||||
dotenvy.workspace = true
|
||||
axum.workspace = true
|
||||
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
uuid = { version = "1.7", features = ["v4"] }
|
||||
tower-http = { version = "0.6", features = ["cors", "trace", "fs"] }
|
||||
minijinja = { version = "2.11.0", features = ["loader"] }
|
||||
minijinja-autoreload = "2.11.0"
|
||||
48
crates/noil-client/src/api.rs
Normal file
48
crates/noil-client/src/api.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use axum::{
|
||||
extract::{MatchedPath, State as AxumState},
|
||||
http::{Request, StatusCode},
|
||||
response::Html,
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
use tower_http::{services::ServeDir, trace::TraceLayer};
|
||||
|
||||
use crate::state::State;
|
||||
|
||||
pub fn create_router(state: State) -> Router {
|
||||
Router::new()
|
||||
.route("/", get(index))
|
||||
.route("/health", get(health))
|
||||
.nest_service("/static", ServeDir::new("static"))
|
||||
.with_state(state)
|
||||
.layer(
|
||||
TraceLayer::new_for_http().make_span_with(|request: &Request<_>| {
|
||||
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,
|
||||
)
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
async fn index(AxumState(state): AxumState<State>) -> Result<Html<String>, StatusCode> {
|
||||
let env = state.template_env();
|
||||
let tmpl = env
|
||||
.get_template("index.html")
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
let rendered = tmpl
|
||||
.render(minijinja::context!())
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
Ok(Html(rendered))
|
||||
}
|
||||
|
||||
async fn health() -> &'static str {
|
||||
"OK"
|
||||
}
|
||||
45
crates/noil-client/src/cli.rs
Normal file
45
crates/noil-client/src/cli.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use clap::{Parser, Subcommand};
|
||||
|
||||
use crate::{api, state::State};
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about, long_about = None, subcommand_required = true)]
|
||||
pub struct Command {
|
||||
#[command(subcommand)]
|
||||
command: Option<Commands>,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum Commands {
|
||||
Serve {
|
||||
#[arg(env = "SERVICE_HOST", long, default_value = "127.0.0.1:3000")]
|
||||
host: SocketAddr,
|
||||
},
|
||||
}
|
||||
|
||||
impl Command {
|
||||
pub async fn execute() -> anyhow::Result<()> {
|
||||
let cmd = Command::parse();
|
||||
|
||||
match cmd.command {
|
||||
Some(Commands::Serve { host }) => {
|
||||
tracing::info!("Starting service");
|
||||
|
||||
let state = State::new().await?;
|
||||
let app = api::create_router(state);
|
||||
|
||||
tracing::info!("listening on {}", host);
|
||||
let listener = tokio::net::TcpListener::bind(host).await?;
|
||||
axum::serve(listener, app.into_make_service()).await?;
|
||||
}
|
||||
None => {
|
||||
// This shouldn't happen due to subcommand_required = true
|
||||
anyhow::bail!("No command provided");
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
13
crates/noil-client/src/main.rs
Normal file
13
crates/noil-client/src/main.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
mod api;
|
||||
mod cli;
|
||||
mod state;
|
||||
|
||||
use cli::Command;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
dotenvy::dotenv().ok();
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
Command::execute().await
|
||||
}
|
||||
32
crates/noil-client/src/state.rs
Normal file
32
crates/noil-client/src/state.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
use minijinja::Environment;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct State {
|
||||
template_env: Arc<Environment<'static>>,
|
||||
}
|
||||
|
||||
impl State {
|
||||
pub async fn new() -> anyhow::Result<Self> {
|
||||
let mut env = Environment::new();
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
env.set_loader(minijinja::path_loader("templates"));
|
||||
}
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
{
|
||||
env.add_template("base.html", include_str!("../../../templates/base.html"))?;
|
||||
env.add_template("index.html", include_str!("../../../templates/index.html"))?;
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
template_env: Arc::new(env),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn template_env(&self) -> Arc<Environment<'static>> {
|
||||
self.template_env.clone()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user