feat: refactor files
Some checks failed
continuous-integration/drone/push Build is failing

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2024-02-11 13:49:14 +01:00
parent be8d605b6a
commit de1ff1b9a6
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
5 changed files with 145 additions and 82 deletions

View File

@ -0,0 +1,20 @@
use std::net::SocketAddr;
use axum::{routing::get, Router};
use crate::app::SharedApp;
pub async fn axum_serve(host: SocketAddr, app: SharedApp) -> anyhow::Result<()> {
let app = Router::new().route("/", get(root)).with_state(app);
tracing::info!("listening on {}", host);
let listener = tokio::net::TcpListener::bind(host).await.unwrap();
axum::serve(listener, app.into_make_service()).await?;
Ok(())
}
async fn root() -> &'static str {
"Hello, flux-releaser!"
}

View File

@ -0,0 +1,38 @@
use std::{ops::Deref, sync::Arc};
#[derive(Clone)]
pub struct SharedApp(Arc<App>);
impl Deref for SharedApp {
type Target = Arc<App>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Default for SharedApp {
fn default() -> Self {
Self::new()
}
}
impl SharedApp {
pub fn new() -> Self {
Self(Arc::new(App::new()))
}
}
pub struct App {}
impl Default for App {
fn default() -> Self {
Self::new()
}
}
impl App {
pub fn new() -> Self {
Self {}
}
}

View File

@ -0,0 +1,46 @@
use clap::{Parser, Subcommand};
use std::net::SocketAddr;
use crate::{api::axum_serve, app::SharedApp, grpc::tonic_serve};
#[derive(Parser)]
#[command(author, version, about, long_about = None, subcommand_required = true)]
pub struct Command {
#[command(subcommand)]
pub command: Option<Commands>,
}
#[derive(Subcommand)]
pub enum Commands {
Serve {
#[arg(env = "SERVICE_HOST", long, default_value = "127.0.0.1:3000")]
host: SocketAddr,
#[arg(env = "SERVICE_GRPC_HOST", long, default_value = "127.0.0.1:7900")]
grpc_host: SocketAddr,
},
}
impl Command {
pub async fn run() -> anyhow::Result<()> {
let cli = Command::parse();
if let Some(Commands::Serve { host, grpc_host }) = cli.command {
tracing_subscriber::fmt::init();
tracing::info!("Starting service");
let app = SharedApp::new();
tokio::select! {
res = axum_serve(host, app) => {
res?;
},
res = tonic_serve(grpc_host) => {
res?;
},
};
}
Ok(())
}
}

View File

@ -0,0 +1,35 @@
use std::net::SocketAddr;
use tonic::transport::Server;
use self::gen::{greeter_server, HelloReply, HelloRequest};
mod gen {
tonic::include_proto!("flux_releaser");
}
#[derive(Debug, Default)]
pub struct FluxReleaserGrpc {}
#[tonic::async_trait]
impl greeter_server::Greeter for FluxReleaserGrpc {
async fn say_hello(
&self,
request: tonic::Request<HelloRequest>,
) -> std::result::Result<tonic::Response<HelloReply>, tonic::Status> {
Ok(tonic::Response::new(HelloReply {
message: "something".into(),
}))
}
}
pub async fn tonic_serve(host: SocketAddr) -> anyhow::Result<()> {
tracing::info!("grpc listening on: {}", host);
Server::builder()
.add_service(greeter_server::GreeterServer::new(
FluxReleaserGrpc::default(),
))
.serve(host)
.await?;
Ok(())
}

View File

@ -1,93 +1,17 @@
use std::net::SocketAddr;
use cli::Command;
use axum::routing::get;
use axum::Router;
use clap::{Parser, Subcommand};
use grpc::{greeter_server, HelloReply, HelloRequest};
use tokio::net::TcpListener;
use tonic::transport::Server;
mod cli;
#[derive(Parser)]
#[command(author, version, about, long_about = None, subcommand_required = true)]
struct Command {
#[command(subcommand)]
command: Option<Commands>,
}
mod api;
mod grpc;
#[derive(Subcommand)]
enum Commands {
Serve {
#[arg(env = "SERVICE_HOST", long, default_value = "127.0.0.1:3000")]
host: SocketAddr,
#[arg(env = "SERVICE_GRPC_HOST", long, default_value = "127.0.0.1:7900")]
grpc_host: SocketAddr,
},
}
async fn axum_serve(listener: TcpListener, app: Router) -> anyhow::Result<()> {
axum::serve(listener, app.into_make_service()).await?;
Ok(())
}
mod grpc {
tonic::include_proto!("flux_releaser");
}
#[derive(Debug, Default)]
pub struct FluxReleaserGrpc {}
#[tonic::async_trait]
impl greeter_server::Greeter for FluxReleaserGrpc {
async fn say_hello(
&self,
request: tonic::Request<HelloRequest>,
) -> std::result::Result<tonic::Response<HelloReply>, tonic::Status> {
Ok(tonic::Response::new(HelloReply {
message: "something".into(),
}))
}
}
async fn tonic_serve(host: SocketAddr) -> anyhow::Result<()> {
tracing::info!("grpc listening on: {}", host);
Server::builder()
.add_service(greeter_server::GreeterServer::new(
FluxReleaserGrpc::default(),
))
.serve(host)
.await?;
Ok(())
}
mod app;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
dotenv::dotenv().ok();
tracing_subscriber::fmt::init();
let cli = Command::parse();
if let Some(Commands::Serve { host, grpc_host }) = cli.command {
tracing::info!("Starting service");
let app = Router::new().route("/", get(root));
tracing::info!("listening on {}", host);
let listener = tokio::net::TcpListener::bind(host).await.unwrap();
tokio::select! {
res = axum_serve(listener, app) => {
res?;
},
res = tonic_serve(grpc_host) => {
res?;
},
};
}
Command::run().await?;
Ok(())
}
async fn root() -> &'static str {
"Hello, flux-releaser!"
}