Skip to content

Commit

Permalink
feat(all): server 2/n
Browse files Browse the repository at this point in the history
  • Loading branch information
martsokha committed Dec 9, 2024
1 parent bda0623 commit eb78ff8
Show file tree
Hide file tree
Showing 80 changed files with 1,316 additions and 792 deletions.
28 changes: 14 additions & 14 deletions .github/dependabot.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
version: 2
updates:

- package-ecosystem: "cargo"
directory: "/"
schedule:
interval: "weekly"
timezone: "Europe/Warsaw"
day: "friday"
time: "18:00"
- package-ecosystem: "cargo"
directory: "/"
schedule:
interval: "weekly"
timezone: "Europe/Warsaw"
day: "friday"
time: "18:00"

- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "weekly"
timezone: "Europe/Warsaw"
day: "friday"
time: "18:00"
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "weekly"
timezone: "Europe/Warsaw"
day: "friday"
time: "18:00"
47 changes: 47 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: ci & cd

on:
push:
branches:
- "main" # Trigger on main branch.
tags:
- "v*.*.*" # Trigger on semantic version tags.
pull_request: # Validation only (without pushing).

jobs:
build:
runs-on: ubuntu-22.04
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: stable

- name: Run Cargo:fmt
run: cargo +nightly fmt --all -- --check

- name: Run Cargo:clippy
run: cargo clippy --all-features -- -D warnings

- name: Run Cargo:test
run: cargo test --verbose --all-features

publish:
runs-on: ubuntu-22.04
if: github.event_name == 'push'
steps:
- name: Check out
uses: actions/checkout@v3

- name: Set up Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: stable

- name: Publish
run: cargo publish --token ${CRATES_TOKEN}
env:
CRATES_TOKEN: ${{ secrets.CRATES_TOKEN }}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ output/
build/

# Generated
/crates/database/entities/
crates/runtime/protobuf/

# Binaries
*.exe
Expand Down
33 changes: 20 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,23 @@ repository = "https://github.com/axiston/axiston"
homepage = "https://github.com/axiston/axiston"
documentation = "https://docs.rs/axiston"

# TODO: Import axiston-database-migrate from crates.io.
# TODO: Import axiston-database-schema from crates.io.

[workspace.dependencies]
axiston-db-generate = { path = "../database/crates/generate" }
axiston-db-migrate = { path = "../database/crates/migrate" }
axiston-db-connect = { path = "./crates/database", version = "0.1.0" }

axiston-graph = { path = "./crates/graph", version = "0.1.0" }
axiston-runtime = { path = "./crates/runtime", version = "0.1.0" }
axiston-server = { path = "./crates/server", version = "0.1.0" }

tokio = { version = "1.41", features = ["rt-multi-thread", "macros"] }
deadpool = { version = "0.12", features = ["rt_tokio_1"] }
futures = { version = "0.3", features = [] }
async-trait = { version = "0.1.83", features = [] }
thiserror = { version = "2.0", features = [] }
anyhow = { version = "1.0", features = ["backtrace"] }

tracing = { version = "0.1", features = [] }
tracing-subscriber = { version = "0.3", features = ["env-filter", "time"] }
Expand All @@ -35,26 +48,20 @@ axum = { version = "0.7", features = ["http2", "macros", "ws"] }
axum-server = { version = "0.7.1", features = ["tls-rustls"] }
axum-extra = { version = "0.9", features = ["typed-header"] }
axum-test = { version = "16.4", features = [] }

sea-orm = { version = "1.1", features = ["runtime-tokio-rustls", "sqlx-postgres", "macros"] }
sea-orm-migration = { version = "1.1", features = ["runtime-tokio-rustls", "sqlx-postgres"] }
sea-orm-cli = { version = "1.1", features = ["runtime-tokio-rustls"] }

thiserror = { version = "2.0", features = [] }
anyhow = { version = "1.0", features = ["backtrace"] }
tower = { version = "0.4", features = ["full"] }
tower-http = { version = "0.5", features = ["full"] }

