feat: with basic server
All checks were successful
continuous-integration/drone/push Build is passing

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2024-05-11 15:50:24 +02:00
parent 7d268e73f0
commit 6a147ba0d2
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
15 changed files with 280 additions and 125 deletions

21
Cargo.lock generated
View File

@ -943,14 +943,13 @@ dependencies = [
"dirs", "dirs",
"dotenv", "dotenv",
"hyperlog-core", "hyperlog-core",
"hyperlog-server",
"hyperlog-tui", "hyperlog-tui",
"serde", "serde",
"serde_json", "serde_json",
"similar-asserts", "similar-asserts",
"sqlx",
"tempfile", "tempfile",
"tokio", "tokio",
"tower-http",
"tracing", "tracing",
"tracing-subscriber", "tracing-subscriber",
"uuid", "uuid",
@ -978,6 +977,24 @@ dependencies = [
"uuid", "uuid",
] ]
[[package]]
name = "hyperlog-server"
version = "0.1.0"
dependencies = [
"anyhow",
"axum",
"hyperlog-core",
"serde",
"serde_json",
"similar-asserts",
"sqlx",
"tempfile",
"tokio",
"tower-http",
"tracing",
"uuid",
]
[[package]] [[package]]
name = "hyperlog-tui" name = "hyperlog-tui"
version = "0.1.0" version = "0.1.0"

View File

@ -5,6 +5,7 @@ resolver = "2"
[workspace.dependencies] [workspace.dependencies]
hyperlog-core = { path = "crates/hyperlog-core" } hyperlog-core = { path = "crates/hyperlog-core" }
hyperlog-tui = { path = "crates/hyperlog-tui" } hyperlog-tui = { path = "crates/hyperlog-tui" }
hyperlog-server = { path = "crates/hyperlog-server" }
anyhow = { version = "1" } anyhow = { version = "1" }
tokio = { version = "1", features = ["full"] } tokio = { version = "1", features = ["full"] }
@ -13,8 +14,10 @@ tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
clap = { version = "4", features = ["derive", "env"] } clap = { version = "4", features = ["derive", "env"] }
dotenv = { version = "0.15" } dotenv = { version = "0.15" }
axum = { version = "0.7" } axum = { version = "0.7" }
serde = { version = "1.0.201", features = ["derive"] }
serde_json = "1.0.117" serde_json = "1.0.117"
itertools = "0.12.1" itertools = "0.12.1"
uuid = { version = "1.8.0", features = ["v4"] }
[workspace.package] [workspace.package]
version = "0.1.0" version = "0.1.0"

View File

@ -0,0 +1,28 @@
[package]
name = "hyperlog-server"
version = "0.1.0"
edition = "2021"
[dependencies]
hyperlog-core.workspace = true
anyhow.workspace = true
tokio.workspace = true
tracing.workspace = true
axum.workspace = true
serde.workspace = true
serde_json.workspace = true
uuid.workspace = true
tower-http = { version = "0.5.2", features = ["cors", "trace"] }
sqlx = { version = "0.7.4", features = [
"runtime-tokio",
"tls-rustls",
"postgres",
"uuid",
"time",
] }
[dev-dependencies]
similar-asserts = "1.5.0"
tempfile = "3.10.1"

View File

@ -0,0 +1,40 @@
use std::{net::SocketAddr, sync::Arc};
use axum::{extract::MatchedPath, http::Request, routing::get, Router};
use tower_http::trace::TraceLayer;
use crate::state::{SharedState, State};
async fn root() -> &'static str {
"Hello, hyperlog!"
}
pub async fn serve(state: &SharedState, host: &SocketAddr) -> anyhow::Result<()> {
let app = Router::new()
.route("/", get(root))
.with_state(state.clone())
.layer(
TraceLayer::new_for_http().make_span_with(|request: &Request<_>| {
// Log the matched route's path (with placeholders not filled in).
// Use request.uri() or OriginalUri if you want the real path.
let matched_path = request
.extensions()
.get::<MatchedPath>()
.map(MatchedPath::as_str);
tracing::info_span!(
"http_request",
method = ?request.method(),
matched_path,
some_other_field = tracing::field::Empty,
)
}), // ...
);
tracing::info!("listening on {}", host);
let listener = tokio::net::TcpListener::bind(host).await.unwrap();
axum::serve(listener, app.into_make_service())
.await
.unwrap();
Ok(())
}

