1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
mod api;
mod auth;
mod database;
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>,
secret: String,
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
// Errors can get very tough,
// the rust log saved my ass
std::env::set_var("RUST_LOG", "debug");
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: 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())
})
.bind(("127.0.0.1", 8000))?
.run()
.await
}
/* main page*/
async fn root() -> String {
String::from("Server is up and running")
}
|