-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
14 changed files
with
20 additions
and
896 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,103 +1,21 @@ | ||
use std::io; | ||
use std::net::SocketAddr; | ||
use std::sync::Arc; | ||
|
||
use actix_web::http::header; | ||
use actix_web::{dev, guard, middleware, web, App, HttpServer}; | ||
use alerion_datamodel::webserver::SystemOptions; | ||
use utils::bearer_auth::BearerAuth; | ||
|
||
use crate::config::AlerionConfig; | ||
use crate::servers::ServerPool; | ||
use warp::Filter; | ||
|
||
const ALLOWED_HEADERS: &str = "Accept, Accept-Encoding, Authorization, Cache-Control, Content-Type, Content-Length, Origin, X-Real-IP, X-CSRF-Token"; | ||
const ALLOWED_METHODS: &str = "GET, POST, PATCH, PUT, DELETE, OPTIONS"; | ||
|
||
fn default_headers(config: &AlerionConfig) -> middleware::DefaultHeaders { | ||
middleware::DefaultHeaders::new() | ||
.add((header::ACCESS_CONTROL_ALLOW_ORIGIN, config.remote.clone())) | ||
.add((header::ACCESS_CONTROL_MAX_AGE, 7200)) | ||
.add((header::ACCESS_CONTROL_ALLOW_HEADERS, ALLOWED_HEADERS)) | ||
.add((header::ACCESS_CONTROL_ALLOW_METHODS, ALLOWED_METHODS)) | ||
.add((header::ACCESS_CONTROL_ALLOW_CREDENTIALS, "true")) | ||
} | ||
|
||
pub struct Webserver { | ||
server_fut: dev::Server, | ||
} | ||
|
||
impl Webserver { | ||
/// Build the webserver. May block if a DNS lookup is required to resolve the host | ||
/// set in the configuration. | ||
pub fn make(config: AlerionConfig, server_pool: Arc<ServerPool>) -> io::Result<Self> { | ||
let moved_out_config = config.clone(); | ||
|
||
let http_server = HttpServer::new(move || { | ||
let config = moved_out_config.clone(); | ||
let token = &config.auth.token; | ||
|
||
let base_system_options = SystemOptions { | ||
architecture: "amd64", | ||
cpu_count: 8, | ||
kernel_version: "5.14.0-362.8.1.el9_3.x86_64", | ||
os: "linux", | ||
version: "1.11.11", | ||
}; | ||
|
||
App::new() | ||
.app_data(web::Data::new(base_system_options)) | ||
.app_data(web::Data::new(config.clone())) | ||
.app_data(web::Data::new(Arc::clone(&server_pool))) | ||
.wrap(default_headers(&config)) | ||
.wrap(utils::camel_case::CamelCaseHeaders) | ||
.wrap(middleware::Logger::new("%r")) | ||
.route("/", web::get().to(router::root)) | ||
.service({ | ||
use router::api; | ||
|
||
web::scope("/api") | ||
.route( | ||
"servers", | ||
web::post() | ||
.wrap(BearerAuth::new(token.clone())) | ||
.to(api::servers_post), | ||
) | ||
.route( | ||
"system", | ||
web::get() | ||
.wrap(BearerAuth::new(token.clone())) | ||
.to(api::system_get), | ||
) | ||
.route( | ||
"system", | ||
web::route().guard(guard::Options()).to(api::system_options), | ||
) | ||
.route( | ||
"update", | ||
web::post() | ||
.wrap(BearerAuth::new(token.clone())) | ||
.to(api::update_post), | ||
) | ||
.service(web::scope("/servers/{id}").route("ws", web::get().to(api::ws))) | ||
}) | ||
}); | ||
|
||
let ip = config.api.host; | ||
let port = config.api.port; | ||
|
||
let server_fut = http_server | ||
.worker_max_blocking_threads(16) | ||
.workers(1) | ||
.bind(SocketAddr::new(ip, port))? | ||
.run(); | ||
|
||
Ok(Webserver { server_fut }) | ||
} | ||
|
||
pub async fn serve(self) -> io::Result<()> { | ||
self.server_fut.await | ||
} | ||
//fn default_headers(config: &AlerionConfig) -> middleware::DefaultHeaders { | ||
//middleware::DefaultHeaders::new() | ||
//.add((header::ACCESS_CONTROL_ALLOW_ORIGIN, config.remote.clone())) | ||
//.add((header::ACCESS_CONTROL_MAX_AGE, 7200)) | ||
//.add((header::ACCESS_CONTROL_ALLOW_HEADERS, ALLOWED_HEADERS)) | ||
//.add((header::ACCESS_CONTROL_ALLOW_METHODS, ALLOWED_METHODS)) | ||
//.add((header::ACCESS_CONTROL_ALLOW_CREDENTIALS, "true")) | ||
//} | ||
|
||
pub async fn serve(address: impl Into<SocketAddr>) { | ||
warp::serve(warp::path!("test" / String).map(|name| format!("Hello {}!", name))) | ||
.run(address) | ||
.await | ||
} | ||
|
||
pub mod router; | ||
pub mod utils; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.