Skip to content

Commit

Permalink
refactor(Backend): ➖ use grpc crate for backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Eason0729 committed Jul 2, 2024
1 parent 551c44e commit 539dc72
Show file tree
Hide file tree
Showing 38 changed files with 260 additions and 358 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

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

13 changes: 6 additions & 7 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ codegen-backend = "cranelift"
tikv-jemallocator = { version = "0.5", optional = true }
log = "0.4.18"
paste = "1.0.12"
prost = { workspace = true }
toml = { workspace = true }
prost-types = { workspace = true }
thiserror = "1.0.44"
derive_builder = { workspace = true }
futures = "0.3.29"
Expand All @@ -41,6 +39,11 @@ sea-orm-cli = { version = "0.12.12", optional = true }
governor = "0.6.0"
http = "^0.2"
lazy_static = "1.5.0"
prost-wkt-types = { workspace = true }

[dependencies.grpc]
path = "../grpc"
features = ["server", "client", "backend", "judger", "transport", "serde"]

[dependencies.postcard]
version = "^1.0"
Expand All @@ -66,7 +69,7 @@ features = ["serde"]
[dependencies.reqwest]
version = "0.11.22"
default-features = false
features = ["rustls-tls", "json","multipart"]
features = ["rustls-tls", "json", "multipart"]

[dependencies.k256]
version = "0.13.2"
Expand Down Expand Up @@ -116,10 +119,6 @@ version = "0.12.11"
optional = true
features = ["runtime-tokio-rustls", "sqlx-sqlite", "with-chrono"]

[build-dependencies.tonic-build]
workspace = true
features = ["default"]

[features]
default = ["debug"]
standalone = ["dep:migration", "dep:sea-orm-migration", "dep:sea-orm-cli"]
Expand Down
41 changes: 0 additions & 41 deletions backend/build.rs

This file was deleted.

22 changes: 11 additions & 11 deletions backend/src/controller/imgur.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::HashMap;

use reqwest::{multipart, Client};
use serde::Serialize;
use tracing::instrument;

use crate::{init::config, report_internal};
Expand All @@ -25,16 +24,17 @@ impl From<Error> for tonic::Status {
}
}

/// json serialization for imgur api
///
/// Read Imgur API Docs for more
#[derive(Serialize)]
struct AccessTokenRequest<'a> {
refresh_token: &'a str,
client_id: &'a str,
client_secret: &'a str,
grant_type: &'static str,
}
// FIXME: use token to delete image
// /// json serialization for imgur api
// ///
// /// Read Imgur API Docs for more
// #[derive(Serialize)]
// struct AccessTokenRequest<'a> {
// refresh_token: &'a str,
// client_id: &'a str,
// client_secret: &'a str,
// grant_type: &'static str,
// }

