Skip to main content

text_index_server/
lib.rs

1use std::io;
2
3use runtime_core::{
4    server::{self, ServerAdapterMetadata},
5    PackageSurface,
6};
7
8pub const LIBRARY_CRATE: &str = "text-index";
9pub const SURFACE_KIND: &str = "api";
10pub const LIBRARY_IMPORT: &str = "use text_index";
11pub const CLI_PACKAGE: &str = "text-index-cli";
12pub const APP_PACKAGE: &str = "text-index-app";
13pub const WASM_PACKAGE: &str = "text-index-wasm";
14
15pub type HttpResponse = server::HttpResponse;
16
17const METADATA: ServerAdapterMetadata = ServerAdapterMetadata {
18    library_crate: LIBRARY_CRATE,
19    surface_kind: SURFACE_KIND,
20    library_import: LIBRARY_IMPORT,
21    cli_package: CLI_PACKAGE,
22    app_package: APP_PACKAGE,
23    wasm_package: WASM_PACKAGE,
24};
25
26pub fn package_surface() -> PackageSurface {
27    text_index::surface::package_surface()
28}
29
30pub fn serve(addr: &str) -> io::Result<()> {
31    server::serve(
32        addr,
33        METADATA,
34        package_surface,
35        text_index::surface::run_surface_operation,
36    )
37}
38
39pub fn response_for(method: &str, path: &str, body: &str) -> HttpResponse {
40    server::response_for(
41        method,
42        path,
43        body,
44        METADATA,
45        package_surface,
46        text_index::surface::run_surface_operation,
47    )
48}
49
50pub fn package_metadata_json() -> String {
51    server::package_metadata_json(METADATA, package_surface())
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn health_endpoint_reports_package() {
60        let response = response_for("GET", "/health", "");
61        assert_eq!(response.status_code, 200);
62        assert!(response.body.contains(LIBRARY_CRATE));
63    }
64}