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
use crate::errors::BackendError;
use crate::types::ServerMessage;
use common::utils::find_available_port;
use common::{consts::DEFAULT_PORT, types::LocalAddressType};
use server::errors::ServerError;
use std::sync::mpsc::Receiver;
use std::thread;
type ServerInfo = (thread::JoinHandle<Result<(), ServerError>>, Receiver<ServerMessage>);
pub fn start_server(external_port: Option<u16>, search: bool, watch: bool) -> Result<ServerInfo, BackendError> {
let start_port = external_port.unwrap_or(DEFAULT_PORT);
match find_available_port(search, start_port, LocalAddressType::Localhost) {
Some(port) => {
let (handle, receiver) = server::start(port, watch);
Ok((handle, receiver))
}
None => {
if search {
Err(BackendError::AvailablePort)
} else {
Err(BackendError::PortInUse(start_port))
}
}
}
}