Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move authentication middleware to new scope #14

Merged
merged 3 commits into from
Aug 6, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 49 additions & 43 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use storage::ObjectStorage;
// Global configurations
const MAX_EVENT_PAYLOAD_SIZE: usize = 102400;
const API_BASE_PATH: &str = "/api";
const API_VERSION: &str = "/v1";
const API_VERSION: &str = "v1";

#[actix_web::main]
async fn main() -> anyhow::Result<()> {
Expand Down Expand Up @@ -118,8 +118,7 @@ async fn run_http() -> anyhow::Result<()> {
(_, _) => None,
};

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

// Base path "{url}/api/v1"
// POST "/query" ==> Get results of the SQL query passed in request body
cfg.service(web::resource(query_path()).route(web::post().to(handlers::event::query)))
.service(
// logstream API
web::resource(logstream_path("{logstream}"))
// PUT "/logstream/{logstream}" ==> Create log stream
.route(web::put().to(handlers::logstream::put))
// POST "/logstream/{logstream}" ==> Post logs to given log stream
.route(web::post().to(handlers::event::post_event))
// DELETE "/logstream/{logstream}" ==> Delete log stream
.route(web::delete().to(handlers::logstream::delete))
.app_data(web::JsonConfig::default().limit(MAX_EVENT_PAYLOAD_SIZE)),
)
.service(
web::resource(alert_path("{logstream}"))
// PUT "/logstream/{logstream}/alert" ==> Set alert for given log stream
.route(web::put().to(handlers::logstream::put_alert))
// GET "/logstream/{logstream}/alert" ==> Get alert for given log stream
.route(web::get().to(handlers::logstream::get_alert)),
)
// GET "/logstream" ==> Get list of all Log Streams on the server
.service(web::resource(logstream_path("")).route(web::get().to(handlers::logstream::list)))
.service(
// GET "/logstream/{logstream}/schema" ==> Get schema for given log stream
web::resource(schema_path("{logstream}"))
.route(web::get().to(handlers::logstream::schema)),
)
// GET "/liveness" ==> Livenss check as per https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/#define-a-liveness-command
.service(web::resource(liveness_path()).route(web::get().to(handlers::liveness)))
// GET "/readiness" ==> Readiness check as per https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/#define-readiness-probes
.service(web::resource(readiness_path()).route(web::get().to(handlers::readiness)))
// GET "/" ==> Serve the static frontend directory
.service(ResourceFiles::new("/", generated));
cfg.service(
// Base path "{url}/api/v1"
web::scope(&base_path())
Copy link
Contributor Author

@trueleo trueleo Aug 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nitisht base path is used inside this web::scope thus every path grouped underneath it will share same base url i.e /api/v1. I changed the related functions to not return full path.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah okay..we need to test all the APIs then, just to make sure nothing is broken

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep

// POST "/query" ==> Get results of the SQL query passed in request body
.service(web::resource(query_path()).route(web::post().to(handlers::event::query)))
.service(
// logstream API
web::resource(logstream_path("{logstream}"))
// PUT "/logstream/{logstream}" ==> Create log stream
.route(web::put().to(handlers::logstream::put))
// POST "/logstream/{logstream}" ==> Post logs to given log stream
.route(web::post().to(handlers::event::post_event))
// DELETE "/logstream/{logstream}" ==> Delete log stream
.route(web::delete().to(handlers::logstream::delete))
.app_data(web::JsonConfig::default().limit(MAX_EVENT_PAYLOAD_SIZE)),
)
.service(
web::resource(alert_path("{logstream}"))
// PUT "/logstream/{logstream}/alert" ==> Set alert for given log stream
.route(web::put().to(handlers::logstream::put_alert))
// GET "/logstream/{logstream}/alert" ==> Get alert for given log stream
.route(web::get().to(handlers::logstream::get_alert)),
)
// GET "/logstream" ==> Get list of all Log Streams on the server
.service(
web::resource(logstream_path("")).route(web::get().to(handlers::logstream::list)),
)
.service(
// GET "/logstream/{logstream}/schema" ==> Get schema for given log stream
web::resource(schema_path("{logstream}"))
.route(web::get().to(handlers::logstream::schema)),
)
// GET "/liveness" ==> Livenss check as per https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/#define-a-liveness-command
.service(web::resource(liveness_path()).route(web::get().to(handlers::liveness)))
// GET "/readiness" ==> Readiness check as per https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/#define-readiness-probes
.service(web::resource(readiness_path()).route(web::get().to(handlers::readiness)))
.wrap(HttpAuthentication::basic(validator)),
)
// GET "/" ==> Serve the static frontend directory
.service(ResourceFiles::new("/", generated));
}

#[macro_export]
Expand All @@ -188,26 +193,27 @@ macro_rules! create_app {
}

fn base_path() -> String {
format!("{}{}", API_BASE_PATH, API_VERSION)
format!("{}/{}", API_BASE_PATH, API_VERSION)
}

fn logstream_path(stream_name: &str) -> String {
if stream_name.is_empty() {
return format!("{}/logstream", base_path());
"/logstream".to_string()
} else {
format!("/logstream/{}", stream_name)
}
format!("{}/logstream/{}", base_path(), stream_name)
}

fn readiness_path() -> String {
format!("{}/readiness", base_path())
"/readiness".to_string()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How will it get the base path?

}

fn liveness_path() -> String {
format!("{}/liveness", base_path())
"/liveness".to_string()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

}

fn query_path() -> String {
format!("{}/query", base_path())
"/query".to_string()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

}

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