Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
feat: openapi
Browse files Browse the repository at this point in the history
  • Loading branch information
ralvescosta committed Feb 20, 2023
1 parent 6b646e6 commit da08ac9
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 21 deletions.
195 changes: 195 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions httpw/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ serde_json = { version = "1.0.93" }
tracing = { version = "0.1.37" }
opentelemetry = { version = "0.18.0" }
actix-web-opentelemetry = { version = "0.13.0", features = ["metrics"] }
utoipa = { version = "3.0.2", features = ["actix_extras"] }
utoipa-swagger-ui = { version = "3.0.2", features = ["actix-web"] }
37 changes: 19 additions & 18 deletions httpw/src/server/server.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::types::RouteConfig;
use crate::{errors::HttpServerError, middlewares};
use actix_web::{
http::KeepAlive,
middleware as actix_middleware,
web::{self, Data},
App, HttpResponse, HttpServer, Responder,
Expand All @@ -9,14 +10,16 @@ use actix_web_opentelemetry::{RequestMetricsBuilder, RequestTracing};
use auth::jwt_manager::JwtManager;
use env::AppConfig;
use opentelemetry::global;
use std::sync::Arc;
use std::{sync::Arc, time::Duration};
use tracing::error;
use utoipa::openapi::OpenApi;
use utoipa_swagger_ui::SwaggerUi;

pub struct HttpwServerImpl {
services: Vec<RouteConfig>,
jwt_manager: Option<Arc<dyn JwtManager + Send + Sync>>,
addr: String,
// openapi_file_path: Option<String>,
openapi: Option<OpenApi>,
}

impl HttpwServerImpl {
Expand All @@ -25,7 +28,7 @@ impl HttpwServerImpl {
services: vec![],
addr: cfg.app_addr(),
jwt_manager: None,
// openapi_file_path: None,
openapi: None,
}
}
}
Expand All @@ -41,16 +44,16 @@ impl HttpwServerImpl {
self
}

// pub fn openapi_file_path(mut self, file_path: String) -> Self {
// self.openapi_file_path = Some(file_path);
// self
// }
pub fn openapi(mut self, openapi: &OpenApi) -> Self {
self.openapi = Some(openapi.to_owned());
self
}

pub async fn start(&self) -> Result<(), HttpServerError> {
HttpServer::new({
let services = self.services.to_vec();
let jwt_manager = self.jwt_manager.clone();
// let openapi_file = self.openapi_file_path.clone();
let openapi = self.openapi.clone();

move || {
let meter = global::meter("actix_web");
Expand All @@ -74,22 +77,20 @@ impl HttpwServerImpl {
app = app.configure(svc);
}

// if let Some(_openapi) = openapi_file.clone() {
// let spec = swagger_ui::swagger_spec_file!("../../swagger-ui/examples/openapi.json");
// let config = swagger_ui::Config::default();
//
// let app = app.service(
// scope("/v1/doc")
// .configure(actix_web_swagger_ui::swagger(spec, config))
// );
//
// }
if openapi.is_some() {
app = app.service(
SwaggerUi::new("/docs/{_:.*}")
.url("/docs/openapi.json", openapi.clone().unwrap()),
);
}

app.route("/health", web::get().to(health_handler))
.default_service(web::to(middlewares::not_found::not_found))
.wrap(actix_middleware::Logger::default())
}
})
.shutdown_timeout(60)
.keep_alive(KeepAlive::Timeout(Duration::new(2, 0)))
.bind(&self.addr)
.map_err(|e| {
error!(
Expand Down
6 changes: 3 additions & 3 deletions httpw/src/viewmodels/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::fmt;

use serde::Serialize;
use std::fmt;
use utoipa::ToSchema;

#[derive(Debug, Default, Serialize)]
#[derive(Debug, Default, Serialize, ToSchema)]
pub struct HttpErrorViewModel {
pub status_code: u16,
pub message: String,
Expand Down

0 comments on commit da08ac9

Please sign in to comment.