cryptgeon/packages/backend/src/main.rs

68 lines
1.9 KiB
Rust
Raw Normal View History

2024-08-23 14:27:47 +02:00
use axum::{
2024-08-25 22:19:41 +02:00
extract::{DefaultBodyLimit, Request},
2024-08-23 14:27:47 +02:00
routing::{delete, get, post},
Router, ServiceExt,
};
2021-12-21 00:15:04 +01:00
use dotenv::dotenv;
2024-08-23 14:27:47 +02:00
use tower::Layer;
use tower_http::{
compression::CompressionLayer,
normalize_path::NormalizePathLayer,
services::{ServeDir, ServeFile},
};
2021-05-01 12:39:40 +02:00
#[macro_use]
extern crate lazy_static;
mod config;
mod health;
2021-05-01 12:39:40 +02:00
mod note;
mod status;
2021-05-02 12:32:03 +02:00
mod store;
2021-05-01 12:39:40 +02:00
2024-08-23 14:27:47 +02:00
#[tokio::main]
async fn main() {
2021-12-21 00:15:04 +01:00
dotenv().ok();
if !store::can_reach_redis() {
2024-08-23 14:27:47 +02:00
println!("cannot reach redis");
panic!("canont reach redis");
}
2024-08-23 14:27:47 +02:00
let notes_routes = Router::new()
.route("/", post(note::create))
.route("/:id", delete(note::delete))
.route("/:id", get(note::preview));
let health_routes = Router::new().route("/live", get(health::report_health));
let status_routes = Router::new().route("/status", get(status::get_status));
let api_routes = Router::new()
.nest("/notes", notes_routes)
.nest("/", health_routes)
.nest("/", status_routes);
let index = format!("{}{}", config::FRONTEND_PATH.to_string(), "/index.html");
let serve_dir =
ServeDir::new(config::FRONTEND_PATH.to_string()).not_found_service(ServeFile::new(index));
let app = Router::new()
.nest("/api", api_routes)
.fallback_service(serve_dir)
2024-08-25 22:19:41 +02:00
.layer(DefaultBodyLimit::max(*config::LIMIT))
2024-08-23 14:27:47 +02:00
.layer(
CompressionLayer::new()
.br(true)
.deflate(true)
.gzip(true)
.zstd(true),
2024-08-25 22:19:41 +02:00
);
2024-08-23 14:27:47 +02:00
let app = NormalizePathLayer::trim_trailing_slash().layer(app);
let listener = tokio::net::TcpListener::bind(config::LISTEN_ADDR.to_string())
.await
.unwrap();
println!("listening on {}", listener.local_addr().unwrap());
axum::serve(listener, ServiceExt::<Request>::into_make_service(app))
.await
.unwrap();
2021-05-01 12:39:40 +02:00
}