This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
683 additions
and
306 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,6 @@ | ||
[default] | ||
address = "0.0.0.0" | ||
# no file lock, worker has to be 1 | ||
workers = 1 | ||
|
||
[debug] | ||
port = 8000 | ||
limits = { json = "50 MiB" } | ||
|
||
[release] | ||
port = 9999 | ||
ip_header = false | ||
limits = { json = "10 MiB" } | ||
[default.databases.sea_orm] | ||
# sqlite | ||
url = "sqlite://db.sqlite?mode=rwc" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[package] | ||
name = "plagiarism-detector-rust-api" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] | ||
async-stream = { version = "0.3" } | ||
async-trait = { version = "0.1" } | ||
plagiarism-detector-rust-service = { path = "../service" } | ||
futures = { version = "0.3" } | ||
futures-util = { version = "0.3" } | ||
rocket = { version = "0.5.0-rc.4", features = ["json"] } | ||
rocket_dyn_templates = { version = "0.1.0-rc.1", features = ["tera"] } | ||
serde_json = { version = "1" } | ||
entity = { path = "../entity" } | ||
migration = { path = "../migration" } | ||
tokio = "1.29.0" | ||
jieba-rs = "0.6" | ||
|
||
[dependencies.sea-orm-rocket] | ||
# path = "../../../sea-orm-rocket/lib" # remove this line in your own project and uncomment the following line | ||
version = "0.5.3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/// Get stopword list from stopwords.txt | ||
pub fn stop_words() -> std::collections::HashSet<String> { | ||
let mut words = std::collections::HashSet::new(); | ||
for item in String::from_utf8_lossy(include_bytes!("../../stopwords.txt")).split_whitespace() { | ||
words.insert(item.to_string()); | ||
} | ||
|
||
words | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
extern crate rocket; | ||
|
||
use rocket::fairing::{self, AdHoc}; | ||
use rocket::serde::json::Json; | ||
use rocket::{Build, Rocket}; | ||
|
||
use migration::MigratorTrait; | ||
use sea_orm_rocket::{Connection, Database}; | ||
|
||
mod pool; | ||
use ::entity::req::*; | ||
use pool::Db; | ||
|
||
mod data; | ||
mod process; | ||
|
||
pub use entity::paper; | ||
pub use entity::paper::Entity as Post; | ||
|
||
#[rocket::post("/", format = "json", data = "<data>")] | ||
async fn check( | ||
data: Json<ReqData>, | ||
state: &rocket::State<SharedData>, | ||
conn: Connection<'_, Db>, | ||
) -> Json<ResData> { | ||
let req: ReqData = data.into_inner(); | ||
let db = conn.into_inner(); | ||
|
||
match process::similarity(&req, req.write, db, &state.jieba, &state.stop_words).await { | ||
Ok(r) => Json(ResData { | ||
code: 200, | ||
msg: format!("{}成功", if req.write { "加入" } else { "查询" }), | ||
similarity: r, | ||
}), | ||
Err(e) => Json(ResData { | ||
code: 500, | ||
msg: e.to_string(), | ||
similarity: [].to_vec(), | ||
}), | ||
} | ||
} | ||
|
||
async fn run_migrations(rocket: Rocket<Build>) -> fairing::Result { | ||
let conn = &Db::fetch(&rocket).unwrap().conn; | ||
let _ = migration::Migrator::up(conn, None).await; | ||
Ok(rocket) | ||
} | ||
|
||
#[tokio::main] | ||
async fn start() -> Result<(), rocket::Error> { | ||
rocket::build() | ||
.attach(Db::init()) | ||
.attach(AdHoc::try_on_ignite("Migrations", run_migrations)) | ||
.mount("/", rocket::routes![check]) | ||
.manage(SharedData { | ||
jieba: jieba_rs::Jieba::new(), | ||
stop_words: data::stop_words(), | ||
}) | ||
.launch() | ||
.await | ||
.map(|_| ()) | ||
} | ||
|
||
pub fn main() { | ||
let result = start(); | ||
|
||
println!("Rocket: deorbit."); | ||
|
||
if let Some(err) = result.err() { | ||
println!("Error: {err}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use plagiarism_detector_rust_service::sea_orm; | ||
|
||
use async_trait::async_trait; | ||
use sea_orm::ConnectOptions; | ||
use sea_orm_rocket::{rocket::figment::Figment, Config, Database}; | ||
use std::time::Duration; | ||
|
||
#[derive(Database, Debug)] | ||
#[database("sea_orm")] | ||
pub struct Db(SeaOrmPool); | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct SeaOrmPool { | ||
pub conn: sea_orm::DatabaseConnection, | ||
} | ||
|
||
#[async_trait] | ||
impl sea_orm_rocket::Pool for SeaOrmPool { | ||
type Error = sea_orm::DbErr; | ||
|
||
type Connection = sea_orm::DatabaseConnection; | ||
|
||
async fn init(figment: &Figment) -> Result<Self, Self::Error> { | ||
let config = figment.extract::<Config>().unwrap(); | ||
let mut options: ConnectOptions = config.url.into(); | ||
options | ||
.max_connections(config.max_connections as u32) | ||
.min_connections(config.min_connections.unwrap_or_default()) | ||
.connect_timeout(Duration::from_secs(config.connect_timeout)) | ||
.sqlx_logging(false); | ||
if let Some(idle_timeout) = config.idle_timeout { | ||
options.idle_timeout(Duration::from_secs(idle_timeout)); | ||
} | ||
let conn = sea_orm::Database::connect(options).await?; | ||
|
||
Ok(SeaOrmPool { conn }) | ||
} | ||
|
||
fn borrow(&self) -> &Self::Connection { | ||
&self.conn | ||
} | ||
} |
Oops, something went wrong.