Last active
December 14, 2020 21:44
-
-
Save kumo/16cd6f6e1b500a02928606128fee87e0 to your computer and use it in GitHub Desktop.
Rust version of a simple monitoring web app
This file contains hidden or 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
use actix_web::{get, web, App, HttpResponse, HttpServer, Responder}; | |
use std::fs; | |
use std::time::SystemTime; | |
use std::env; | |
use dotenv::dotenv; | |
// This struct represents state | |
struct AppState { | |
token: String, | |
alert_chat_id: i32, | |
status_chat_id: i32, | |
} | |
#[get("/hello")] | |
async fn hello() -> impl Responder { | |
// Create or recreate the status file and give an error otherwise | |
let _file = fs::File::create("status").expect("Couldn't create status file"); | |
// Show some text to the user, but since this wep app will be | |
// called by a script, it doesn't matter what we write | |
HttpResponse::Ok().body("Hello to you too!") | |
} | |
#[get("/check/{minutes}")] | |
async fn check(data: web::Data<AppState>, web::Path(minutes): web::Path<u64>) -> impl Responder { | |
// Get some information about the file | |
let metadata = fs::metadata("status").expect("Couldn't find status file"); | |
// Get the last modified time | |
let modified_time = metadata.modified().expect("Couldn't get the last modified time for the status file"); | |
// Get the current time | |
let now = SystemTime::now(); | |
// Calculate how much time has passed | |
let difference = now.duration_since(modified_time).expect("Was the file created in the future?"); | |
// Convert the requested minutes into seconds | |
let seconds = minutes * 60; | |
// Convert the difference into seconds | |
let difference = difference.as_secs(); | |
if difference < seconds { | |
"It has said hello recently" | |
} else if difference < seconds * 2 { | |
telegram_notifyrs::send_message("It is offline!".to_string(), &data.token, data.alert_chat_id); | |
telegram_notifyrs::send_message("It is offline!".to_string(), &data.token, data.status_chat_id); | |
"Offline and I should send a Telegram message" | |
} else { | |
telegram_notifyrs::send_message("It is offline!".to_string(), &data.token, data.status_chat_id); | |
"Still offline. Should I send a message?" | |
} | |
} | |
#[actix_web::main] | |
async fn main() -> std::io::Result<()> { | |
dotenv().ok(); | |
for (key, value) in env::vars() { | |
println!("{}: {}", key, value); | |
} | |
let token = env::var("TELEGRAM_BOT_TOKEN").expect("TELEGRAM_BOT_TOKEN not set"); | |
let alert_chat_id: i32 = env::var("TELEGRAM_ALERT_CHAT_ID") | |
.expect("Missing TELEGRAM_ALERT_CHAT_ID environment variable") | |
.parse() | |
.expect("Error parsing TELEGRAM_ALERT_CHAT_ID as i32"); | |
let status_chat_id: i32 = env::var("TELEGRAM_STATUS_CHAT_ID") | |
.expect("Missing TELEGRAM_STATUS_CHAT_ID environment variable") | |
.parse() | |
.expect("Error parsing TELEGRAM_STATUS_CHAT_ID as i32"); | |
HttpServer::new(move || { | |
App::new() | |
.data(AppState { | |
token: token.clone(), // is it correct that I have to clone the string? | |
alert_chat_id, | |
status_chat_id, | |
}) | |
.service(hello) | |
.service(check) | |
}) | |
.bind("127.0.0.1:8080")? | |
.run() | |
.await | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment