Skip to content

Commit

Permalink
Initial cut at a webserver
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-robbins committed Nov 20, 2023
1 parent 93341ad commit 3e361eb
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ Cargo.lock

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

.idea
13 changes: 13 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "stignore-agent"
version = "0.1.0"
edition = "2021"
authors = ["Michael Robbins"]

[dependencies]
axum = "0.6.20"
serde = { version="1.0.192", features=["derive"] }
tokio = { version="1.34.0", features=["full"] }
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
tracing = "0.1.40"
toml = "0.8.8"
10 changes: 10 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[agent]
port = 3000
name = "Agent Smith"
base_path = "/path/to/stuff"

[category.a]
relative_path = "a"

[category.b]
relative_path = "b"
51 changes: 51 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use serde::{Serialize,Deserialize};
use std::fs;
use std::process::exit;
use toml;

#[derive(Serialize,Deserialize,Clone)]
pub struct Data {
pub(crate) agent: AgentConfig,
category: toml::Table,
}

// Config struct holds to data from the `[config]` section.
#[derive(Serialize,Deserialize,Clone)]
pub struct AgentConfig {
pub name: String,
pub port: u16,
pub base_path: String,
}

#[derive(Serialize,Deserialize,Clone)]
pub struct Categories {
pub name: String,
pub config: CategoryConfig,
}

#[derive(Serialize,Deserialize,Clone)]
pub struct CategoryConfig {
pub relative_path: String,
}

pub fn load_config(filename: &str) -> Data {

let contents = match fs::read_to_string(filename) {
Ok(c) => c,
Err(_) => {
eprintln!("Could not read file `{}`", filename);
exit(1);
}
};

let data: Data = match toml::from_str(&contents) {
Ok(d) => d,
Err(a) => {
eprintln!("Unable to load data from `{}`", filename);
eprintln!("`{}`", a);
exit(1);
}
};

data
}
43 changes: 43 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
mod config;

use axum::{
extract::State,
routing::get,
http::StatusCode,
response::{Html, IntoResponse},
Json, Router,
};
use std::net::SocketAddr;

#[tokio::main]
async fn main() {
// initialize tracing
tracing_subscriber::fmt::init();

// load config
let data = config::load_config("./config.toml");

let app = Router::new()
.route("/", get(help))
.route("/api/v1/discover", get(category_listing))
.with_state(data.clone());

// run our app with hyper
// `axum::Server` is a re-export of `hyper::Server`
let addr = SocketAddr::from(([127, 0, 0, 1], data.agent.port));
tracing::debug!("listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}

async fn help() -> Html<&'static str> {
Html("Please visit <a href='https://github.com/dalmura/stignore-agent'>the documentation</a> for further information")
}

async fn category_listing(
State(data): State<config::Data>,
) -> impl IntoResponse {
(StatusCode::IM_A_TEAPOT, Json(data))
}

0 comments on commit 3e361eb

Please sign in to comment.