pub struct ImgurController {
client: Client,
Expand Down
14 changes: 3 additions & 11 deletions backend/src/controller/judger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ use std::{
use tokio_stream::StreamExt;

use crate::{
grpc::{backend::StateCode as BackendCode, TonicStream},
init::{config, logger::PACKAGE_NAME},
report_internal,
report_internal, TonicStream,
};
use grpc::backend::StateCode as BackendCode;
use opentelemetry::{global, metrics::ObservableGauge};
use sea_orm::{ActiveModelTrait, ActiveValue, DatabaseConnection, EntityTrait, QueryOrder};
use thiserror::Error;
use tonic::Status;
use tracing::{instrument, Instrument, Span};
use uuid::Uuid;

use crate::grpc::{
use grpc::{
backend::{submit_status, PlaygroundResult, SubmitStatus},
judger::*,
};
Expand Down Expand Up @@ -95,14 +95,6 @@ pub struct Submit {
code: Vec<u8>,
}

impl From<i32> for SubmitStatus {
fn from(value: i32) -> Self {
SubmitStatus {
task: Some(submit_status::Task::Case(value)),
}
}
}

impl From<Code> for SubmitStatus {
fn from(value: Code) -> Self {
SubmitStatus {
Expand Down
6 changes: 2 additions & 4 deletions backend/src/controller/judger/route/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ use tonic::{service::Interceptor, *};
use tracing::{debug_span, instrument, span, Instrument, Level, Span};
use uuid::Uuid;

use crate::{
grpc::judger::{judger_client::*, *},
init::config::{self, Judger as JudgerConfig},
};
use crate::init::config::{self, Judger as JudgerConfig};
use grpc::judger::{judger_client::*, *};

// TODO: add tracing

Expand Down
21 changes: 5 additions & 16 deletions backend/src/endpoint/announcement.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
use super::tools::*;

use crate::grpc::backend::announcement_set_server::*;
use crate::grpc::backend::*;
use crate::grpc::into_prost;
use crate::util::time::into_prost;
use grpc::backend::announcement_set_server::*;
use grpc::backend::*;

use crate::entity::announcement::*;
use crate::entity::*;
use crate::NonZeroU32;

impl From<i32> for AnnouncementId {
fn from(value: i32) -> Self {
Self { id: value }
}
}

impl From<AnnouncementId> for i32 {
fn from(value: AnnouncementId) -> Self {
value.id
}
}
use crate::NonZeroU32;

impl From<Model> for AnnouncementFullInfo {
fn from(value: Model) -> Self {
Expand Down Expand Up @@ -56,7 +45,7 @@ impl From<PartialModel> for AnnouncementInfo {
}

#[async_trait]
impl AnnouncementSet for Arc<Server> {
impl AnnouncementSet for ArcServer {
#[instrument(skip_all, level = "debug")]
async fn list(
&self,
Expand Down
19 changes: 3 additions & 16 deletions backend/src/endpoint/chat.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
use super::tools::*;

use crate::grpc::backend::chat_set_server::*;
use crate::grpc::backend::*;
use grpc::backend::chat_set_server::*;
use grpc::backend::*;

use crate::entity::chat::*;
use crate::grpc::into_prost;

impl From<i32> for ChatId {
fn from(value: i32) -> Self {
ChatId { id: value }
}
}

impl From<ChatId> for i32 {
fn from(value: ChatId) -> Self {
value.id
}
}

impl From<Model> for ChatInfo {
fn from(value: Model) -> Self {
Expand All @@ -31,7 +18,7 @@ impl From<Model> for ChatInfo {
}

#[tonic::async_trait]
impl ChatSet for Arc<Server> {
impl ChatSet for ArcServer {
async fn create(&self, req: Request<CreateChatRequest>) -> Result<Response<ChatId>, Status> {
let (auth, req) = self
.parse_request_n(req, NonZeroU32!(5))
Expand Down
20 changes: 4 additions & 16 deletions backend/src/endpoint/contest.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
use super::tools::*;

use crate::entity::{contest::*, *};
use crate::grpc::backend::contest_set_server::*;
use crate::grpc::backend::*;
use crate::grpc::into_chrono;
use crate::grpc::into_prost;

impl From<i32> for ContestId {
fn from(value: i32) -> Self {
Self { id: value }
}
}
impl From<ContestId> for i32 {
fn from(value: ContestId) -> Self {
value.id
}
}

use grpc::backend::contest_set_server::*;
use grpc::backend::*;

impl From<Model> for ContestFullInfo {
fn from(value: Model) -> Self {
Expand Down Expand Up @@ -61,7 +49,7 @@ impl From<PartialModel> for ContestInfo {
}

#[async_trait]
impl ContestSet for Arc<Server> {
impl ContestSet for ArcServer {
#[instrument(skip_all, level = "debug")]
async fn list(
&self,
Expand Down
18 changes: 3 additions & 15 deletions backend/src/endpoint/education.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
use super::tools::*;

use crate::grpc::backend::education_set_server::*;
use crate::grpc::backend::*;
use grpc::backend::education_set_server::*;
use grpc::backend::*;

use crate::entity::{education::Paginator, education::*, *};

impl From<i32> for EducationId {
fn from(value: i32) -> Self {
Self { id: value }
}
}

impl From<EducationId> for i32 {
fn from(value: EducationId) -> Self {
value.id
}
}

impl From<Model> for EducationFullInfo {
fn from(value: Model) -> Self {
EducationFullInfo {
Expand All @@ -39,7 +27,7 @@ impl From<PartialModel> for EducationInfo {
}

#[async_trait]
impl EducationSet for Arc<Server> {
impl EducationSet for ArcServer {
async fn list(
&self,
req: Request<ListEducationRequest>,
Expand Down
6 changes: 3 additions & 3 deletions backend/src/endpoint/imgur.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use super::tools::*;

use crate::grpc::backend::imgur_set_server::*;
use crate::grpc::backend::*;
use grpc::backend::imgur_set_server::*;
use grpc::backend::*;

#[async_trait]
impl ImgurSet for Arc<Server> {
impl ImgurSet for ArcServer {
#[instrument(skip_all, level = "debug")]
async fn upload(
&self,
Expand Down
20 changes: 10 additions & 10 deletions backend/src/endpoint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@ mod tools {
pub const SHORT_ART_SIZE: usize = 128;
/// longest allowed size for long string
pub const LONG_ART_SIZE: usize = 65536;
pub use crate::entity::util::paginator::{Pager, Remain};

pub use crate::grpc::TonicStream;
pub use crate::NonZeroU32;
pub use sea_orm::*;
pub use std::ops::Deref;
pub use tonic::*;
pub use tracing::*;
pub use uuid::Uuid;

pub use crate::entity::util::filter::*;
pub use crate::entity::util::{
filter::*,
paginator::{Pager, Remain},
};
pub use crate::util::{
auth::RoleLv,
error::{atomic_fail, Error},
time::*,
};
pub use crate::{
check_exist_length, check_length, fill_active_model, fill_exist_active_model,
parse_pager_param, server::Server,
parse_pager_param, server::ArcServer, TonicStream,
};
pub use sea_orm::*;
pub use std::sync::Arc;
pub use tonic::*;
pub use tracing::*;
pub use uuid::Uuid;
}

// FIXME: currently we report transaction error as internal error,
Expand Down
Loading

0 comments on commit 539dc72

Please sign in to comment.