Skip to content

Commit 4a6ce9c

Browse files
authored
feat(server): re-structure server logic to have middleware applied optionally (#452)
1 parent 71a20b0 commit 4a6ce9c

File tree

12 files changed

+209
-48
lines changed

12 files changed

+209
-48
lines changed

Cargo.lock

Lines changed: 136 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ gloo = "0.11.0"
2323
gloo-file = "0.3.0"
2424
humansize = "2.1.3"
2525
http = "1.1.0"
26-
http-auth-basic = "0.3.3"
2726
http-body-util = "0.1"
2827
hyper = "1.4"
2928
hyper-util = "0.1.9"
@@ -44,8 +43,9 @@ serde_json = "1.0.128"
4443
tokio = "1.40"
4544
tokio-util = "0.7.12"
4645
toml = "0.8.19"
47-
tower-http = "0.6.1"
4846
tower = "0.5.1"
47+
tower-http = "0.6.1"
48+
tower-layer = "0.3.3"
4949
tracing = "0.1.40"
5050
tracing-subscriber = "0.3.18"
5151
web-sys = "0.3.72"

crates/http-server/Cargo.toml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ keywords = ["configurable", "http", "server", "serve", "static"]
1010
license = "MIT OR Apache-2.0"
1111
readme = "README.md"
1212

13+
[[bin]]
14+
name = "http-server"
15+
path = "src/bin/main.rs"
16+
17+
[lib]
18+
name = "http_server_rs"
19+
path = "src/lib.rs"
20+
1321
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1422

1523
[dependencies]
@@ -18,7 +26,6 @@ async-trait = { workspace = true }
1826
bytes = { workspace = true }
1927
clap = { workspace = true, features = ["env", "derive", "std"] }
2028
http = { workspace = true }
21-
http-auth-basic = { workspace = true }
2229
http-body-util = { workspace = true }
2330
hyper = { workspace = true }
2431
hyper-util = { workspace = true, features = ["full"] }
@@ -27,6 +34,7 @@ local-ip-address = { workspace = true }
2734
tokio = { workspace = true, features = ["fs", "rt-multi-thread", "signal", "macros"] }
2835
tower-http = { workspace = true, features = ["cors"] }
2936
tower = { workspace = true, features = ["util"] }
37+
tower-layer = { workspace = true }
3038
tracing = { workspace = true }
3139
tracing-subscriber = { workspace = true, features = ["env-filter"] }
3240

File renamed without changes.

crates/http-server/src/cli/command/start.rs renamed to crates/http-server/src/bin/cli/command/start.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use clap::Parser;
77
use tokio::runtime::Builder;
88
use tracing::{error, info};
99

10-
use crate::config::Config;
11-
use crate::server::Server;
10+
use http_server_rs::server::config::Config;
11+
use http_server_rs::server::Server;
1212

1313
const THREAD_NAME: &str = "http-server";
1414

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
pub mod cli;
2-
pub mod config;
3-
pub mod plugin;
4-
pub mod server;
52

63
use anyhow::Result;
74
use clap::Parser;

crates/http-server/src/config.rs

Lines changed: 0 additions & 10 deletions
This file was deleted.

crates/http-server/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod server;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use std::net::IpAddr;
2+
use std::str::FromStr;
3+
4+
use anyhow::{bail, Error, Result};
5+
6+
#[derive(Clone, Debug)]
7+
pub struct Config {
8+
/// The IP address to bind to.
9+
pub host: IpAddr,
10+
/// The port to bind to.
11+
pub port: u16,
12+
/// Enable CORS with a permissive policy.
13+
pub cors: bool,
14+
}
15+
16+
#[derive(Clone, Debug)]
17+
pub struct BasicAuth {
18+
pub username: String,
19+
pub password: String,
20+
}
21+
22+
impl FromStr for BasicAuth {
23+
type Err = Error;
24+
25+
fn from_str(s: &str) -> Result<Self> {
26+
let parts = s.split(":").collect::<Vec<&str>>();
27+
28+
if parts.len() != 2 {
29+
bail!("Expected a string with a colon to separe username and password for Basic Authentication.");
30+
}
31+
32+
Ok(BasicAuth {
33+
username: parts[0].into(),
34+
password: parts[1].into(),
35+
})
36+
}
37+
}

0 commit comments

Comments
 (0)