🎉 Start developing!

This commit is contained in:
2024-01-13 12:35:59 +08:00
parent 3f434bfe46
commit bf7004c89c
74 changed files with 2118 additions and 3205 deletions

9
src/config/loader.rs Normal file
View File

@@ -0,0 +1,9 @@
use config::Config;
pub fn load_settings() -> Config {
Config::builder()
.add_source(config::File::with_name("Settings"))
.add_source(config::Environment::with_prefix("ROADSIGN"))
.build()
.unwrap()
}

1
src/config/mod.rs Normal file
View File

@@ -0,0 +1 @@
pub mod loader;

39
src/main.rs Normal file
View File

@@ -0,0 +1,39 @@
mod config;
mod proxies;
mod sideload;
use poem::{listener::TcpListener, Route, Server};
use poem_openapi::OpenApiService;
#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
// Load settings
let settings = config::loader::load_settings();
println!(
"Will listen at {:?}",
settings.get_array("listen.proxies").unwrap()
);
if std::env::var_os("RUST_LOG").is_none() {
std::env::set_var("RUST_LOG", "poem=debug");
}
tracing_subscriber::fmt::init();
// Proxies
// Sideload
let sideload = OpenApiService::new(sideload::SideloadApi, "Sideload API", "1.0")
.server("http://localhost:3000/cgi");
let sideload_server = Server::new(TcpListener::bind(
settings
.get_string("listen.sideload")
.unwrap_or("0.0.0.0:81".to_string()),
))
.run(Route::new().nest("/cgi", sideload));
tokio::try_join!(sideload_server)?;
Ok(())
}

22
src/proxies/config.rs Normal file
View File

@@ -0,0 +1,22 @@
use std::{collections::HashMap, time::Duration};
#[derive(Debug, Clone)]
pub struct Region {
id: String,
locations: Vec<Location>,
}
#[derive(Debug, Clone)]
pub struct Location {
hosts: Vec<String>,
paths: Vec<String>,
headers: Vec<HashMap<String, String>>,
query_strings: Vec<HashMap<String, String>>,
destination: Vec<Destination>,
}
#[derive(Debug, Clone)]
pub struct Destination {
uri: Vec<String>,
timeout: Duration,
}

1
src/proxies/mod.rs Normal file
View File

@@ -0,0 +1 @@
pub mod config;

3
src/sideload/mod.rs Normal file
View File

@@ -0,0 +1,3 @@
pub mod overview;
pub struct SideloadApi;

14
src/sideload/overview.rs Normal file
View File

@@ -0,0 +1,14 @@
use poem_openapi::{param::Query, payload::PlainText, OpenApi};
use super::SideloadApi;
#[OpenApi]
impl SideloadApi {
#[oai(path = "/hello", method = "get")]
async fn index(&self, name: Query<Option<String>>) -> PlainText<String> {
match name.0 {
Some(name) => PlainText(format!("hello, {name}!")),
None => PlainText("hello!".to_string()),
}
}
}