-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
174 lines (156 loc) · 5.38 KB
/
mod.rs
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use std::fs::File;
use std::io::{self, BufReader};
use std::net::SocketAddr;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use axum::body::Body;
use axum::error_handling::HandleErrorLayer;
use axum::extract::{Path, State};
use axum::http::StatusCode;
use axum::middleware::from_extractor_with_state;
use axum::response::{IntoResponse, Response};
use axum::routing::get;
use axum::Router;
use hyper::body::Incoming;
use hyper::Request;
use hyper_util::rt::{TokioExecutor, TokioIo};
use tokio::net::TcpListener;
use tokio_rustls::rustls::ServerConfig;
use tokio_rustls::TlsAcceptor;
use tower::{BoxError, ServiceBuilder, ServiceExt};
use tower_http::compression::CompressionLayer;
use tower_http::services::{ServeDir, ServeFile};
use tower_http::trace::TraceLayer;
use tower_service::Service;
use tracing::{debug, error, info};
use crate::db::AtomicDatabase;
use crate::provider;
use crate::server::auth::Login;
mod auth;
use auth::Auth;
pub use auth::AuthConfig;
mod api;
use api::Project;
pub use api::UserConfig;
/// Start the backend server
pub async fn start(
host: SocketAddr,
domain: &str,
db: AtomicDatabase,
assets: PathBuf,
tls: Tls,
auth: Option<AuthConfig>,
user: Option<UserConfig>,
) {
let tls = tls.load_config().expect("invalid TLS config");
let auth = Auth::new(domain, auth);
let project = Project::new(db, user, auth.clone());
let app = Router::new()
.nest("/auth", auth::routes(auth.clone()))
.nest("/api", api::routes(project.clone()))
.route(
"/",
get(static_index)
.with_state(assets.clone())
.layer(from_extractor_with_state::<Login, Auth>(auth.clone())),
)
.route(
"/*file",
get(static_assets)
.with_state(assets)
.layer(from_extractor_with_state::<Login, Auth>(auth.clone())),
)
.layer(
ServiceBuilder::new()
.layer(CompressionLayer::new())
.layer(HandleErrorLayer::new(|error: BoxError| async move {
if error.is::<tower::timeout::error::Elapsed>() {
Ok(StatusCode::REQUEST_TIMEOUT)
} else {
error!("Internal server error: {error}");
Err(StatusCode::INTERNAL_SERVER_ERROR)
}
}))
.timeout(Duration::from_secs(10))
.layer(TraceLayer::new_for_http())
.into_inner(),
);
debug!("Listening on {host}");
let (_, r) = tokio::join!(auth::background(auth), serve(host, tls, app));
r.unwrap();
}
async fn serve(host: SocketAddr, tls: ServerConfig, app: Router) -> io::Result<()> {
let acceptor = TlsAcceptor::from(Arc::new(tls));
let listener = TcpListener::bind(&host).await.unwrap();
loop {
let (stream, peer) = listener.accept().await?;
let acceptor = acceptor.clone();
let app = app.clone();
tokio::spawn(async move {
let Ok(stream) = acceptor.accept(stream).await else {
info!("tls handshake failed: {peer}");
return;
};
let stream = TokioIo::new(stream);
// Hyper has also its own `Service` trait and doesn't use tower. We can use
// `hyper::service::service_fn` to create a hyper `Service` that calls our app through
// `tower::Service::call`.
let hyper_service = hyper::service::service_fn(move |request: Request<Incoming>| {
// We have to clone `app` because hyper's `Service` uses `&self` whereas
// tower's `Service` requires `&mut self`.
app.clone().call(request)
});
let ret = hyper_util::server::conn::auto::Builder::new(TokioExecutor::new())
.serve_connection_with_upgrades(stream, hyper_service)
.await;
if let Err(err) = ret {
info!("serving failed {peer}: {err}");
}
});
}
}
/// TLS configuration
pub struct Tls {
pub cert: PathBuf,
pub key: PathBuf,
}
impl Tls {
fn load_config(self) -> io::Result<ServerConfig> {
use rustls_pemfile::{certs, pkcs8_private_keys};
let certs = certs(&mut BufReader::new(File::open(self.cert)?)).collect::<Result<_, _>>()?;
let key = pkcs8_private_keys(&mut BufReader::new(File::open(self.key)?))
.next()
.ok_or(io::Error::new(io::ErrorKind::InvalidInput, "invalid key"))??;
rustls::ServerConfig::builder()
.with_no_client_auth()
.with_single_cert(certs, key.into())
.map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))
}
}
async fn static_index(State(dir): State<PathBuf>, req: Request<Body>) -> Response {
ServeFile::new(dir.join("index.html"))
.oneshot(req)
.await
.unwrap()
.into_response()
}
async fn static_assets(
State(dir): State<PathBuf>,
Path(file): Path<String>,
req: Request<Body>,
) -> Response {
if !file.contains('.') {
ServeFile::new(dir.join(file).with_extension("html"))
.oneshot(req)
.await
.unwrap()
.into_response()
} else {
ServeDir::new(dir.clone())
.oneshot(req)
.await
.unwrap()
.into_response()
}
}