summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs28
1 files changed, 22 insertions, 6 deletions
diff --git a/src/main.rs b/src/main.rs
index 31271e4..65a32d4 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,11 +3,13 @@ use axum::{
http::StatusCode,
response::IntoResponse,
routing::{get, post, delete},
- Json, Router
+ Json, Router, Extension
};
+use errors::ServiceError;
use serde::{Deserialize, Serialize};
-use std::{net::SocketAddr, time::Duration, str::FromStr};
+use sqlx::{Connection, query, PgConnection, PgPool, postgres::PgPoolOptions};
+use std::{net::SocketAddr, time::Duration, str::FromStr, sync::Arc};
use tower::{BoxError, ServiceBuilder};
use tower_http::trace::TraceLayer;
@@ -18,9 +20,13 @@ mod errors;
mod handlers;
+pub struct State {
+ pub conn: PgPool,
+}
+
#[tokio::main]
async fn main() {
- kankyo::init();
+ kankyo::init().unwrap();
color_eyre::install().unwrap();
tracing_subscriber::registry()
.with(tracing_subscriber::EnvFilter::new(
@@ -30,12 +36,20 @@ async fn main() {
.with(tracing_subscriber::fmt::layer())
.init();
+ let mut conn = PgPoolOptions::new()
+ .max_connections(5)
+ .connect(&std::env::var("DATABASE_URL").unwrap_or("postgres://postgres@localhost/homeworld".to_string())).await.unwrap();
+
+ let shared_state = Arc::new(State {
+ conn
+ });
+
let app = Router::new()
.route("/health", get(health_check))
.route("/ships/list", get(handlers::ships::list))
- .route("/ships/new", post(handlers::ships::new))
- .route("/ships/update", post(handlers::ships::update))
+ .route("/ships/new", post(handlers::ships::new))
.route("/ships/delete/:shasum", delete(handlers::ships::delete))
+ .route("/ships/get/:shasum", get(handlers::ships::get))
.layer( ServiceBuilder::new()
.layer(HandleErrorLayer::new(|error: BoxError| async move {
if error.is::<tower::timeout::error::Elapsed>() {
@@ -50,7 +64,8 @@ async fn main() {
.timeout(Duration::from_secs(10))
.layer(TraceLayer::new_for_http())
.into_inner(),
- );
+ )
+ .layer(Extension(shared_state));
let addr = SocketAddr::from_str(std::env::var("BIND_ADDR").unwrap().as_str().into()).unwrap();
tracing::info!("Listening on {}", addr);
@@ -64,3 +79,4 @@ async fn main() {
async fn health_check() -> &'static str {
"OK"
}
+