View File

@ -0,0 +1,40 @@
use std::{net::SocketAddr, sync::Arc};
use axum::{extract::MatchedPath, http::Request, routing::get, Router};
use tower_http::trace::TraceLayer;
use crate::state::{SharedState, State};
async fn root() -> &'static str {
"Hello, hyperlog!"
}
pub async fn serve(state: &SharedState, host: &SocketAddr) -> anyhow::Result<()> {
let app = Router::new()
.route("/", get(root))
.with_state(state.clone())
.layer(
TraceLayer::new_for_http().make_span_with(|request: &Request<_>| {
// Log the matched route's path (with placeholders not filled in).
// Use request.uri() or OriginalUri if you want the real path.
let matched_path = request
.extensions()
.get::<MatchedPath>()
.map(MatchedPath::as_str);
tracing::info_span!(
"http_request",
method = ?request.method(),
matched_path,
some_other_field = tracing::field::Empty,
)
}), // ...
);
tracing::info!("listening on {}", host);
let listener = tokio::net::TcpListener::bind(host).await.unwrap();
axum::serve(listener, app.into_make_service())
.await
.unwrap();
Ok(())
}

View File

@ -0,0 +1,36 @@
use std::{net::SocketAddr, sync::Arc};
use crate::state::{SharedState, State};
mod external_http;
mod internal_http;
mod state;
#[derive(Clone)]
pub struct ServeOptions {
pub external_http: SocketAddr,
pub internal_http: SocketAddr,
}
pub async fn serve(opts: ServeOptions) -> anyhow::Result<()> {
let ctrl_c = async {
tokio::signal::ctrl_c().await.unwrap();
tracing::info!("kill signal received, shutting down");
};
tracing::debug!("setting up dependencies");
let state = SharedState(Arc::new(State::new().await?));
tracing::debug!("serve starting");
tokio::select!(
res = external_http::serve(&state, &opts.external_http) => {
res?
},
res = internal_http::serve(&state, &opts.internal_http) => {
res?
},
() = ctrl_c => {}
);
tracing::debug!("serve finalized");
Ok(())
}

View File

