Skip to content

Commit

Permalink
Utoipa migration (#139)
Browse files Browse the repository at this point in the history
* migrate from okapi to utoipa since its better maintained
  • Loading branch information
Lukas-Heiligenbrunner authored Jan 14, 2025
1 parent da5d612 commit 97cf628
Show file tree
Hide file tree
Showing 15 changed files with 304 additions and 200 deletions.
187 changes: 57 additions & 130 deletions backend/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ anyhow = "1.0.94"
reqwest = { version = "0.12.9", features = ["blocking", "gzip"] }

rocket = "0.5.1"
rocket_okapi = { version = "0.9.0", features = ["swagger"] }
rocket_oauth2 = "0.5.0"
utoipa = { version = "5.3.1", features = ["rocket_extras"] }
utoipa-redoc = { version = "5.0.1", features = ["rocket"] }
utoipa-scalar = { version = "0.2.1", features = ["rocket"] }

dotenvy = "0.15.7"
sea-orm = { version = "1.1.2", features = [ "sqlx-sqlite", "sqlx-postgres", "runtime-tokio-rustls", "macros", "with-bigdecimal" ] }
sea-orm-migration = {version = "1.1.2", features = ["sqlx-sqlite", "runtime-tokio-rustls"]}
Expand Down
18 changes: 14 additions & 4 deletions backend/src/api/aur.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
use crate::aur::api::query_aur;
use rocket::serde::json::Json;

use rocket::get;

use crate::api::types::authenticated::Authenticated;
use crate::api::types::input::ApiPackage;
use rocket_okapi::openapi;
use rocket::get;
use utoipa::OpenApi;

#[derive(OpenApi)]
#[openapi(paths(search))]
pub struct AURApi;

#[openapi(tag = "aur")]
#[utoipa::path(
responses(
(status = 200, description = "Get all todos", body = [ApiPackage]),
),
params(
("query", description = "AUR query"),
)
)]
#[get("/search?<query>")]
pub async fn search(query: &str, _a: Authenticated) -> Result<Json<Vec<ApiPackage>>, String> {
if query.len() < 2 {
Expand Down
15 changes: 15 additions & 0 deletions backend/src/api/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,27 @@ use rocket::get;
use rocket::http::{Cookie, CookieJar, SameSite};
use rocket::response::Redirect;
use rocket_oauth2::{OAuth2, TokenResponse};
use utoipa::OpenApi;

#[derive(OpenApi)]
#[openapi(paths(oauth_login, oauth_callback))]
pub struct AuthApi;

#[utoipa::path(
responses(
(status = 200, description = "Redirect to oidc login endpoint"),
)
)]
#[get("/login")]
pub fn oauth_login(oauth2: OAuth2<()>, cookies: &CookieJar<'_>) -> Redirect {
oauth2.get_redirect(cookies, &["user:email"]).unwrap()
}

#[utoipa::path(
responses(
(status = 200, description = "Oauth callback (called by oidc provider)"),
)
)]
#[get("/auth")]
pub fn oauth_callback(token: TokenResponse<()>, cookies: &CookieJar<'_>) -> Redirect {
debug!("Token: {:?}", token);
Expand Down
5 changes: 2 additions & 3 deletions backend/src/api/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ use crate::api::build::*;
use crate::api::health::*;
use crate::api::package::*;
use crate::api::stats::*;
use rocket::Route;
use rocket_okapi::openapi_get_routes;
use rocket::{routes, Route};

pub fn build_api() -> Vec<Route> {
openapi_get_routes![
routes![
search,
package_list,
package_add_endpoint,
Expand Down
Loading

0 comments on commit 97cf628

Please sign in to comment.