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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use super::{
actix::{
dev::{ServiceRequest, ServiceResponse},
middleware::{Condition, NormalizePath},
App, Error, HttpServer,
Scope
},
cors::Cors,
default_routes::static_files,
logger::{init_logger, RapidLogger},
tui::server_init,
shift::generate::create_typescript_types
};
use actix_http::{body::MessageBody, Request, Response};
use actix_service::{IntoServiceFactory, ServiceFactory};
use actix_web::dev::AppConfig;
use lazy_static::lazy_static;
use rapid_cli::rapid_config::config::{find_rapid_config, RapidConfig};
use rapid_web_codegen::routes;
extern crate proc_macro;
#[derive(Clone)]
pub struct RapidServer {
pub port: Option<u16>,
pub base_route: Option<String>,
pub hostname: Option<String>,
}
lazy_static! {
static ref RAPID_SERVER_CONFIG: RapidConfig = find_rapid_config();
}
impl RapidServer {
pub fn create(port: Option<u16>, base_route: Option<String>, hostname: Option<String>) -> Self {
Self { port, base_route, hostname }
}
pub fn router(
cors: Option<Cors>,
log_type: Option<RapidLogger>,
) -> App<impl ServiceFactory<ServiceRequest, Response = ServiceResponse<impl MessageBody>, Config = (), InitError = (), Error = Error>> {
let is_logging = match RAPID_SERVER_CONFIG.server.as_ref() {
Some(value) => match value.is_logging {
Some(log_value) => log_value,
None => true,
},
None => true,
};
let is_serving_static_files = match RAPID_SERVER_CONFIG.server.as_ref() {
Some(value) => match value.serve_static_files {
Some(static_value) => static_value,
None => true,
},
None => true,
};
let config_logging_server = {
if is_logging {
match log_type {
Some(logging_type) => App::new()
.wrap(cors.unwrap_or(Cors::default()))
.wrap(Condition::new(true, logging_type))
.wrap(NormalizePath::trim()),
None => App::new()
.wrap(cors.unwrap_or(Cors::default()))
.wrap(Condition::new(true, RapidLogger::minimal()))
.wrap(NormalizePath::trim()),
}
} else {
App::new()
.wrap(cors.unwrap_or(Cors::default()))
.wrap(Condition::new(false, RapidLogger::minimal()))
.wrap(NormalizePath::trim())
}
};
match is_serving_static_files {
true => config_logging_server.service(static_files::static_files()),
false => config_logging_server,
}
}
pub fn fs_router(cors: Option<Cors>, log_type: Option<RapidLogger>, routes: Scope) -> App<impl ServiceFactory<ServiceRequest, Response = ServiceResponse<impl MessageBody>, Config = (), InitError = (), Error = Error>> {
create_typescript_types(std::env::current_dir().unwrap(), std::env::current_dir().unwrap());
RapidServer::router(cors, log_type).service(routes)
}
pub async fn listen<F, I, S, B>(self, server: HttpServer<F, I, S, B>) -> std::io::Result<()>
where
F: Fn() -> I + Send + Clone + 'static,
I: IntoServiceFactory<S, Request>,
S: ServiceFactory<Request, Config = AppConfig> + 'static,
S::Error: Into<Error>,
S::InitError: std::fmt::Debug,
S::Response: Into<Response<B>>,
B: MessageBody + 'static,
{
init_logger();
let bind_config = get_default_bind_config(RAPID_SERVER_CONFIG.clone(), self.hostname, self.port);
server_init(bind_config.clone());
server.bind(bind_config)?.run().await
}
}
fn get_default_bind_config(config: RapidConfig, host_name: Option<String>, port: Option<u16>) -> (String, u16) {
let server_hostname = match host_name {
Some(value) => value,
None => String::from("localhost"),
};
let fallback_port = match port {
Some(value) => value,
None => 8080,
};
let port = match config.server {
Some(value) => match value.port {
Some(port) => port,
None => fallback_port,
},
None => fallback_port,
};
(server_hostname, port)
}