@ -0,0 +1,37 @@
use std::{ops::Deref, sync::Arc};
use anyhow::Context;
use sqlx::{Pool, Postgres};
#[derive(Clone)]
pub struct SharedState(pub Arc<State>);
impl Deref for SharedState {
type Target = Arc<State>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
pub struct State {
pub _db: Pool<Postgres>,
}
impl State {
pub async fn new() -> anyhow::Result<Self> {
let db = sqlx::PgPool::connect(
&std::env::var("DATABASE_URL").context("DATABASE_URL is not set")?,
)
.await?;
sqlx::migrate!("migrations/crdb")
.set_locking(false)
.run(&db)
.await?;
let _ = sqlx::query("SELECT 1;").fetch_one(&db).await?;
Ok(Self { _db: db })
}
}

View File

@ -7,6 +7,7 @@ repository = "https://git.front.kjuulh.io/kjuulh/hyperlog"
[dependencies] [dependencies]
hyperlog-core.workspace = true hyperlog-core.workspace = true
hyperlog-tui.workspace = true hyperlog-tui.workspace = true
hyperlog-server = { workspace = true, optional = true }
anyhow.workspace = true anyhow.workspace = true
tokio.workspace = true tokio.workspace = true
@ -15,21 +16,17 @@ tracing-subscriber.workspace = true
clap.workspace = true clap.workspace = true
dotenv.workspace = true dotenv.workspace = true
axum.workspace = true axum.workspace = true
serde.workspace = true
serde_json.workspace = true serde_json.workspace = true
uuid.workspace = true
serde = { version = "1.0.201", features = ["derive"] }
sqlx = { version = "0.7.4", features = [
"runtime-tokio",
"tls-rustls",
"postgres",
"uuid",
"time",
] }
uuid = { version = "1.8.0", features = ["v4"] }
tower-http = { version = "0.5.2", features = ["cors", "trace"] }
bus = "2.4.1" bus = "2.4.1"
dirs = "5.0.1" dirs = "5.0.1"
[dev-dependencies] [dev-dependencies]
similar-asserts = "1.5.0" similar-asserts = "1.5.0"
tempfile = "3.10.1" tempfile = "3.10.1"
[features]
default = []
include_server = ["dep:hyperlog-server"]

View File

@ -3,8 +3,6 @@ use std::net::SocketAddr;
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use hyperlog_core::{commander, state}; use hyperlog_core::{commander, state};
use crate::server::serve;
#[derive(Parser)] #[derive(Parser)]
#[command(author, version, about, long_about = None)] #[command(author, version, about, long_about = None)]
struct Command { struct Command {
@ -14,9 +12,14 @@ struct Command {
#[derive(Subcommand)] #[derive(Subcommand)]
enum Commands { enum Commands {
#[cfg(feature = "include_server")]
Serve { Serve {
#[arg(env = "SERVICE_HOST", long, default_value = "127.0.0.1:3000")] #[arg(env = "EXTERNAL_HOST", long, default_value = "127.0.0.1:3000")]
host: SocketAddr, external_host: SocketAddr,
#[arg(env = "INTERNAL_HOST", long, default_value = "127.0.0.1:3001")]
internal_host: SocketAddr,
#[arg(env = "EXTERNAL_GRPC_HOST", long, default_value = "127.0.0.1:4000")]
external_grpc_host: SocketAddr,
}, },
Exec { Exec {
#[command(subcommand)] #[command(subcommand)]
@ -70,58 +73,75 @@ pub async fn execute() -> anyhow::Result<()> {
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
} }
let state = state::State::new()?;
match cli.command { match cli.command {
Some(Commands::Serve { host }) => { #[cfg(feature = "include_server")]
Some(Commands::Serve {
external_host,
internal_host,
..
}) => {
tracing::info!("Starting service"); tracing::info!("Starting service");
serve(host).await?; hyperlog_server::serve(hyperlog_server::ServeOptions {
external_http: external_host,
internal_http: internal_host,
})
.await?;
} }
Some(Commands::Exec { commands }) => match commands { Some(Commands::Exec { commands }) => {
ExecCommands::CreateRoot { root } => state let state = state::State::new()?;
.commander match commands {
.execute(commander::Command::CreateRoot { root })?, ExecCommands::CreateRoot { root } => state
ExecCommands::CreateSection { root, path } => { .commander
state.commander.execute(commander::Command::CreateSection { .execute(commander::Command::CreateRoot { root })?,
root, ExecCommands::CreateSection { root, path } => {
path: path state.commander.execute(commander::Command::CreateSection {
.unwrap_or_default() root,
.split('.') path: path
.map(|s| s.to_string()) .unwrap_or_default()
.filter(|s| !s.is_empty()) .split('.')
.collect::<Vec<String>>(), .map(|s| s.to_string())
})? .filter(|s| !s.is_empty())
.collect::<Vec<String>>(),
})?
}
} }
}, }
Some(Commands::Query { commands }) => match commands { Some(Commands::Query { commands }) => {
QueryCommands::Get { root, path } => { let state = state::State::new()?;
let res = state.querier.get( match commands {
&root, QueryCommands::Get { root, path } => {
path.unwrap_or_default() let res = state.querier.get(
.split('.') &root,
.filter(|s| !s.is_empty()), path.unwrap_or_default()
); .split('.')
.filter(|s| !s.is_empty()),
);
let output = serde_json::to_string_pretty(&res)?; let output = serde_json::to_string_pretty(&res)?;
println!("{}", output); println!("{}", output);
}
} }
}, }
Some(Commands::CreateRoot { name }) => { Some(Commands::CreateRoot { name }) => {
let state = state::State::new()?;
state state
.commander .commander
.execute(commander::Command::CreateRoot { root: name })?; .execute(commander::Command::CreateRoot { root: name })?;
println!("Root was successfully created, now run:\n\n$ hyperlog"); println!("Root was successfully created, now run:\n\n$ hyperlog");
} }
Some(Commands::Info {}) => { Some(Commands::Info {}) => {
let state = state::State::new()?;
println!("graph stored at: {}", state.storage.info()?) println!("graph stored at: {}", state.storage.info()?)
} }
Some(Commands::ClearLock {}) => { Some(Commands::ClearLock {}) => {
let state = state::State::new()?;
state.storage.clear_lock_file(); state.storage.clear_lock_file();
println!("cleared lock file"); println!("cleared lock file");
} }
None => { None => {
let state = state::State::new()?;
hyperlog_tui::execute(state).await?; hyperlog_tui::execute(state).await?;
} }
} }

View File

@ -1,6 +1,4 @@
mod cli; mod cli;
pub(crate) mod server;
pub(crate) mod state;
#[tokio::main] #[tokio::main]
async fn main() -> anyhow::Result<()> { async fn main() -> anyhow::Result<()> {

View File

@ -1,42 +1 @@
use std::{net::SocketAddr, sync::Arc};
use axum::{extract::MatchedPath, http::Request, routing::get, Router};
use tower_http::trace::TraceLayer;
use crate::state::{SharedState, State};
async fn root() -> &'static str {
"Hello, hyperlog!"
}
pub async fn serve(host: SocketAddr) -> anyhow::Result<()> {
let state = SharedState(Arc::new(State::new().await?));
let app = Router::new()
.route("/", get(root))
.with_state(state.clone())
.layer(
TraceLayer::new_for_http().make_span_with(|request: &Request<_>| {
// Log the matched route's path (with placeholders not filled in).
// Use request.uri() or OriginalUri if you want the real path.
let matched_path = request
.extensions()
.get::<MatchedPath>()
.map(MatchedPath::as_str);
tracing::info_span!(
"http_request",
method = ?request.method(),
matched_path,
some_other_field = tracing::field::Empty,
)
}), // ...
);
tracing::info!("listening on {}", host);
let listener = tokio::net::TcpListener::bind(host).await.unwrap();
axum::serve(listener, app.into_make_service())
.await
.unwrap();
Ok(())
}

View File

@ -1,37 +1 @@
use std::{ops::Deref, sync::Arc};
use anyhow::Context;
use sqlx::{Pool, Postgres};
#[derive(Clone)]
pub struct SharedState(pub Arc<State>);
impl Deref for SharedState {
type Target = Arc<State>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
pub struct State {
pub _db: Pool<Postgres>,
}
impl State {
pub async fn new() -> anyhow::Result<Self> {
let db = sqlx::PgPool::connect(
&std::env::var("DATABASE_URL").context("DATABASE_URL is not set")?,
)
.await?;
sqlx::migrate!("migrations/crdb")
.set_locking(false)
.run(&db)
.await?;
let _ = sqlx::query("SELECT 1;").fetch_one(&db).await?;
Ok(Self { _db: db })
}
}

View File

@ -15,3 +15,7 @@ please:
api_url: https://git.front.kjuulh.io api_url: https://git.front.kjuulh.io
actions: actions:
rust: rust:
scripts:
dev:
type: shell

12
scripts/dev.sh Executable file
View File

@ -0,0 +1,12 @@
#!/usr/bin/env zsh
echo "starting services"
docker compose -f templates/docker-compose.yaml up -d --remove-orphans
tear_down() {
docker compose -f templates/docker-compose.yaml down -v || true
}
trap tear_down EXIT
RUST_LOG=trace,tokio=info,tower=info,mio=info,sqlx=info cargo run -F include_server -- serve