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 ea6e075
Show file tree
Hide file tree
Showing 19 changed files with 227 additions and 33 deletions.
53 changes: 33 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 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 @@ -48,8 +49,8 @@ assets = [
"644",
],
[
"templates/server.toml",
"etc/rsjudge/server.toml",
"templates/services.toml",
"etc/rsjudge/services.toml",
"644",
],
[
Expand Down Expand Up @@ -81,7 +82,7 @@ assets = [
copyright = "2023-2024 NJUPT-SAST"
changelog = "packaging/deb/changelog"
features = ["default"]
conf-files = ["/etc/rsjudge/server.toml", "/etc/rsjudge/executors.toml"]
conf-files = ["/etc/rsjudge/services.toml", "/etc/rsjudge/executors.toml"]
extended-description = "An online judge sandbox server in Rust, inspired by go-judge, for SASTOJ."
maintainer = "Xiaoqing Xuan <[email protected]>"
maintainer-scripts = "packaging/deb/"
Expand Down Expand Up @@ -152,24 +153,24 @@ clap = { version = "4.5.4", features = ["derive"] }
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 {}
Loading

0 comments on commit ea6e075

Please sign in to comment.