feat: with sled db

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2023-08-27 16:49:53 +02:00
parent 9e61ed7ef7
commit 757d1081bd
12 changed files with 682 additions and 31 deletions

View File

@@ -1,8 +1,10 @@
mod agent;
mod db;
mod event;
mod lease;
use std::net::SocketAddr;
use std::path::PathBuf;
use agent::AgentService;
use anyhow::Error;
@@ -11,18 +13,37 @@ use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::routing::{get, post};
use axum::{Json, Router};
use churn_domain::{LeaseResp, ServerEnrollReq, ServerMonitorResp};
use clap::{Parser, Subcommand};
use event::{EventService, LogEvent};
use churn_domain::{LeaseResp, LogEvent, ServerEnrollReq, ServerMonitorResp};
use clap::{Args, Parser, Subcommand, ValueEnum};
use event::EventService;
use lease::LeaseService;
use serde::{Deserialize, Serialize};
use serde_json::json;
use crate::db::Db;
#[derive(Parser)]
#[command(author, version, about, long_about = None, subcommand_required = true)]
struct Command {
#[command(subcommand)]
command: Option<Commands>,
#[clap(flatten)]
global: GlobalArgs,
}
#[derive(Args)]
struct GlobalArgs {
#[arg(env = "CHURN_DATABASE", long, default_value = "sled")]
database: DatabaseType,
#[arg(env = "CHURN_SLED_PATH", long, default_value = "churn-server.sled")]
sled_path: PathBuf,
}
#[derive(ValueEnum, Clone)]
enum DatabaseType {
Sled,
}
#[derive(Subcommand)]
@@ -55,6 +76,9 @@ async fn main() -> anyhow::Result<()> {
match cli.command {
Some(Commands::Serve { host }) => {
tracing::info!("Starting churn server");
let db = match cli.global.database {
DatabaseType::Sled => Db::new_sled(&cli.global.sled_path),
};
let app = Router::new()
.route("/ping", get(ping))
@@ -70,7 +94,7 @@ async fn main() -> anyhow::Result<()> {
.with_state(AppState {
agent: AgentService::default(),
leases: LeaseService::default(),
events: EventService::default(),
events: EventService::new(db),
});
tracing::info!("churn server listening on {}", host);