cryptgeon/packages/backend/src/main.rs

68 lines
1.9 KiB
Rust
Raw Normal View History

2024-08-23 12:27:47 +00:00
use axum::{
2024-08-25 20:19:41 +00:00
extract::{DefaultBodyLimit, Request},
2024-08-23 12:27:47 +00:00
routing::{delete, get, post},
Router, ServiceExt,
};
2021-12-20 23:15:04 +00:00
use dotenv::dotenv;
2024-08-23 12:27:47 +00:00
use tower::Layer;
use tower_http::{
compression::CompressionLayer,
normalize_path::NormalizePathLayer,
services::{ServeDir, ServeFile},
};
2021-05-01 10:39:40 +00:00
#[macro_use]
extern crate lazy_static;
mod config;
mod health;
2021-05-01 10:39:40 +00:00
mod note;
mod status;
2021-05-02 10:32:03 +00:00
mod store;
2021-05-01 10:39:40 +00:00
2024-08-23 12:27:47 +00:00
#[tokio::main]
async fn main() {
2021-12-20 23:15:04 +00:00
dotenv().ok();
if !store::can_reach_redis() {
2024-08-23 12:27:47 +00:00
println!("cannot reach redis");
panic!("canont reach redis");
}
2024-08-23 12:27:47 +00: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 20:19:41 +00:00
.layer(DefaultBodyLimit::max(*config::LIMIT))
2024-08-23 12:27:47 +00:00
.layer(
CompressionLayer::new()
.br(true)
.deflate(true)
.gzip(true)
.zstd(true),
2024-08-25 20:19:41 +00:00
);
2024-08-23 12:27:47 +00: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 10:39:40 +00:00
}