Skip to content

Commit 560019d

Browse files
authored
Merge pull request #3169 from spinframework/separate-routes-crate
Move router to its own separate crate
2 parents dae8ca3 + 4abca82 commit 560019d

File tree

6 files changed

+70
-33
lines changed

6 files changed

+70
-33
lines changed

Cargo.lock

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

crates/http/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ routefinder = "0.5.4"
1515
serde = { workspace = true }
1616
spin-app = { path = "../app", optional = true }
1717
spin-factor-outbound-http = { path = "../factor-outbound-http" }
18+
spin-http-routes = { path = "../routes" }
1819
tracing = { workspace = true }
1920
wasmtime = { workspace = true }
2021
wasmtime-wasi = { workspace = true }

crates/http/src/config.rs

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use serde::{Deserialize, Serialize};
2+
use spin_http_routes::HttpTriggerRouteConfig;
23

34
/// Configuration for the HTTP trigger
45
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
@@ -13,34 +14,6 @@ pub struct HttpTriggerConfig {
1314
pub executor: Option<HttpExecutorType>,
1415
}
1516

16-
/// An HTTP trigger route
17-
#[derive(Clone, Debug, Deserialize, Serialize)]
18-
#[serde(untagged)]
19-
pub enum HttpTriggerRouteConfig {
20-
Route(String),
21-
Private(HttpPrivateEndpoint),
22-
}
23-
24-
/// Indicates that a trigger is a private endpoint (not routable).
25-
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
26-
#[serde(deny_unknown_fields)]
27-
pub struct HttpPrivateEndpoint {
28-
/// Whether the private endpoint is private. This must be true.
29-
pub private: bool,
30-
}
31-
32-
impl Default for HttpTriggerRouteConfig {
33-
fn default() -> Self {
34-
Self::Route(Default::default())
35-
}
36-
}
37-
38-
impl<T: Into<String>> From<T> for HttpTriggerRouteConfig {
39-
fn from(value: T) -> Self {
40-
Self::Route(value.into())
41-
}
42-
}
43-
4417
/// The executor for the HTTP component.
4518
/// The component can either implement the Spin HTTP interface,
4619
/// the `wasi-http` interface, or the Wagi CGI interface.

crates/http/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ pub use wasmtime_wasi_http::body::HyperIncomingBody as Body;
33

44
pub mod app_info;
55
pub mod config;
6-
pub mod routes;
76
pub mod trigger;
87
#[cfg(feature = "runtime")]
98
pub mod wagi;
109

11-
pub const WELL_KNOWN_PREFIX: &str = "/.well-known/spin/";
10+
pub use spin_http_routes as routes;
11+
pub use spin_http_routes::WELL_KNOWN_PREFIX;
1212

1313
#[cfg(feature = "runtime")]
1414
pub mod body {

crates/routes/Cargo.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "spin-http-routes"
3+
version.workspace = true
4+
authors.workspace = true
5+
edition.workspace = true
6+
license.workspace = true
7+
homepage.workspace = true
8+
repository.workspace = true
9+
rust-version.workspace = true
10+
11+
[dependencies]
12+
anyhow = { workspace = true }
13+
indexmap = { workspace = true }
14+
percent-encoding = "2"
15+
routefinder = "0.5.4"
16+
serde = { workspace = true }
17+
tracing = { workspace = true }
18+
19+
[lints]
20+
workspace = true

crates/http/src/routes.rs renamed to crates/routes/src/lib.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
use anyhow::{anyhow, Result};
66
use indexmap::IndexMap;
7+
use serde::{Deserialize, Serialize};
78
use std::{borrow::Cow, collections::HashMap, fmt};
89

9-
use crate::config::HttpTriggerRouteConfig;
10+
/// The prefix for well-known routes.
11+
pub const WELL_KNOWN_PREFIX: &str = "/.well-known/spin/";
1012

1113
/// Router for the HTTP trigger.
1214
#[derive(Clone, Debug)]
@@ -383,10 +385,38 @@ fn sanitize<S: Into<String>>(s: S) -> String {
383385
}
384386
}
385387

388+
/// An HTTP trigger route
389+
#[derive(Clone, Debug, Deserialize, Serialize)]
390+
#[serde(untagged)]
391+
pub enum HttpTriggerRouteConfig {
392+
/// A route that is routable.
393+
Route(String),
394+
/// A route that is not routable, but indicates a private endpoint.
395+
Private(HttpPrivateEndpoint),
396+
}
397+
398+
/// Indicates that a trigger is a private endpoint (not routable).
399+
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
400+
#[serde(deny_unknown_fields)]
401+
pub struct HttpPrivateEndpoint {
402+
/// Whether the private endpoint is private. This must be true.
403+
pub private: bool,
404+
}
405+
406+
impl Default for HttpTriggerRouteConfig {
407+
fn default() -> Self {
408+
Self::Route(Default::default())
409+
}
410+
}
411+
412+
impl<T: Into<String>> From<T> for HttpTriggerRouteConfig {
413+
fn from(value: T) -> Self {
414+
Self::Route(value.into())
415+
}
416+
}
417+
386418
#[cfg(test)]
387419
mod route_tests {
388-
use crate::config::HttpPrivateEndpoint;
389-
390420
use super::*;
391421

392422
#[test]

0 commit comments

Comments
 (0)