23
crates/como_bin/Cargo.toml
Normal file
23
crates/como_bin/Cargo.toml
Normal file
@@ -0,0 +1,23 @@
|
||||
[package]
|
||||
name = "como_bin"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
como_gql.workspace = true
|
||||
como_core.workspace = true
|
||||
como_domain.workspace = true
|
||||
como_infrastructure.workspace = true
|
||||
como_api.workspace = true
|
||||
|
||||
|
||||
axum.workspace = true
|
||||
serde_json.workspace = true
|
||||
tokio.workspace = true
|
||||
anyhow.workspace = true
|
||||
dotenv.workspace = true
|
||||
tracing.workspace = true
|
||||
tracing-subscriber.workspace = true
|
||||
clap.workspace = true
|
22
crates/como_bin/src/error.rs
Normal file
22
crates/como_bin/src/error.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
use axum::{http::StatusCode, response::IntoResponse, Json};
|
||||
use serde_json::json;
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug)]
|
||||
pub enum AppError {
|
||||
WrongCredentials,
|
||||
InternalServerError,
|
||||
}
|
||||
|
||||
impl IntoResponse for AppError {
|
||||
fn into_response(self) -> axum::response::Response {
|
||||
let (status, err_msg) = match self {
|
||||
Self::WrongCredentials => (StatusCode::BAD_REQUEST, "invalid credentials"),
|
||||
Self::InternalServerError => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
"something went wrong with your request",
|
||||
),
|
||||
};
|
||||
(status, Json(json!({ "error": err_msg }))).into_response()
|
||||
}
|
||||
}
|
41
crates/como_bin/src/main.rs
Normal file
41
crates/como_bin/src/main.rs
Normal file
@@ -0,0 +1,41 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
mod error;
|
||||
|
||||
use clap::Parser;
|
||||
|
||||
use anyhow::Context;
|
||||
|
||||
use como_api::router::Api;
|
||||
use como_infrastructure::{
|
||||
configs::AppConfig, database::ConnectionPoolManager, register::ServiceRegister,
|
||||
};
|
||||
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
tracing::info!("Loading dotenv");
|
||||
dotenv::dotenv()?;
|
||||
|
||||
let config = Arc::new(AppConfig::parse());
|
||||
|
||||
tracing_subscriber::registry()
|
||||
.with(tracing_subscriber::EnvFilter::new(&config.rust_log))
|
||||
.with(tracing_subscriber::fmt::layer())
|
||||
.init();
|
||||
|
||||
let pool = ConnectionPoolManager::new_pool(&config.database_url, true).await?;
|
||||
|
||||
let service_register = ServiceRegister::new(pool, config.clone()).await?;
|
||||
|
||||
Api::new(
|
||||
config.api_port,
|
||||
&config.cors_origin,
|
||||
service_register.clone(),
|
||||
)
|
||||
.await
|
||||
.context("could not initialize API")?;
|
||||
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user