summaryrefslogtreecommitdiffstats
path: root/crates/profile-service/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/profile-service/src/main.rs')
-rw-r--r--crates/profile-service/src/main.rs111
1 files changed, 109 insertions, 2 deletions
diff --git a/crates/profile-service/src/main.rs b/crates/profile-service/src/main.rs
index e7a11a9..5fe1331 100644
--- a/crates/profile-service/src/main.rs
+++ b/crates/profile-service/src/main.rs
@@ -1,3 +1,110 @@
-fn main() {
- println!("Hello, world!");
+mod cnfg;
+mod server;
+mod state;
+use std::net::{Ipv6Addr, SocketAddr};
+
+use clap::Parser;
+use sellershut_core::profile::profile_server::ProfileServer;
+use stack_up::{Configuration, Services, tracing::Tracing};
+use tokio::signal;
+use tonic::transport::{Server, server::TcpIncoming};
+use tracing::{error, info};
+
+use crate::{
+ server::interceptor::MyInterceptor,
+ state::{AppHandle, AppState},
+};
+
+/// sellershut-profiles
+#[derive(Parser, Debug)]
+#[command(version, about, long_about = None)]
+struct Args {
+ /// Path to config file
+ #[arg(short, long)]
+ config_file: Option<std::path::PathBuf>,
+}
+
+#[tokio::main]
+async fn main() -> anyhow::Result<()> {
+ let args = Args::parse();
+ let config = include_str!("../profile.toml");
+
+ let mut config = config::Config::builder()
+ .add_source(config::File::from_str(config, config::FileFormat::Toml));
+
+ if let Some(cf) = args.config_file.as_ref().and_then(|v| v.to_str()) {
+ config = config.add_source(config::File::new(cf, config::FileFormat::Toml));
+ };
+
+ let mut config: Configuration = config.build()?.try_deserialize()?;
+ config.application.name = env!("CARGO_CRATE_NAME").into();
+ config.application.version = env!("CARGO_PKG_VERSION").into();
+
+ let tracing = Tracing::builder().build(&config.monitoring);
+
+ let mut services = Services::builder()
+ .postgres(&config.database)
+ .await
+ .inspect_err(|e| error!("database: {e}"))?
+ .cache(&config.cache)
+ .await
+ .inspect_err(|e| error!("cache: {e}"))?
+ .build();
+
+ let postgres = services
+ .postgres
+ .take()
+ .ok_or_else(|| anyhow::anyhow!("database is not ready"))?;
+
+ let cache = services
+ .cache
+ .take()
+ .ok_or_else(|| anyhow::anyhow!("cache is not ready"))?;
+
+ let services = crate::state::Services { postgres, cache };
+
+ let state = AppState::create(services, &config).await?;
+
+ let addr = SocketAddr::from((Ipv6Addr::UNSPECIFIED, config.application.port));
+
+ let listener = tokio::net::TcpListener::bind(addr).await?;
+
+ info!(addr = ?addr, "starting server");
+
+ Server::builder()
+ .trace_fn(|_| tracing::info_span!(env!("CARGO_PKG_NAME")))
+ // .add_service(QueryUsersServer::new(state.clone()))
+ .add_service(ProfileServer::with_interceptor(
+ state.clone(),
+ MyInterceptor,
+ ))
+ .serve_with_incoming_shutdown(TcpIncoming::from(listener), shutdown_signal(state))
+ .await?;
+
+ Ok(())
+}
+async fn shutdown_signal(state: AppHandle) {
+ let ctrl_c = async {
+ signal::ctrl_c()
+ .await
+ .expect("failed to install Ctrl+C handler");
+ };
+
+ #[cfg(unix)]
+ let terminate = async {
+ signal::unix::signal(signal::unix::SignalKind::terminate())
+ .expect("failed to install signal handler")
+ .recv()
+ .await;
+ };
+
+ #[cfg(not(unix))]
+ let terminate = std::future::pending::<()>();
+
+ tokio::select! {
+ _ = ctrl_c => {
+ },
+ _ = terminate => {
+ },
+ }
}