Repository navigation
Expand file tree
/
Copy pathserver_example.rs
More file actions
140 lines (113 loc) · 4.04 KB
/
Copy pathserver_example.rs
File metadata and controls
140 lines (113 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//! Example of starting the Oxidowl reasoner with a web server
//!
//! This example demonstrates how to:
//! 1. Load an ontology
//! 2. Start a web server for remote access (`OWLlink`, SPARQL, REST API)
//! 3. Configure server ports and bind addresses
//!
//! Usage:
//! ```bash
//! # Run with default settings (requires 'server' feature)
//! cargo run --example server_example --features server -- path/to/ontology.owl
//!
//! # Run on a custom port
//! cargo run --example server_example --features server -- path/to/ontology.owl --port 9090
//! ```
use clap::Parser;
#[allow(unused_imports)]
use oxidowl::{OntologyFormat, Reasoner, ReasonerConfig, Result};
use std::path::PathBuf;
#[derive(Parser)]
#[command(name = "server_example")]
#[command(about = "Example of running Oxidowl with a web server")]
struct Args {
/// Path to the ontology file
ontology: PathBuf,
/// Server port (default: 8080)
#[arg(short, long, default_value = "8080")]
port: u16,
/// Bind address (default: 127.0.0.1)
#[arg(short, long, default_value = "127.0.0.1")]
bind: String,
/// Enable `OWLlink` server
#[arg(long)]
owllink: bool,
/// `OWLlink` port (default: 8081)
#[arg(long, default_value = "8081")]
owllink_port: u16,
}
#[cfg(feature = "server")]
#[tokio::main]
async fn main() -> Result<()> {
use std::sync::Arc;
// Initialize logging
tracing_subscriber::fmt::init();
let args = Args::parse();
println!("Oxidowl Server Example");
println!("======================\n");
// Create reasoner with default configuration
let mut config = ReasonerConfig::default();
// Configure server settings
config.server.enable(oxidowl::config::ServerFeature::Server);
config.server.rest_api_port = args.port;
config.server.bind_address = args.bind.clone();
if args.owllink {
config
.server
.enable(oxidowl::config::ServerFeature::OWLlink);
config.server.owllink_port = args.owllink_port;
}
println!("Creating reasoner...");
let mut reasoner = Reasoner::new(config.clone())?;
// Load the ontology
println!("Loading ontology from: {}", args.ontology.display());
reasoner.load_ontology_from_file(&args.ontology, OntologyFormat::Auto)?;
println!("Ontology loaded successfully!\n");
// Perform initial consistency check
println!("Performing consistency check...");
let is_consistent = reasoner.is_consistent()?;
println!(
"Ontology is {}\n",
if is_consistent {
"consistent"
} else {
"inconsistent"
}
);
// Create and start the server
println!("Starting web server...");
let ontology = reasoner
.get_ontology()
.ok_or_else(|| oxidowl::Error::io("No ontology loaded".to_string()))?;
let ontology_clone = ontology
.read()
.map_err(|_| oxidowl::Error::io("Failed to acquire ontology read lock".to_string()))?
.clone();
let reasoning_service = Arc::new(oxidowl::reasoning::ReasoningService::new(
ontology_clone,
config.clone(),
)?);
let mut server_manager = oxidowl::ServerManager::new(config.server.clone(), reasoning_service);
server_manager.start_all().await?;
println!("\n✓ Server started successfully!");
println!("\nServer endpoints:");
println!(" REST API: http://{}:{}", args.bind, args.port);
if args.owllink {
println!(" OWLlink: http://{}:{}", args.bind, args.owllink_port);
}
println!("\nPress Ctrl+C to stop the server...\n");
// Wait for Ctrl+C
tokio::signal::ctrl_c()
.await
.map_err(|e| oxidowl::Error::io(format!("Failed to listen for shutdown signal: {}", e)))?;
println!("\nShutting down servers...");
server_manager.stop_all().await?;
println!("Servers stopped successfully.");
Ok(())
}
#[cfg(not(feature = "server"))]
fn main() {
eprintln!("This example requires the 'server' feature to be enabled.");
eprintln!("Run with: cargo run --example server_example --features server");
std::process::exit(1);
}