-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
93341ad
commit 3e361eb
Showing
5 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,5 @@ Cargo.lock | |
|
||
# MSVC Windows builds of rustc generate these, which store debugging information | ||
*.pdb | ||
|
||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |