grafbase_local_backend/
server_api.rs

1use crate::errors::BackendError;
2use crate::types::ServerMessage;
3use common::types::LocalAddressType;
4use common::utils::find_available_port;
5use server::errors::ServerError;
6use std::sync::mpsc::Receiver;
7use std::thread;
8
9type ServerInfo = (thread::JoinHandle<Result<(), ServerError>>, Receiver<ServerMessage>);
10
11/// starts the server if an available port can be found
12///
13/// # Errors
14///
15/// returns [`BackendError::AvailablePort`] if no available port can  be found
16///
17/// returns [`BackendError::PortInUse`] if search is off and the supplied port is in use
18pub fn start_server(start_port: u16, search: bool, watch: bool, tracing: bool) -> Result<ServerInfo, BackendError> {
19    match find_available_port(search, start_port, LocalAddressType::Localhost) {
20        Some(port) => {
21            let (handle, receiver) = server::start(port, watch, tracing);
22            Ok((handle, receiver))
23        }
24        None => {
25            if search {
26                Err(BackendError::AvailablePort)
27            } else {
28                Err(BackendError::PortInUse(start_port))
29            }
30        }
31    }
32}