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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use actix_files::{Files, NamedFile};
use actix_web::dev::{fn_service, ServiceRequest, ServiceResponse};
use actix_web::{get, web, App, HttpResponse, HttpServer, Responder, Result};
use config::Config;
use log::{error, info};
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
use serde::Serialize;
use std::process;
use std::process::Command;
use database::repository;
mod api;
mod email;
#[derive(Serialize)]
pub struct Response {
pub message: String,
}
#[get("/health")]
async fn healthcheck() -> impl Responder {
let response = Response {
message: "Everything is working fine".to_string(),
};
HttpResponse::Ok().json(response)
}
#[get("/shutdown")]
async fn shutdown_server() -> impl Responder {
let kill_results = Command::new("kill")
.args(["-2".to_string(), process::id().to_string()])
.spawn();
match kill_results {
Ok(_) => {
let response = Response {
message: "Shutdown everything".to_string(),
};
HttpResponse::Ok().json(response)
}
Err(_) => {
let response = Response {
message: "Can't Shutdown everything".to_string(),
};
HttpResponse::InternalServerError().json(response)
}
}
}
async fn not_found() -> Result<HttpResponse> {
error!("Invalid page load");
let response = Response {
message: "Resource not found".to_string(),
};
Ok(HttpResponse::NotFound().json(response))
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let server_config = Config::builder()
.add_source(config::File::with_name("server.config"))
.build()
.expect("Missing Server Config File");
let secret_config = Config::builder()
.add_source(config::File::with_name("secret.config"))
.build()
.expect("Missing Secret Config File");
if std::env::var_os("RUST_LOG").is_none() {
std::env::set_var("RUST_LOG", "info");
}
env_logger::init();
info!("Start of web server");
let todo_db = repository::db_connector::Database::new();
let app_data = web::Data::new((todo_db, server_config.clone(), secret_config.clone()));
let port = server_config
.get("port")
.expect("Missing Port in Server Config");
info!("Using port: {}", port);
let mut use_ssl: bool = server_config
.get("SSL_ENABLE")
.expect("Missing \"SSL_ENABLE\" value");
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls())
.expect("Unable to setup ssl builder (NOTE: needed with and without ssl)");
if use_ssl {
let ssl_certificate_file_path: String = server_config
.get("SSL_CERTIFICATE")
.expect("Missing \"SSL_CERTIFICATE\" value");
let ssl_key_file_path: String = server_config
.get("SSL_KEY")
.expect("Missing \"SSL_KEY\" value");
info!("Getting SSL");
let key_results = builder.set_private_key_file(ssl_key_file_path, SslFiletype::PEM);
match key_results {
Ok(_) => {
let cert_results = builder.set_certificate_chain_file(ssl_certificate_file_path);
match cert_results {
Ok(_) => {}
Err(_) => {
error!("Can't read ssl certificate chain file");
use_ssl = false;
}
}
}
Err(_) => {
error!("Can't read ssl key file");
use_ssl = false;
}
}
}
let server_builder = HttpServer::new(move || {
App::new()
.app_data(app_data.clone())
.configure(api::api_services::config)
.service(healthcheck)
.service(shutdown_server) .service(
Files::new("/studentpage", "./studentpage/dist/")
.index_file("index.html")
.default_handler(fn_service(|req: ServiceRequest| async {
let (req, _) = req.into_parts();
let file = NamedFile::open_async("./studentpage/dist/index.html").await?;
let res = file.into_response(&req);
Ok(ServiceResponse::new(req, res))
})),
)
.service(
Files::new("/adminpage", "./adminpage/dist/")
.index_file("index.html")
.default_handler(fn_service(|req: ServiceRequest| async {
let (req, _) = req.into_parts();
let file = NamedFile::open_async("./adminpage/dist/index.html").await?;
let res = file.into_response(&req);
Ok(ServiceResponse::new(req, res))
})),
)
.default_service(Files::new("/", "./res/").default_handler(web::route().to(not_found)))
.wrap(actix_web::middleware::Logger::default())
});
let server = if use_ssl {
info!("Running using ssl (https)");
server_builder.bind_openssl(format!("0.0.0.0:{}", port), builder)?
} else {
info!("Running using no ssl (http)");
server_builder.bind(("0.0.0.0", port))?
};
server.run().await
}