Skip to content

Commit 71a20b0

Browse files
committed
feat(server): cli support for cors
1 parent 3f8cafd commit 71a20b0

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
host = "127.0.0.1"
22
port = "7878"
3+
cors = true
34

45
[file-explorer]
56
path = "./"

crates/http-server/src/cli/command/start.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ pub struct StartOpt {
2020
/// Port to bind the server
2121
#[clap(short = 'p', long, default_value = "7878")]
2222
pub port: u16,
23+
/// Enable CORS with a permissive policy
24+
#[clap(long, default_value = "false")]
25+
pub cors: bool,
26+
}
27+
28+
impl From<&StartOpt> for Config {
29+
fn from(val: &StartOpt) -> Self {
30+
Config {
31+
host: val.host,
32+
port: val.port,
33+
cors: val.cors,
34+
}
35+
}
2336
}
2437

2538
impl StartOpt {
@@ -29,10 +42,7 @@ impl StartOpt {
2942
.thread_name(THREAD_NAME)
3043
.build()?;
3144
let rt = Arc::new(rt);
32-
let config = Config {
33-
host: self.host,
34-
port: self.port,
35-
};
45+
let config: Config = self.into();
3646
let server = Server::new(config);
3747

3848
rt.block_on(async {

crates/http-server/src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ pub struct Config {
55
pub host: IpAddr,
66
/// The port to bind to.
77
pub port: u16,
8+
/// Enable CORS with a permissive policy.
9+
pub cors: bool,
810
}
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,14 @@ impl Server {
3434
let addr = SocketAddr::from((self.config.host, self.config.port));
3535
let listener = TcpListener::bind(addr).await?;
3636
let functions = Arc::new(ExternalFunctions::new());
37-
let plugin_library = PathBuf::from_str("./target/debug/libfile_explorer.dylib").unwrap();
38-
let config = PathBuf::from_str("./config.toml").unwrap();
37+
let plugin_library = PathBuf::from_str("./target/debug/libfile_explorer.dylib")?;
38+
let config = PathBuf::from_str("./config.toml")?;
3939
let handle = Arc::new(rt.handle().to_owned());
4040

4141
unsafe {
4242
functions
4343
.load(Arc::clone(&handle), config, plugin_library)
44-
.await
45-
.expect("Function loading failed");
44+
.await?;
4645
}
4746

4847
loop {

0 commit comments

Comments
 (0)