Files
library_updater/src/main.rs
2023-09-24 22:39:54 +02:00

75 lines
1.9 KiB
Rust

#[macro_use]
extern crate lazy_static;
pub mod config;
pub mod types;
pub mod updater;
pub mod utils;
use axum::{http::HeaderMap, routing::post, Router};
use sentry::{integrations::debug_images::DebugImagesIntegration, types::Dsn, ClientOptions};
use std::{net::SocketAddr, str::FromStr};
use tower_http::trace::{self, TraceLayer};
use tracing::log;
use tracing::Level;
use crate::updater::cron_jobs;
async fn update(headers: HeaderMap) -> &'static str {
let config_api_key = config::CONFIG.api_key.clone();
let api_key = match headers.get("Authorization") {
Some(v) => v,
None => return "No api-key!",
};
if config_api_key != api_key.to_str().unwrap() {
return "Wrong api-key!";
}
tokio::spawn(async {
match updater::update().await {
Ok(_) => log::info!("Updated!"),
Err(err) => log::info!("Updater err: {:?}", err),
};
});
"Update started"
}
async fn start_app() {
let app = Router::new().route("/update", post(update)).layer(
TraceLayer::new_for_http()
.make_span_with(trace::DefaultMakeSpan::new().level(Level::INFO))
.on_response(trace::DefaultOnResponse::new().level(Level::INFO)),
);
let addr = SocketAddr::from(([0, 0, 0, 0], 8080));
log::info!("Start webserver...");
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
log::info!("Webserver shutdown...")
}
#[tokio::main]
async fn main() {
tracing_subscriber::fmt()
.with_target(false)
.compact()
.init();
let options = ClientOptions {
dsn: Some(Dsn::from_str(&config::CONFIG.sentry_dsn).unwrap()),
default_integrations: false,
..Default::default()
}
.add_integration(DebugImagesIntegration::new());
let _guard = sentry::init(options);
tokio::join![cron_jobs(), start_app()];
}