Skip to content

Commit 3011834

Browse files
authored
Move authentication middleware to new scope (#14)
This ensure UI is unauthenticated and frontend manages complete authentication for dashboard
1 parent 9fb31a7 commit 3011834

File tree

1 file changed

+49
-43
lines changed

1 file changed

+49
-43
lines changed

server/src/main.rs

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use storage::ObjectStorage;
5151
// Global configurations
5252
const MAX_EVENT_PAYLOAD_SIZE: usize = 102400;
5353
const API_BASE_PATH: &str = "/api";
54-
const API_VERSION: &str = "/v1";
54+
const API_VERSION: &str = "v1";
5555

5656
#[actix_web::main]
5757
async fn main() -> anyhow::Result<()> {
@@ -118,8 +118,7 @@ async fn run_http() -> anyhow::Result<()> {
118118
(_, _) => None,
119119
};
120120

121-
let http_server =
122-
HttpServer::new(move || create_app!().wrap(HttpAuthentication::basic(validator)));
121+
let http_server = HttpServer::new(move || create_app!());
123122
if let Some(builder) = ssl_acceptor {
124123
http_server
125124
.bind_openssl(&CONFIG.parseable.address, builder)?
@@ -135,40 +134,46 @@ async fn run_http() -> anyhow::Result<()> {
135134
pub fn configure_routes(cfg: &mut web::ServiceConfig) {
136135
let generated = generate();
137136

138-
// Base path "{url}/api/v1"
139-
// POST "/query" ==> Get results of the SQL query passed in request body
140-
cfg.service(web::resource(query_path()).route(web::post().to(handlers::event::query)))
141-
.service(
142-
// logstream API
143-
web::resource(logstream_path("{logstream}"))
144-
// PUT "/logstream/{logstream}" ==> Create log stream
145-
.route(web::put().to(handlers::logstream::put))
146-
// POST "/logstream/{logstream}" ==> Post logs to given log stream
147-
.route(web::post().to(handlers::event::post_event))
148-
// DELETE "/logstream/{logstream}" ==> Delete log stream
149-
.route(web::delete().to(handlers::logstream::delete))
150-
.app_data(web::JsonConfig::default().limit(MAX_EVENT_PAYLOAD_SIZE)),
151-
)
152-
.service(
153-
web::resource(alert_path("{logstream}"))
154-
// PUT "/logstream/{logstream}/alert" ==> Set alert for given log stream
155-
.route(web::put().to(handlers::logstream::put_alert))
156-
// GET "/logstream/{logstream}/alert" ==> Get alert for given log stream
157-
.route(web::get().to(handlers::logstream::get_alert)),
158-
)
159-
// GET "/logstream" ==> Get list of all Log Streams on the server
160-
.service(web::resource(logstream_path("")).route(web::get().to(handlers::logstream::list)))
161-
.service(
162-
// GET "/logstream/{logstream}/schema" ==> Get schema for given log stream
163-
web::resource(schema_path("{logstream}"))
164-
.route(web::get().to(handlers::logstream::schema)),
165-
)
166-
// GET "/liveness" ==> Livenss check as per https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/#define-a-liveness-command
167-
.service(web::resource(liveness_path()).route(web::get().to(handlers::liveness)))
168-
// GET "/readiness" ==> Readiness check as per https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/#define-readiness-probes
169-
.service(web::resource(readiness_path()).route(web::get().to(handlers::readiness)))
170-
// GET "/" ==> Serve the static frontend directory
171-
.service(ResourceFiles::new("/", generated));
137+
cfg.service(
138+
// Base path "{url}/api/v1"
139+
web::scope(&base_path())
140+
// POST "/query" ==> Get results of the SQL query passed in request body
141+
.service(web::resource(query_path()).route(web::post().to(handlers::event::query)))
142+
.service(
143+
// logstream API
144+
web::resource(logstream_path("{logstream}"))
145+
// PUT "/logstream/{logstream}" ==> Create log stream
146+
.route(web::put().to(handlers::logstream::put))
147+
// POST "/logstream/{logstream}" ==> Post logs to given log stream
148+
.route(web::post().to(handlers::event::post_event))
149+
// DELETE "/logstream/{logstream}" ==> Delete log stream
150+
.route(web::delete().to(handlers::logstream::delete))
151+
.app_data(web::JsonConfig::default().limit(MAX_EVENT_PAYLOAD_SIZE)),
152+
)
153+
.service(
154+
web::resource(alert_path("{logstream}"))
155+
// PUT "/logstream/{logstream}/alert" ==> Set alert for given log stream
156+
.route(web::put().to(handlers::logstream::put_alert))
157+
// GET "/logstream/{logstream}/alert" ==> Get alert for given log stream
158+
.route(web::get().to(handlers::logstream::get_alert)),
159+
)
160+
// GET "/logstream" ==> Get list of all Log Streams on the server
161+
.service(
162+
web::resource(logstream_path("")).route(web::get().to(handlers::logstream::list)),
163+
)
164+
.service(
165+
// GET "/logstream/{logstream}/schema" ==> Get schema for given log stream
166+
web::resource(schema_path("{logstream}"))
167+
.route(web::get().to(handlers::logstream::schema)),
168+
)
169+
// GET "/liveness" ==> Livenss check as per https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/#define-a-liveness-command
170+
.service(web::resource(liveness_path()).route(web::get().to(handlers::liveness)))
171+
// GET "/readiness" ==> Readiness check as per https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/#define-readiness-probes
172+
.service(web::resource(readiness_path()).route(web::get().to(handlers::readiness)))
173+
.wrap(HttpAuthentication::basic(validator)),
174+
)
175+
// GET "/" ==> Serve the static frontend directory
176+
.service(ResourceFiles::new("/", generated));
172177
}
173178

174179
#[macro_export]
@@ -188,26 +193,27 @@ macro_rules! create_app {
188193
}
189194

190195
fn base_path() -> String {
191-
format!("{}{}", API_BASE_PATH, API_VERSION)
196+
format!("{}/{}", API_BASE_PATH, API_VERSION)
192197
}
193198

194199
fn logstream_path(stream_name: &str) -> String {
195200
if stream_name.is_empty() {
196-
return format!("{}/logstream", base_path());
201+
"/logstream".to_string()
202+
} else {
203+
format!("/logstream/{}", stream_name)
197204
}
198-
format!("{}/logstream/{}", base_path(), stream_name)
199205
}
200206

201207
fn readiness_path() -> String {
202-
format!("{}/readiness", base_path())
208+
"/readiness".to_string()
203209
}
204210

205211
fn liveness_path() -> String {
206-
format!("{}/liveness", base_path())
212+
"/liveness".to_string()
207213
}
208214

209215
fn query_path() -> String {
210-
format!("{}/query", base_path())
216+
"/query".to_string()
211217
}
212218

213219
fn alert_path(stream_name: &str) -> String {

0 commit comments

Comments
 (0)