hyper_static_server/
main.rs

1
2
3#[ allow (unused_imports) ]
4use crate::hss;
5
6
7
8
9#[ cfg ( any (feature = "server", feature = "exporter") ) ]
10pub fn main (_routes : impl Into<hss::Routes>) -> hss::ServerResult {
11	return main_wrapper (_routes, None, None);
12}
13
14
15#[ cfg ( any (feature = "server", feature = "exporter") ) ]
16pub fn main_wrapper (_routes : impl Into<hss::Routes>, _configuration : Option<hss::Configuration>, _arguments : Option<hss::CliArguments>) -> hss::ServerResult {
17	
18	let _arguments = hss::CliArguments::from_args ();
19	
20	fn _main_serve (_routes : impl Into<hss::Routes>, _configuration : Option<hss::Configuration>, _arguments : hss::CliArguments) -> hss::ServerResult {
21			#[ cfg (feature = "server") ]
22			return main_serve_with_static (_routes, _configuration, Some (_arguments));
23			#[ cfg (not (feature = "server") ) ]
24			hss::fail_with_message! (0x2504f6ba, "executable built without `server` feature!");
25		}
26	
27	fn _main_export (_routes : impl Into<hss::Routes>, _arguments : hss::CliArguments) -> hss::ServerResult {
28			#[ cfg (feature = "exporter") ]
29			return main_export_with_static (_routes, Some (_arguments));
30			#[ cfg (not (feature = "exporter") ) ]
31			hss::fail_with_message! (0xfc32851f, "executable built without `exporter` feature!");
32		}
33	
34	match _arguments.first_str () {
35		
36		None =>
37			return _main_serve (_routes, _configuration, _arguments),
38		Some ("server") | Some ("serve") =>
39			return _main_serve (_routes, _configuration, _arguments.without_first ()),
40		Some ("exporter") | Some ("export") =>
41			return _main_export (_routes, _arguments.without_first ()),
42		Some (_mode) if ! _mode.starts_with ("-") =>
43			hss::fail_with_format! (0x98f52b4c, "invalid mode `{}`!", _mode),
44		Some (_) =>
45			return _main_serve (_routes, _configuration, _arguments),
46	}
47}
48
49
50
51
52#[ cfg (feature = "server") ]
53pub fn main_serve_with_static (_routes : impl Into<hss::Routes>, _configuration : Option<hss::Configuration>, _arguments : Option<hss::CliArguments>) -> hss::ServerResult {
54	
55	let _routes = _routes.into ();
56	let _handler = crate::server::StaticHandler::new (_routes);
57	
58	return hss::main_with_handler_dyn (_handler, _configuration, _arguments);
59}
60
61
62
63
64#[ cfg (feature = "exporter") ]
65pub fn main_export_with_static (_routes : impl Into<hss::Routes>, _arguments : Option<hss::CliArguments>) -> hss::ServerResult {
66	
67	let mut _arguments = hss::CliArguments::unwrap_or_args (_arguments);
68	let _mode = _arguments.remove_first_str ();
69	
70	let _routes = _routes.into ();
71	
72	match _mode.as_deref () {
73		
74		None | Some ("debug") => {
75			if ! _arguments.is_empty () {
76				hss::fail_with_message! (0x2383b422, "unexpected extra arguments!");
77			}
78			return crate::exporter::export_routes_debug (_routes, ::std::io::stdout ());
79		}
80		
81		Some ("cpio") => {
82			if ! _arguments.is_empty () {
83				hss::fail_with_message! (0xba48b225, "unexpected extra arguments!");
84			}
85			return crate::exporter::export_routes_cpio (_routes, ::std::io::stdout ());
86		}
87		
88		Some ("dump") => {
89			let (_route,) = if let Ok (_tuple) = _arguments.into_tuple_1_str () {
90				_tuple
91			} else {
92				hss::fail_with_message! (0x509712ab, "unexpected extra arguments!");
93			};
94			return crate::exporter::export_routes_dump (_routes, &_route, ::std::io::stdout ());
95		}
96		
97		Some (_mode) if ! _mode.starts_with ("-") =>
98			hss::fail_with_format! (0xf108089b, "invalid exporter mode `{}`!", _mode),
99		
100		Some (_) =>
101			hss::fail_with_message! (0x186bfec4, "unexpected extra arguments!"),
102	}
103}
104