feat(ops): add operation-system app skeleton

This commit is contained in:
caoqianming 2026-04-16 12:59:31 +08:00
parent c562bcc10b
commit 3cc13ccf1e
8 changed files with 141 additions and 1 deletions

1
Cargo.lock generated
View File

@ -139,6 +139,7 @@ dependencies = [
"dotenv",
"plc_platform_core",
"tokio",
"tower",
"tower-http",
"tracing",
]

View File

@ -11,6 +11,9 @@ tower-http = { version = "0.6", features = ["cors", "fs"] }
tracing = "0.1"
dotenv = "0.15"
[dev-dependencies]
tower = { version = "0.5", features = ["util"] }
[[bin]]
name = "app_operation_system"
path = "src/main.rs"

View File

@ -0,0 +1,69 @@
use crate::router::build_router;
#[derive(Clone, Debug)]
pub struct AppConfig {
pub server_host: String,
pub server_port: u16,
}
impl AppConfig {
pub fn from_env() -> Self {
Self {
server_host: std::env::var("OPS_SERVER_HOST")
.unwrap_or_else(|_| "127.0.0.1".to_string()),
server_port: std::env::var("OPS_SERVER_PORT")
.ok()
.and_then(|value| value.parse().ok())
.unwrap_or(3100),
}
}
}
#[derive(Clone, Debug)]
pub struct AppState {
pub app_name: &'static str,
pub config: AppConfig,
}
pub async fn run() {
dotenv::dotenv().ok();
plc_platform_core::util::log::init_logger();
let _platform = plc_platform_core::bootstrap::bootstrap_platform();
let _single_instance =
match plc_platform_core::util::single_instance::try_acquire("PLCControl.OperationSystem") {
Ok(guard) => guard,
Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => {
tracing::warn!("Another operation-system instance is already running");
return;
}
Err(err) => {
tracing::error!("Failed to initialize single instance guard: {}", err);
return;
}
};
let state = AppState {
app_name: "operation-system",
config: AppConfig::from_env(),
};
let app = build_router(state.clone());
let addr = format!("{}:{}", state.config.server_host, state.config.server_port);
tracing::info!("Starting operation-system server at http://{}", addr);
let listener = tokio::net::TcpListener::bind(&addr)
.await
.expect("operation-system listener should bind");
axum::serve(listener, app)
.await
.expect("operation-system server should run");
}
pub fn test_state() -> AppState {
AppState {
app_name: "operation-system",
config: AppConfig {
server_host: "127.0.0.1".to_string(),
server_port: 0,
},
}
}

View File

@ -0,0 +1,5 @@
pub mod app;
pub mod router;
pub use app::{run, test_state, AppState};
pub use router::build_router;

View File

@ -1 +1,6 @@
fn main() {}
#![cfg_attr(all(windows, not(debug_assertions)), windows_subsystem = "windows")]
#[tokio::main]
async fn main() {
app_operation_system::run().await;
}

View File

@ -0,0 +1,20 @@
use axum::{extract::State, routing::get, Router};
use tower_http::services::ServeDir;
use crate::app::AppState;
const WEB_ROOT: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/web");
pub fn build_router(state: AppState) -> Router {
Router::new()
.route("/api/health", get(health_check))
.nest_service(
"/ui",
ServeDir::new(WEB_ROOT).append_index_html_on_directories(true),
)
.with_state(state)
}
async fn health_check(State(state): State<AppState>) -> String {
format!("{}:ok", state.app_name)
}

View File

@ -0,0 +1,23 @@
use axum::{
body::Body,
http::{Method, Request, StatusCode},
};
use tower::ServiceExt;
#[tokio::test]
async fn operation_system_router_exposes_health_endpoint() {
let app = app_operation_system::build_router(app_operation_system::app::test_state());
let response = app
.oneshot(
Request::builder()
.method(Method::GET)
.uri("/api/health")
.body(Body::empty())
.expect("request should build"),
)
.await
.expect("router should answer request");
assert_eq!(response.status(), StatusCode::OK);
}

View File

@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>PLC Control Operation System</title>
</head>
<body>
<main>
<h1>Operation System</h1>
<p>This web root is a placeholder for the operation-system app skeleton.</p>
</main>
</body>
</html>