Skip to content

Commit

Permalink
feat: ✨ add individual config to services
Browse files Browse the repository at this point in the history
  • Loading branch information
Jisu-Woniu committed Apr 23, 2024
1 parent e22afe0 commit 5a3d180
Show file tree
Hide file tree
Showing 19 changed files with 496 additions and 10 deletions.
304 changes: 304 additions & 0 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ members = [
"crates/rsjudge-rabbitmq",
"crates/rsjudge-rest",
"crates/rsjudge-runner",
"crates/rsjudge-traits",
"crates/rsjudge-utils",
"xtask",
]
Expand Down Expand Up @@ -149,27 +150,28 @@ rsjudge-rest = { version = "0.1.0", path = "crates/rsjudge-rest", optional = tru

anyhow = "1.0.82"
clap = { version = "4.5.4", features = ["derive"] }
config = "0.14.0"
env_logger = "0.11.3"
log.workspace = true
mimalloc = "0.1.39"
serde = { version = "1.0.197", features = ["derive"] }
sysinfo = { version = "0.30.10", features = ["serde"] }
tokio = { version = "1.37.0", features = ["fs", "rt-multi-thread", "macros", "process"] }
toml = "0.8.12"

# Unused for now:
# nix = { version = "0.27.1", features = ["fs"] }
# serde = { version = "1.0.197", features = ["derive"] }
# strfmt = "0.2.4"
# uzers = "0.11.3"

# [dev-dependencies]
# tokio = { version = "1.37.0", features = ["rt-multi-thread", "macros"] }

[features]
grpc = ["dep:rsjudge-grpc"]
grpc = ["dep:rsjudge-grpc", "rsjudge-grpc/serde"]
rest = ["dep:rsjudge-rest"]
rabbitmq = ["dep:rsjudge-rabbitmq"]
default = ["grpc"]
rabbitmq = ["dep:rsjudge-rabbitmq", "rsjudge-rabbitmq/serde"]
default = ["grpc", "rest", "rabbitmq"]

[build-dependencies]
clap = { version = "4.5.4", features = ["derive"] }
Expand Down
7 changes: 7 additions & 0 deletions crates/rsjudge-grpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@ tokio = { version = "1.37.0", features = ["net"] }
tokio-stream = "0.1.15"
tonic = "0.11.0"

# Optional dependencies
serde = { version = "1.0.198", optional = true, features = ["derive"] }
axum = "0.7.5"

[build-dependencies]
anyhow = "1.0.82"
rsjudge-utils.workspace = true
tokio = { version = "1.37.0", features = ["macros", "rt-multi-thread", "process"] }
tonic-build = "0.11.0"

[features]
serde = ["dep:serde"]
9 changes: 9 additions & 0 deletions crates/rsjudge-grpc/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: Apache-2.0

use std::net::SocketAddr;

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct GrpcConfig {
pub listen: Vec<SocketAddr>,
}
1 change: 1 addition & 0 deletions crates/rsjudge-grpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use tonic::transport::{Error, Server};

use crate::{proto::judge_service_server::JudgeServiceServer, server::JudgeServerImpl};

pub mod config;
mod proto;
mod server;

Expand Down
5 changes: 5 additions & 0 deletions crates/rsjudge-rabbitmq/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ rust-version.workspace = true
amqprs = { version = "1.5.4", features = ["urispec"] }
thiserror = "1.0.58"
tokio = "1.37.0"

serde = { version = "1.0.198", features = ["derive"], optional = true }

[features]
serde = ["dep:serde"]
7 changes: 7 additions & 0 deletions crates/rsjudge-rabbitmq/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: Apache-2.0

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct RabbitMqConfig {
pub uri: String,
}
9 changes: 4 additions & 5 deletions crates/rsjudge-rabbitmq/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ use amqprs::{
connection::{Connection, OpenConnectionArguments},
};

use crate::config::RabbitMqConfig;
pub use crate::error::{Error, Result};

pub mod config;
mod error;

pub async fn register() -> Result<()> {
pub async fn register(config: RabbitMqConfig) -> Result<()> {
// Build arguments for new connection.
let args = OpenConnectionArguments::try_from(
// TODO: Read from configuration file.
"amqp://user:bitnami@localhost",
)?;
let args = OpenConnectionArguments::try_from(&*config.uri)?;
let connection = Connection::open(&args).await?;
connection
.register_callback(DefaultConnectionCallback)
Expand Down
1 change: 1 addition & 0 deletions crates/rsjudge-rest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ rust-version.workspace = true

[dependencies]
axum = "0.7.5"
serde = { version = "1.0.198", features = ["derive"] }
tokio = { version = "1.37.0", features = ["net"] }
10 changes: 10 additions & 0 deletions crates/rsjudge-rest/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: Apache-2.0

use std::net::SocketAddr;

use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, PartialEq, Serialize)]
pub struct RestConfig {
pub listen: Vec<SocketAddr>,
}
2 changes: 2 additions & 0 deletions crates/rsjudge-rest/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// SPDX-License-Identifier: Apache-2.0

pub mod config;

use std::{io, net::SocketAddr};

use axum::{routing::get, Router};
Expand Down
16 changes: 16 additions & 0 deletions crates/rsjudge-traits/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# SPDX-License-Identifier: Apache-2.0

[package]
name = "rsjudge-traits"
version.workspace = true
authors.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
rust-version.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-trait = "0.1.80"
serde = "1.0.198"
7 changes: 7 additions & 0 deletions crates/rsjudge-traits/src/judger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: Apache-2.0

pub trait Judger {
type Result;
fn accept_languages(&self) -> Vec<String>;
fn judge(&self, lang: &str, code: &str, input: &str) -> Self::Result;
}
6 changes: 6 additions & 0 deletions crates/rsjudge-traits/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: Apache-2.0

mod judger;
mod service;

pub use crate::{judger::Judger, service::Service};
19 changes: 19 additions & 0 deletions crates/rsjudge-traits/src/service.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: Apache-2.0

use async_trait::async_trait;
use serde::de::DeserializeOwned;

use crate::Judger;

#[async_trait]
pub trait Service<J, C>
where
J: Judger,
C: Config,
{
type Result;
async fn startup(&mut self, judger: J, config: C) -> Self::Result;
async fn shutdown(&mut self);
}

pub trait Config: DeserializeOwned {}
75 changes: 75 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// SPDX-License-Identifier: Apache-2.0

use std::collections::HashMap;

#[cfg(feature = "grpc")]
use rsjudge_grpc::config::GrpcConfig;
#[cfg(feature = "rabbitmq")]
use rsjudge_rabbitmq::config::RabbitMqConfig;
#[cfg(feature = "rest")]
use rsjudge_rest::config::RestConfig;
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize)]
pub struct Config {
pub executors: HashMap<String, Executor>,
pub services: Services,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Executor {
// TODO: Add fields here
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Services {
#[cfg(feature = "grpc")]
pub grpc: GrpcConfig,
#[cfg(feature = "rabbitmq")]
pub rabbitmq: RabbitMqConfig,
#[cfg(feature = "rest")]
pub rest: RestConfig,
}

#[cfg(test)]
mod tests {
#[cfg(any(feature = "grpc", feature = "rest"))]
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};

#[cfg(feature = "grpc")]
use rsjudge_grpc::config::GrpcConfig;
#[cfg(feature = "rabbitmq")]
use rsjudge_rabbitmq::config::RabbitMqConfig;
#[cfg(feature = "rest")]
use rsjudge_rest::config::RestConfig;

use crate::config::Services;

#[test]
fn test_config() -> anyhow::Result<()> {
println!(
"{}",
toml::to_string_pretty(&Services {
#[cfg(feature = "grpc")]
grpc: GrpcConfig {
listen: vec![
SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 50051)),
SocketAddr::V6(SocketAddrV6::new(Ipv6Addr::UNSPECIFIED, 50051, 0, 0))
]
},
#[cfg(feature = "rest")]
rest: RestConfig {
listen: vec![
SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 80)),
SocketAddr::V6(SocketAddrV6::new(Ipv6Addr::UNSPECIFIED, 80, 0, 0))
]
},
#[cfg(feature = "rabbitmq")]
rabbitmq: RabbitMqConfig {
uri: "amqp://user:bitnami@localhost".to_owned()
},
})?
);
Ok(())
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use tokio::{fs::read, process::Command};
use crate::cli::Args;

mod cli;
mod config;

/// # Errors
///
Expand Down
1 change: 0 additions & 1 deletion templates/server.toml

This file was deleted.

16 changes: 16 additions & 0 deletions templates/services.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# SPDX-License-Identifier: Apache-2.0

[grpc]
listen = [
"0.0.0.0:50051",
"[::]:50051",
]

[rest]
listen = [
"0.0.0.0:80",
"[::]:80",
]

[rabbitmq]
uri = "amqp://user:bitnami@localhost"

0 comments on commit 5a3d180

Please sign in to comment.