summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs58
1 files changed, 56 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs
index 9c96c4b..595723a 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -5,8 +5,17 @@ mod extractors;
use actix_web::{web, App, HttpServer};
use dotenv::dotenv;
+use std::env;
use std::sync::Arc;
+use utoipa::{
+ openapi::security::{HttpAuthScheme, HttpBuilder, SecurityScheme},
+ Modify, OpenApi, ToSchema,
+};
+
+use utoipa_swagger_ui::SwaggerUi;
+use database::artist::Artist;
+
#[derive(Clone)]
struct AppState {
database: Arc<database::DatabaseWrapper>,
@@ -21,21 +30,65 @@ async fn main() -> std::io::Result<()> {
env_logger::init();
dotenv().ok();
+ /* create database wrapper (reference: acsim) */
let db_raw = match database::DatabaseWrapper::new().await {
Ok(res) => res,
Err(_) => panic!("Error creating database wrapper"),
};
-
let db = Arc::new(db_raw);
+
+ /* get jwt secret from env */
+ let jwt_secret = env::var("SECRET")
+ .expect("environment variable SECRET is *probably not setted up!!")
+ .to_string();
+
+ /* application data struct */
let app_state = AppState {
database: db,
- secret: "secret".to_owned(),
+ secret: jwt_secret,
};
+ /* utoipa setup */
+ #[derive(OpenApi)]
+ #[openapi(
+ paths(
+ ),
+ components(
+ schemas(
+ Artist
+ )
+ ),
+ modifiers(&SecurityAddon)
+ )]
+ struct ApiDoc;
+
+ struct SecurityAddon;
+ impl Modify for SecurityAddon {
+ fn modify(&self, openapi : &mut utoipa::openapi::OpenApi) {
+ let components = openapi.components.as_mut().unwrap();
+ components.add_security_scheme(
+ "bearer_auth",
+ SecurityScheme::Http(
+ HttpBuilder::new()
+ .scheme(HttpAuthScheme::Bearer)
+ .bearer_format("JWT")
+ .build()
+ ),
+ );
+ }
+ }
+
+ let openapi = ApiDoc::openapi();
+
+ /* main server setup */
HttpServer::new(move || {
App::new()
.app_data(web::Data::new(app_state.clone()))
.route("/", web::get().to(root))
+ .service(SwaggerUi::new("/docs/{_:.*}").url(
+ "/docs/openapi.json",
+ openapi.clone(),
+ ))
.service(api::api_scope())
.service(auth::auth_scope())
})
@@ -44,6 +97,7 @@ async fn main() -> std::io::Result<()> {
.await
}
+/* main page*/
async fn root() -> String {
String::from("Server is up and running")
}