derive_more = { version = "1.0", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
serde_with = { version = "3.11", features = [] }
serde_toml = { package = "toml", version = "0.8", features = [] }
serde_json = { version = "1.0", features = [] }
ts-rs = { version = "10.1", features = ["uuid-impl"] }

bytes = { version = "1.9", features = ["serde"] }
uuid = { version = "1.11", features = ["v4", "serde"] }
time = { version = "0.3", features = ["serde"] }
base64 = { version = "0.22", features = [] }
ecow = { version = "0.2", features = ["serde"] }

axiston-database = { path = "./crates/database", version = "0.1.0" }
axiston-graph = { path = "./crates/graph", version = "0.1.0" }
axiston-runtime = { path = "./crates/runtime", version = "0.1.0" }
axiston-server = { path = "./crates/server", version = "0.1.0" }
cron = { version = "0.13", features = [] }
ipnet = { version = "2.10", features = ["serde"] }
34 changes: 16 additions & 18 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ default = []
# - Enables HTTPS support with the axum-server crate.
# - Spawns a server that redirects incoming HTTP requests.
support-https = ["dep:axum-server"]

# - Builds the graph editor app into the app directory.
# - Enables service that serves files from app directory.
support-files = []
Expand All @@ -31,26 +32,23 @@ support-files = []
axiston-server = { workspace = true }

clap = { version = "4.5", features = ["derive"] }
tokio = { version = "1.41", features = ["macros", "rt-multi-thread", "signal"] }
anyhow = { version = "1.0", features = ["backtrace"] }

serde = { version = "1.0", features = ["derive"] }
serde_with = { version = "3.11", features = [] }
serde_toml = { package = "toml", version = "0.8", features = [] }
serde_json = { version = "1.0", features = [] }
countio = { version = "0.2", features = [] }
tokio = { workspace = true }
anyhow = { workspace = true }

tracing = { version = "0.1", features = [] }
tracing-subscriber = { version = "0.3", features = ["env-filter", "time"] }
tracing-opentelemetry = { version = "0.27.0", features = [] }
opentelemetry = { version = "0.26.0", features = [] }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
tracing-opentelemetry = { workspace = true }
opentelemetry = { workspace = true }

axum = { version = "0.7", features = ["http2", "macros", "ws"] }
axum-server = { version = "0.7", optional = true, features = ["tls-rustls"] }
axum-extra = { version = "0.9", features = ["typed-header"] }
axum = { workspace = true }
axum-server = { workspace = true, optional = true }
axum-extra = { workspace = true }
tower = { workspace = true }
tower-http = { workspace = true }

tower = { version = "0.4", features = ["full"] }
tower-http = { version = "0.5", features = ["full"] }
serde = { workspace = true }
serde_toml = { workspace = true }
serde_json = { workspace = true }

[dev-dependencies]
axum-test = { version = "16.3.0", features = [] }
axum-test = { workspace = true }
7 changes: 0 additions & 7 deletions crates/cli/config/load_yaml.rs

This file was deleted.

9 changes: 3 additions & 6 deletions crates/cli/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//! TODO.
//! Loads and parses configuration files.
//!
mod load_json;
mod load_toml;
mod load_yaml;

use std::ffi::OsStr;
use std::path::PathBuf;
Expand All @@ -14,7 +13,6 @@ use serde::Deserialize;

use crate::config::load_json::load_json;
use crate::config::load_toml::load_toml;
use crate::config::load_yaml::load_yaml;
use crate::server::ServerConfig;

/// Command-line arguments.
Expand Down Expand Up @@ -68,7 +66,6 @@ impl Args {
Cli::File { path } => match path.extension() {
Some(ext) if OsStr::new("toml") == ext => load_toml(path),
Some(ext) if OsStr::new("json") == ext => load_json(path),
Some(ext) if OsStr::new("yaml") == ext => load_yaml(path),
_ => Err(anyhow::anyhow!("should specify a supported file extension")),
},
}
Expand All @@ -77,8 +74,8 @@ impl Args {
/// Returns a new [`AppConfig`].
pub fn build_app_config(&self) -> AppConfig {
AppConfig {
database_conn: self.database.clone(),
multiple_gateways: self.multiple,
database: self.database.clone(),
multiple: self.multiple,
}
}

Expand Down
13 changes: 9 additions & 4 deletions crates/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ mod server;
use std::time::Duration;

use axiston_server::handler::routes;
use axiston_server::middleware::{initialize_tracing, RouterExt};
use axiston_server::service::AppState;
use axiston_server::middleware::RouterTracingExt;
use axiston_server::service::{AppState, SchedulerRuntime};
use axum::Router;
use clap::Parser;

use crate::config::Args;
use crate::middleware::initialize_tracing;
use crate::server::run_supported_server;

#[tokio::main]
Expand All @@ -25,15 +26,19 @@ async fn main() -> anyhow::Result<()> {
let app_config = args.build_app_config();
let state = AppState::connect(app_config).await?;

let scheduler = SchedulerRuntime::new(state.clone());
let scheduler_handler = scheduler.run_trigger_loop();

let app = Router::new()
.merge(routes())
.with_error_handling_layer(Duration::from_secs(60))
.with_observability_layer()
.with_inner_error_handling_layer(Duration::from_secs(60))
.with_inner_observability_layer()
.with_state(state);

// Listen.
let server_config = args.build_server_config();
run_supported_server(server_config, app).await?;
let _ = scheduler_handler.await?;

Ok(())
}
41 changes: 41 additions & 0 deletions crates/cli/middleware/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,42 @@
// TODO: Move initialize_tracing here

#[must_use]
fn build_env_filter() -> tracing_subscriber::EnvFilter {
let current = std::env::var("RUST_LOG")
.or_else(|_| std::env::var("OTEL_LOG_LEVEL"))
.unwrap_or_else(|_| "info".to_string());

let env = format!("{},server=trace,otel=debug,tower_http=debug", current);
std::env::set_var("RUST_LOG", env);
tracing_subscriber::EnvFilter::from_default_env()
}

pub async fn initialize_tracing() -> anyhow::Result<()> {
use tracing_subscriber::fmt::layer;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;

// Setups a temporary subscriber to log output during setup.
let env_filter = build_env_filter();
let fmt_layer = layer().pretty();
let subscriber = tracing_subscriber::registry()
.with(env_filter)
.with(fmt_layer);

let _guard = tracing::subscriber::set_default(subscriber);
tracing::trace!(target: "server:otel", "initialized temporary subscriber");

// TODO: Enable OpenTelemetry.
// https://github.com/davidB/tracing-opentelemetry-instrumentation-sdk

// Setups an actual subscriber.
let env_filter = build_env_filter();
let fmt_layer = layer().pretty();
tracing_subscriber::registry()
.with(env_filter)
.with(fmt_layer)
.init();

tracing::trace!(target: "server:otel", "initialized subscriber");
Ok(())
}
2 changes: 1 addition & 1 deletion crates/cli/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub async fn run_supported_server(
server_config: ServerConfig,
app_router: Router,
) -> anyhow::Result<()> {
let timeout = Duration::from_secs(60);
let timeout = Duration::from_secs(server_config.shutdown);
let fut = shutdown_signal(timeout);

#[cfg(not(feature = "support-https"))]
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/server/run_default.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::future::Future;
use std::net::{Ipv4Addr, SocketAddr};

use axiston_server::service::AppConnectInfo;
use axiston_server::extract::AppConnectInfo;
use axum::Router;
use tokio::net::TcpListener;

Expand Down
2 changes: 1 addition & 1 deletion crates/cli/server/run_secure.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::future::Future;
use std::net::{Ipv4Addr, SocketAddr};

use axiston_server::service::AppConnectInfo;
use axiston_server::extract::AppConnectInfo;
use axum::Router;
use axum_server::tls_rustls::RustlsConfig;
use axum_server::Handle;
Expand Down
11 changes: 7 additions & 4 deletions crates/cli/server/serv_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ use std::path::PathBuf;
#[must_use = "configs do nothing unless you use them"]
#[derive(Debug, Clone)]
pub struct ServerConfig {
/// Used by the primary server.
/// Port exposed by the primary server.
pub port: u16,
// Shutdown duration in seconds.
pub shutdown: u64,

// TODO: Shutdown duration.
/// Used by the secondary (`http` to `https` redirection) server.
/// Port exposed by the secondary (redirection) server.
#[cfg(feature = "support-https")]
#[cfg_attr(docsrs, doc(cfg(feature = "support-https")))]
pub redirect: u16,
Expand Down Expand Up @@ -53,6 +54,7 @@ impl Default for ServerConfig {
#[derive(Debug, Default, Clone)]
pub struct ServerBuilder {
pub port: Option<u16>,
pub shutdown: Option<u64>,

#[cfg(feature = "support-https")]
#[cfg_attr(docsrs, doc(cfg(feature = "support-https")))]
Expand All @@ -78,8 +80,9 @@ impl ServerBuilder {
pub fn build(self) -> ServerConfig {
ServerConfig {
port: self.port.unwrap_or(3000),
shutdown: self.shutdown.unwrap_or(10),
#[cfg(feature = "support-https")]
redirect: self.port.unwrap_or(3001),
redirect: self.redirect.unwrap_or(3001),
#[cfg(feature = "support-https")]
cert: self.cert.unwrap_or(PathBuf::from("./cert.pem")),
#[cfg(feature = "support-https")]
Expand Down
Loading

0 comments on commit eb78ff8

Please sign in to comment.