Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub struct GlazedConfig {
pub bind_address: SocketAddr,
pub public_address: Option<Url>,
pub tiled_client: TiledClientConfig,
#[serde(default)]
pub log_level: LogLevel,
}
impl GlazedConfig {
pub fn from_file(path: &Path) -> Result<Self, ConfigError> {
Expand All @@ -24,6 +26,7 @@ impl GlazedConfig {
tiled_client: TiledClientConfig {
address: Url::parse("http://localhost:8000").expect("Static URL is valid"),
},
log_level: LogLevel::Info,
}
}
}
Expand All @@ -32,3 +35,24 @@ impl GlazedConfig {
pub struct TiledClientConfig {
pub address: Url,
}

#[derive(Debug, Default, Deserialize, Clone, Copy)]
pub enum LogLevel {
#[default]
#[serde(alias = "info")]
Info,
#[serde(alias = "debug")]
Debug,
#[serde(alias = "trace")]
Trace,
}

impl From<LogLevel> for tracing::level_filters::LevelFilter {
fn from(value: LogLevel) -> Self {
match value {
LogLevel::Info => Self::INFO,
LogLevel::Debug => Self::DEBUG,
LogLevel::Trace => Self::TRACE,
}
}
}
30 changes: 25 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use async_graphql::{EmptyMutation, EmptySubscription, Schema};
use axum::extract::Path;
use axum::http::StatusCode;
use axum::response::{Html, IntoResponse};
use axum::routing::{get, post};
Expand All @@ -18,17 +19,24 @@ use serde_json::json;
use tokio::select;
use tokio::signal::unix::{SignalKind, signal};
use tracing::info;
use tracing::level_filters::LevelFilter;
use tracing_subscriber::layer::SubscriberExt as _;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::{Registry, fmt, reload};
use url::Url;

use crate::clients::TiledClient;
use crate::config::GlazedConfig;
use crate::config::{GlazedConfig, LogLevel};
use crate::handlers::{download_handler, graphiql_handler, graphql_handler};
use crate::model::TiledQuery;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let subscriber = tracing_subscriber::FmtSubscriber::new();
tracing::subscriber::set_global_default(subscriber)?;
let (filter, filter_reload) = reload::Layer::new(LevelFilter::INFO);
tracing_subscriber::registry()
.with(filter)
.with(fmt::Layer::default())
.init();

let cli = Cli::init();
let config;
Expand All @@ -41,15 +49,21 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
info!("Using default config");
config = GlazedConfig::default();
}
filter_reload
.modify(|f| *f = config.log_level.into())
.unwrap();
match cli.command {
Commands::Serve => serve(config).await,
Commands::Serve => serve(config, filter_reload).await,
}
}

#[derive(Clone)]
pub struct RootAddress(Url);

async fn serve(config: GlazedConfig) -> Result<(), Box<dyn std::error::Error>> {
async fn serve(
config: GlazedConfig,
reload: reload::Handle<LevelFilter, Registry>,
) -> Result<(), Box<dyn std::error::Error>> {
let client = TiledClient::new(config.tiled_client.address);
let public_address = config
.public_address
Expand All @@ -72,6 +86,12 @@ async fn serve(config: GlazedConfig) -> Result<(), Box<dyn std::error::Error>> {
get(Json(json!({"version": env!("CARGO_PKG_VERSION")}))),
)
.route("/asset/{run}/{stream}/{det}/{id}", get(download_handler))
.route(
"/loglevel/{level}",
post(|level: Path<LogLevel>| async move {
reload.clone().modify(|f| *f = level.0.into()).unwrap();
}),
)
.with_state(client)
.fallback((
StatusCode::NOT_FOUND,
Expand Down
3 changes: 1 addition & 2 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::collections::HashMap;

use async_graphql::{Context, Object, Result, Union};
use serde_json::Value;
use tracing::{info, instrument};
use tracing::instrument;

use crate::RootAddress;
use crate::clients::{ClientError, TiledClient};
Expand Down Expand Up @@ -174,7 +174,6 @@ impl TableData {
.map(|s| s.as_str())
.collect::<Vec<_>>()
.join("/");
info!("path: {:?}", p);

let table_data = client.table_full(&p, columns, headers).await?;
Ok(table_data)
Expand Down
Loading