2024-02-01 15:25:51 -05:00
|
|
|
// Copyright 2021 Jeremy Wall
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2024-02-03 16:31:41 -06:00
|
|
|
use std::path::PathBuf;
|
2024-02-01 15:25:51 -05:00
|
|
|
|
2024-02-03 16:31:41 -06:00
|
|
|
use anyhow;
|
2024-02-03 19:06:38 -06:00
|
|
|
use axum::{self, extract::State, routing::*, Router};
|
2024-02-04 16:39:25 -06:00
|
|
|
use clap::{self, Parser, ValueEnum};
|
|
|
|
use tokio::net::TcpListener;
|
|
|
|
use tower_http::trace::TraceLayer;
|
|
|
|
use tracing::Level;
|
|
|
|
use tracing_subscriber::FmtSubscriber;
|
2024-02-01 15:25:51 -05:00
|
|
|
|
2024-02-03 16:31:41 -06:00
|
|
|
mod dashboard;
|
|
|
|
mod query;
|
2024-02-01 15:25:51 -05:00
|
|
|
mod routes;
|
|
|
|
|
2024-02-04 16:39:25 -06:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
|
|
|
|
enum Verbosity {
|
|
|
|
ERROR,
|
|
|
|
WARN,
|
|
|
|
INFO,
|
|
|
|
DEBUG,
|
|
|
|
TRACE,
|
|
|
|
}
|
|
|
|
|
2024-02-03 16:31:28 -06:00
|
|
|
#[derive(clap::Parser)]
|
|
|
|
#[command(author, version, about, long_about = None)]
|
|
|
|
struct Cli {
|
|
|
|
#[arg(long)]
|
2024-02-04 16:39:25 -06:00
|
|
|
pub listen: Option<String>,
|
2024-02-03 16:31:41 -06:00
|
|
|
#[arg(long)]
|
2024-02-04 16:39:25 -06:00
|
|
|
pub config: PathBuf,
|
|
|
|
#[arg(long, value_enum, default_value_t = Verbosity::INFO)]
|
|
|
|
pub verbose: Verbosity,
|
2024-02-03 16:31:28 -06:00
|
|
|
}
|
|
|
|
|
2024-02-04 16:39:25 -06:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> anyhow::Result<()> {
|
|
|
|
let args = Cli::parse();
|
|
|
|
let subscriber_builder = FmtSubscriber::builder().with_max_level(match args.verbose {
|
|
|
|
Verbosity::ERROR => Level::ERROR,
|
|
|
|
Verbosity::WARN => Level::WARN,
|
|
|
|
Verbosity::INFO => Level::INFO,
|
|
|
|
Verbosity::DEBUG => Level::DEBUG,
|
|
|
|
Verbosity::TRACE => Level::TRACE,
|
|
|
|
});
|
|
|
|
tracing::subscriber::set_global_default(
|
|
|
|
subscriber_builder.with_writer(std::io::stderr).finish(),
|
|
|
|
)
|
|
|
|
.expect("setting default subscriber failed");
|
|
|
|
|
|
|
|
let config = std::sync::Arc::new(dashboard::read_dashboard_list(args.config.as_path())?);
|
|
|
|
let router = Router::new()
|
|
|
|
// JSON api endpoints
|
2024-02-07 14:37:24 -06:00
|
|
|
.nest("/js", routes::mk_js_routes(config.clone()))
|
2024-02-04 16:39:25 -06:00
|
|
|
.nest("/api", routes::mk_api_routes(config.clone()))
|
|
|
|
// HTMX ui component endpoints
|
|
|
|
.nest("/ui", routes::mk_ui_routes(config.clone()))
|
2024-02-07 07:10:12 -06:00
|
|
|
.route("/", get(routes::index).with_state(State(config.clone())))
|
2024-02-04 16:39:25 -06:00
|
|
|
.layer(TraceLayer::new_for_http())
|
|
|
|
.with_state(State(config.clone()));
|
|
|
|
let socket_addr = args.listen.unwrap_or("127.0.0.1:3000".to_string());
|
|
|
|
let listener = TcpListener::bind(socket_addr).await.expect("Unable to bind listener to address");
|
|
|
|
axum::serve(listener, router).await?;
|
|
|
|
Ok(())
|
2024-02-01 15:25:51 -05:00
|
|
|
}
|