Skip to content

Commit

Permalink
update CI
Browse files Browse the repository at this point in the history
  • Loading branch information
Eason0729 committed Feb 1, 2024
1 parent 015c09d commit 0424fe5
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 32 deletions.
6 changes: 3 additions & 3 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ opentelemetry-stdout = { version = "0.2.0", features = ["metrics"] }
opentelemetry-semantic-conventions = "0.13.0"
opentelemetry-otlp = { version = "0.14.0", features = ["metrics"] }
migration = { path = "./migration",optional = true }
sea-orm-cli = "0.12.12"
sea-orm-cli = {version = "0.12.12",optional = true}

[dependencies.chrono]
workspace = true
Expand Down Expand Up @@ -72,7 +72,7 @@ features = ["macros", "rt-multi-thread", "full", "time"]
version = "0.12.11"
default-features = false
features = [
"runtime-tokio-rustls",
"runtime-tokio",
"macros",
"mock",
"sqlx-sqlite",
Expand Down Expand Up @@ -106,4 +106,4 @@ tonic-build = { workspace = true }

[features]
default = []
standalone = ["dep:migration","dep:sea-orm-migration"]
standalone = ["dep:migration","dep:sea-orm-migration","dep:sea-orm-cli"]
2 changes: 1 addition & 1 deletion backend/migration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ features = ["attributes", "tokio1"]
version = "0.12.11"
default-features = false
features = [
"runtime-tokio-rustls",
"runtime-async-std-rustls",
"macros",
"mock",
"sqlx-sqlite",
Expand Down
10 changes: 6 additions & 4 deletions backend/migration/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ ARG ARCH
WORKDIR /complier

RUN apt update -y
RUN apt install musl-tools protobuf-compiler pkg-config openssl make -y
RUN apt install musl-tools protobuf-compiler pkg-config make -y

RUN cargo install just

RUN rustup target add ${ARCH}-unknown-linux-musl

WORKDIR /complier
COPY . .

RUN rustup target add ${ARCH}-unknown-linux-musl

RUN cargo install --target ${ARCH}-unknown-linux-musl --path backend/migration

FROM debian
FROM scratch
COPY --from=builder /usr/local/cargo/bin/migration /
RUN apt install openssl -y

CMD ["/migration"]
1 change: 1 addition & 0 deletions backend/src/endpoint/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! collection of endpoint implementation
mod announcement;
mod chat;
mod contest;
Expand Down
1 change: 0 additions & 1 deletion backend/src/entity/contest.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::ops::Deref;

use sea_orm::{DatabaseBackend, Statement};
use sea_query::QueryStatementBuilder;

use crate::{grpc::backend::ContestSortBy, partial_union};

Expand Down
1 change: 1 addition & 0 deletions backend/src/entity/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! collection of entity
use sea_orm::{
entity::prelude::*, EntityTrait, FromQueryResult, PrimaryKeyTrait, QueryFilter, Select,
};
Expand Down
6 changes: 3 additions & 3 deletions backend/src/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ pub mod judger {
pub mod backend {
tonic::include_proto!("oj.backend");
}

/// convert chrono's time to prost_types's
pub fn into_prost(time: chrono::NaiveDateTime) -> prost_types::Timestamp {
prost_types::Timestamp {
seconds: time.timestamp(),
nanos: time.timestamp_subsec_nanos() as i32,
}
}

/// convert prost_types's time to chrono's
pub fn into_chrono(time: prost_types::Timestamp) -> chrono::NaiveDateTime {
chrono::NaiveDateTime::from_timestamp_opt(time.seconds, time.nanos as u32).unwrap_or_default()
}

/// server side stream in tonic
pub type TonicStream<T> =
std::pin::Pin<Box<dyn tokio_stream::Stream<Item = Result<T, tonic::Status>> + Send>>;

Expand Down
20 changes: 8 additions & 12 deletions backend/src/init/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ use super::config::{self};
use crate::{controller::crypto::CryptoController, util::auth::RoleLv};

#[instrument(skip_all, name = "construct_db",parent=span)]
/// initialize the database and connection
///
/// 1. Connect to database.
/// 2. Check and run migration.(skip when not(feature="standalone"))
/// 3. insert user admin@admin if there is no user.
/// 4. return DatabaseConnection
pub async fn init(
config: &config::Database,
crypto: &CryptoController,
Expand Down Expand Up @@ -39,18 +45,7 @@ pub async fn init(
}

#[cfg(feature = "standalone")]
async fn migrate(db: &DatabaseConnection) {
run_migrate(
::migration::Migrator,
db,
Some(MigrateSubcommands::Up { num: None }),
false,
)
.await
.expect("Unable to setup database migration");
}

#[cfg(feature = "standalone")]
/// Run migration
async fn migrate(db: &DatabaseConnection) {
run_migrate(
::migration::Migrator,
Expand All @@ -63,6 +58,7 @@ async fn migrate(db: &DatabaseConnection) {
}

#[instrument(skip_all, name = "construct_admin")]
/// check if any user exist or inser user admin@admin
async fn init_user(db: &DatabaseConnection, crypto: &CryptoController) {
if crate::entity::user::Entity::find().count(db).await.unwrap() != 0 {
return;
Expand Down
13 changes: 5 additions & 8 deletions backend/src/init/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
//! Procedural for initialization
//!
//! This module is heavily couple with crate::server and require refactor

pub mod config;
pub mod db;
pub mod error;
pub mod logger;

// pub async fn new() -> (GlobalConfig, OtelGuard) {
// let config = config::init().await;
// let olp_guard = logger::init(&config);
// db::init(&config.database).await;
// (config, olp_guard)
// }
pub mod logger;
13 changes: 13 additions & 0 deletions backend/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ use crate::{

const MAX_FRAME_SIZE: u32 = 1024 * 1024 * 8;

/// A wrapper to launch server
///
/// [`Server`] doesn't hold state
pub struct Server {
pub token: Arc<token::TokenController>,
pub judger: Arc<judger::JudgerController>,
Expand All @@ -35,6 +38,15 @@ pub struct Server {
}

impl Server {
/// Create a new server
///
/// It will initialize project's stateful components in following order:
/// 1. Config
/// 2. Logger
/// 3. Crypto Controller
/// 4. Other Controller
///
/// Also of note, private/public `*.pem` is loaded during [`Server::start`] instead of this function
pub async fn new() -> Arc<Self> {
let config = config::init().await;

Expand Down Expand Up @@ -65,6 +77,7 @@ impl Server {
_otel_guard: otel_guard,
})
}
/// Start the server
pub async fn start(self: Arc<Self>) {
let mut identity = None;

Expand Down

0 comments on commit 0424fe5

Please sign in to comment.