add console static server

This commit is contained in:
weisd
2025-01-22 13:42:40 +08:00
parent 5c339f0757
commit 375834ea46
7 changed files with 72 additions and 5 deletions
+1
View File
@@ -69,6 +69,7 @@ crypto = { path = "../crypto" }
iam = { path = "../iam" }
jsonwebtoken = "9.3.0"
tower-http = { version = "0.6.2", features = ["cors"] }
include_dir = "0.7.4"
[build-dependencies]
prost-build.workspace = true
+3
View File
@@ -53,4 +53,7 @@ pub struct Opt {
/// Domain name used for virtual-hosted-style requests.
#[arg(long)]
pub domain_name: Option<String>,
#[arg(long, default_value_t = format!("0.0.0.0:{}", 0))]
pub console_address: String,
}
+37
View File
@@ -0,0 +1,37 @@
use axum::{
body::Body,
http::{Response, StatusCode},
response::IntoResponse,
routing::get,
Router,
};
use include_dir::{include_dir, Dir};
static STATIC_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/static");
async fn static_handler(uri: axum::http::Uri) -> impl IntoResponse {
let path = uri.path().trim_start_matches('/');
if let Some(file) = STATIC_DIR.get_file(path) {
Response::builder()
.status(StatusCode::OK)
.body(Body::from(file.contents()))
.unwrap()
} else {
Response::builder()
.status(StatusCode::NOT_FOUND)
.body(Body::from("404 Not Found"))
.unwrap()
}
}
pub async fn start_static_file_server(addrs: &str) {
// 创建路由
let app = Router::new().route("/*file", get(static_handler));
let listener = tokio::net::TcpListener::bind(addrs).await.unwrap();
println!("console listening on: {}", listener.local_addr().unwrap());
axum::serve(listener, app).await.unwrap();
}
+6 -1
View File
@@ -1,6 +1,7 @@
mod admin;
mod auth;
mod config;
mod console;
mod grpc;
mod service;
mod storage;
@@ -176,7 +177,7 @@ async fn run(opt: config::Opt) -> Result<()> {
let http_server = ConnBuilder::new(TokioExecutor::new());
let mut ctrl_c = std::pin::pin!(tokio::signal::ctrl_c());
let graceful = hyper_util::server::graceful::GracefulShutdown::new();
info!("server is running at http://{local_addr}");
println!("server is running at http://{local_addr}");
loop {
let (socket, _) = tokio::select! {
@@ -239,6 +240,10 @@ async fn run(opt: config::Opt) -> Result<()> {
info!("server was started");
tokio::spawn(async move {
console::start_static_file_server(&opt.console_address).await;
});
tokio::select! {
_ = tokio::signal::ctrl_c() => {
+1
View File
@@ -0,0 +1 @@
static index