-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added hostinfo route and route utils
- Loading branch information
1 parent
a6fdf19
commit 4bafa1e
Showing
7 changed files
with
130 additions
and
15 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
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use std::io; | ||
use actix_web::{HttpResponse, body::MessageBody}; | ||
use actix_web::http::header::{ContentType, CONTENT_LENGTH}; | ||
use crate::{Config, Hostinfo}; | ||
|
||
/// Hostinfo from Config | ||
/// | ||
/// Attempts to create a [HttpResponse] from a generated [Hostinfo] by a [Config]. | ||
pub fn hostinfo_json(cfg: &Config) -> io::Result<HttpResponse> { | ||
// Generate Hostinfo | ||
let hostinfo = Hostinfo::generate(cfg)?; | ||
hostinfo_data(&hostinfo) | ||
} | ||
|
||
/// Hostinfo from Data | ||
/// | ||
/// Attempts to create a [HttpResponse] from a [Hostinfo] struct. | ||
pub fn hostinfo_data(hstinfo: &Hostinfo) -> io::Result<HttpResponse> { | ||
// Convert to String | ||
let raw_json = serde_json::to_string(hstinfo)?; | ||
|
||
// Return HttpReponse | ||
Ok(HttpResponse::Ok() | ||
.content_type(ContentType::json()) | ||
.append_header( | ||
(CONTENT_LENGTH, raw_json.len()) | ||
) | ||
.body(raw_json)) | ||
} | ||
|
||
/// HTTP Status 500 | ||
/// | ||
/// Returns a 500 Error with an optional body message | ||
pub fn server_500(msg: Option<impl MessageBody + 'static>) -> HttpResponse { | ||
if let Some(message) = msg { | ||
return HttpResponse::InternalServerError() | ||
.body(message) | ||
} | ||
HttpResponse::InternalServerError() | ||
.finish() | ||
} |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use std::time::Instant; | ||
use actix_web::{web, Responder, HttpRequest}; | ||
use super::{CyrkensiaState, responses, uri_noquery}; | ||
use crate::Hostinfo; | ||
|
||
/// Hostinfo Route | ||
/// | ||
/// Route for serving a [Hostinfo]. Server needs [CyrkensiaState] in `.app_data()` for this. | ||
pub async fn hostinfo(req: HttpRequest, data: web::Data<CyrkensiaState>) -> impl Responder { | ||
// Get config | ||
let Some(delay) = data.config.max_age else { | ||
// Ad hoch Hostinfo | ||
let Ok(resp) = responses::hostinfo_json(&data.config) else { | ||
return responses::server_500(Some("Failed to generate hostinfo")); | ||
}; | ||
return resp; | ||
}; | ||
|
||
// Get last update timestamp and cached hostinfo | ||
let Ok(mut last_updated) = data.last_updated.lock() else { | ||
return responses::server_500(None::<String>); | ||
}; | ||
let Ok(mut hostinfo) = data.hostinfo.lock() else { | ||
return responses::server_500(None::<String>); | ||
}; | ||
|
||
if last_updated.elapsed().as_secs() >= delay { | ||
// Generate new Hostinfo if expired | ||
let Ok(new_hostinfo) = Hostinfo::generate(&data.config) else { | ||
return responses::server_500(Some("Failed to update hostinfo")); | ||
}; | ||
|
||
// Update Hostinfo and Timestamp | ||
*hostinfo = new_hostinfo; | ||
*last_updated = Instant::now(); | ||
} | ||
|
||
// Set Origin URL | ||
let mut final_hostinfo = hostinfo.clone(); | ||
final_hostinfo.set_origin(uri_noquery(req.uri())); | ||
|
||
// Return final result | ||
let Ok(finalres) = responses::hostinfo_data(&final_hostinfo) else { | ||
return responses::server_500(Some("Failed to generate hostinfo")); | ||
}; | ||
finalres | ||
} |