Files
storkit/server/src/main.rs

141 lines
3.7 KiB
Rust
Raw Normal View History

mod agents;
mod config;
2026-02-16 16:24:21 +00:00
mod http;
mod io;
mod llm;
mod state;
mod store;
mod workflow;
mod worktree;
use crate::agents::AgentPool;
2026-02-16 16:24:21 +00:00
use crate::http::build_routes;
use crate::http::context::AppContext;
use crate::state::SessionState;
use crate::store::JsonFileStore;
use crate::workflow::WorkflowState;
2026-02-16 16:24:21 +00:00
use poem::Server;
use poem::listener::TcpListener;
use std::path::{Path, PathBuf};
use std::sync::Arc;
const DEFAULT_PORT: u16 = 3001;
fn parse_port(value: Option<String>) -> u16 {
value
.and_then(|v| v.parse::<u16>().ok())
.unwrap_or(DEFAULT_PORT)
}
fn resolve_port() -> u16 {
parse_port(std::env::var("STORYKIT_PORT").ok())
}
fn write_port_file(dir: &Path, port: u16) -> Option<PathBuf> {
let path = dir.join(".story_kit_port");
std::fs::write(&path, port.to_string()).ok()?;
Some(path)
}
fn remove_port_file(path: &Path) {
let _ = std::fs::remove_file(path);
}
#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
let app_state = Arc::new(SessionState::default());
let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
*app_state.project_root.lock().unwrap() = Some(cwd.clone());
let store = Arc::new(
JsonFileStore::from_path(PathBuf::from("store.json")).map_err(std::io::Error::other)?,
);
let workflow = Arc::new(std::sync::Mutex::new(WorkflowState::default()));
let agents = Arc::new(AgentPool::new());
let ctx = AppContext {
state: app_state,
store,
workflow,
agents,
};
2026-02-16 16:24:21 +00:00
let app = build_routes(ctx);
let port = resolve_port();
let addr = format!("127.0.0.1:{port}");
2026-02-16 17:10:23 +00:00
println!(
"\x1b[95;1m ____ _ _ ___ _ \n / ___|| |_ ___ _ __| | _|_ _| |_ \n \\___ \\| __/ _ \\| '__| |/ /| || __|\n ___) | || (_) | | | < | || |_ \n |____/ \\__\\___/|_| |_|\\_\\___|\\__|\n\x1b[0m"
);
println!("STORYKIT_PORT={port}");
println!("\x1b[96;1mFrontend:\x1b[0m \x1b[94mhttp://{addr}\x1b[0m");
println!("\x1b[92;1mOpenAPI Docs:\x1b[0m \x1b[94mhttp://{addr}/docs\x1b[0m");
// Validate agent config at startup — panic on invalid project.toml.
config::ProjectConfig::load(&cwd)
.unwrap_or_else(|e| panic!("Invalid project.toml: {e}"));
let port_file = write_port_file(&cwd, port);
let result = Server::new(TcpListener::bind(&addr)).run(app).await;
if let Some(ref path) = port_file {
remove_port_file(path);
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_port_defaults_to_3001() {
assert_eq!(parse_port(None), 3001);
}
#[test]
fn parse_port_reads_valid_value() {
assert_eq!(parse_port(Some("4200".to_string())), 4200);
}
#[test]
fn parse_port_ignores_invalid_value() {
assert_eq!(parse_port(Some("not_a_number".to_string())), 3001);
}
#[test]
#[should_panic(expected = "Invalid project.toml: Duplicate agent name")]
fn panics_on_duplicate_agent_names() {
let tmp = tempfile::tempdir().unwrap();
let sk = tmp.path().join(".story_kit");
std::fs::create_dir_all(&sk).unwrap();
std::fs::write(
sk.join("project.toml"),
r#"
[[agent]]
name = "coder"
[[agent]]
name = "coder"
"#,
)
.unwrap();
config::ProjectConfig::load(tmp.path())
.unwrap_or_else(|e| panic!("Invalid project.toml: {e}"));
}
#[test]
fn write_and_remove_port_file() {
let tmp = tempfile::tempdir().unwrap();
let path = write_port_file(tmp.path(), 4567).expect("should write port file");
assert_eq!(std::fs::read_to_string(&path).unwrap(), "4567");
2026-02-16 17:10:23 +00:00
remove_port_file(&path);
assert!(!path.exists());
}
}