39 lines
1.0 KiB
Rust
39 lines
1.0 KiB
Rust
use axum::response::IntoResponse;
|
|
use axum::{
|
|
http::StatusCode,
|
|
routing::{get, post},
|
|
Extension, Json, Router,
|
|
};
|
|
use chrono::TimeDelta;
|
|
use std::net::IpAddr;
|
|
|
|
use crate::worker::Worker;
|
|
use crate::AppError;
|
|
|
|
async fn sync(Extension(worker): Extension<Worker>) -> Result<impl IntoResponse, AppError> {
|
|
worker.update(TimeDelta::days(30)).await?;
|
|
|
|
Ok("Ok")
|
|
}
|
|
|
|
pub async fn serve(worker: Worker, ip: IpAddr, port: u16) -> Result<(), AppError> {
|
|
// build our application with a route
|
|
let app = Router::new()
|
|
.route("/health", get(|| async { "Ok" }))
|
|
.route("/sync", post(sync))
|
|
.layer(Extension(worker));
|
|
|
|
// run our app with hyper, listening globally on port 3000
|
|
let listener = tokio::net::TcpListener::bind((ip, port)).await?;
|
|
|
|
axum::serve(listener, app)
|
|
.with_graceful_shutdown(async {
|
|
tokio::signal::ctrl_c()
|
|
.await
|
|
.expect("Failed to install CTRL+C signal handler");
|
|
})
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|