This repository has been archived on 2024-10-26. You can view files and clone it, but cannot push or open issues or pull requests.
staxman-old/src/main.rs

63 lines
1.4 KiB
Rust
Raw Normal View History

2023-11-18 15:35:30 +00:00
use anyhow::Result;
2023-11-18 15:58:28 +00:00
use axum::middleware::from_fn;
2023-11-18 15:35:30 +00:00
use bollard::Docker;
use clap::Parser;
2023-11-18 13:29:19 +00:00
use std::net::SocketAddr;
2023-11-18 11:35:40 +00:00
2023-11-18 15:58:28 +00:00
use crate::http::error::error_interceptor;
2023-11-18 15:35:30 +00:00
mod http;
mod route;
mod stack;
2023-11-18 13:29:19 +00:00
2023-11-18 15:35:30 +00:00
/// GitOps+WebUI for arion-based stacks
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
/// Path to root of stacks
#[arg(short = 'd', long = "stack-dir", env = "STAX_DIR")]
stack_dir: String,
/// Address:port to bind
#[arg(short, long, default_value = "0.0.0.0:3000", env = "STAX_BIND")]
bind: SocketAddr,
}
2023-11-18 13:29:19 +00:00
2023-11-18 15:35:30 +00:00
#[derive(Clone)]
pub struct AppState {
pub stack_dir: String,
pub docker: Docker,
2023-11-18 11:35:40 +00:00
}
#[tokio::main]
2023-11-18 15:35:30 +00:00
async fn main() -> Result<()> {
// Initialize logging and env
_ = dotenvy::dotenv();
2023-11-18 13:29:19 +00:00
tracing_subscriber::fmt::init();
2023-11-18 11:35:40 +00:00
2023-11-18 15:35:30 +00:00
// Try to connect to docker server
let docker = Docker::connect_with_local_defaults()?;
// Ping to make sure it works
let version = docker.version().await?;
tracing::info!("docker version: {}", version.version.unwrap());
2023-11-18 11:35:40 +00:00
2023-11-18 15:35:30 +00:00
// Parse args
let args = Args::parse();
tracing::info!("listening on {}", &args.bind);
2023-11-18 11:35:40 +00:00
2023-11-18 15:35:30 +00:00
let state = AppState {
stack_dir: args.stack_dir,
docker,
};
2023-11-18 15:58:28 +00:00
let app = route::router()
.with_state(state)
.layer(from_fn(error_interceptor));
2023-11-18 15:35:30 +00:00
axum::Server::bind(&args.bind)
2023-11-18 11:35:40 +00:00
.serve(app.into_make_service())
2023-11-18 15:35:30 +00:00
.await?;
Ok(())
2023-11-18 11:35:40 +00:00
}