First commit
This commit is contained in:
commit
ed7d5fb5b7
|
@ -0,0 +1 @@
|
|||
DATABASE_URL=mysql://user:pass@host/dbname
|
|
@ -0,0 +1,4 @@
|
|||
/target
|
||||
/.idea/
|
||||
/.env
|
||||
/inquisitorum.iml
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,13 @@
|
|||
[package]
|
||||
name = "inquisitorum"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
actix-web = "4"
|
||||
sqlx = { version = "0.6.3", features = [ "mysql", "postgres", "migrate", "runtime-actix-native-tls" ] }
|
||||
async-std = { version = "1", features = [ "attributes" ] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
dotenvy_macro = "0.15.7"
|
|
@ -0,0 +1,84 @@
|
|||
use actix_web::{get, web, App, HttpServer, Responder};
|
||||
use dotenvy_macro::dotenv;
|
||||
use serde::Serialize;
|
||||
use sqlx::mysql::MySqlPoolOptions;
|
||||
use sqlx::MySqlPool;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub struct AppState {
|
||||
db: MySqlPool,
|
||||
}
|
||||
|
||||
#[derive(Serialize, sqlx::FromRow)]
|
||||
struct Info {
|
||||
id: u32,
|
||||
title: String,
|
||||
scan: u32,
|
||||
}
|
||||
|
||||
#[derive(Serialize, sqlx::FromRow)]
|
||||
struct Record {
|
||||
key: String,
|
||||
value: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct Process {
|
||||
info: Info,
|
||||
records: HashMap<String, String>,
|
||||
}
|
||||
|
||||
#[get("/degredo")]
|
||||
pub async fn get_degredo(data: web::Data<AppState>) -> actix_web::Result<impl Responder> {
|
||||
let info = sqlx::query_as!(
|
||||
Info,
|
||||
r#"SELECT id, title, scan from processes order by RAND() limit 1"#,
|
||||
)
|
||||
.fetch_one(&data.db)
|
||||
.await
|
||||
.expect("Failed to query");
|
||||
|
||||
let records = sqlx::query_as!(
|
||||
Record,
|
||||
r#"SELECT k.value as `key`, r.value as value from records r JOIN `keys` k ON k.id = r.key_id where process_id = ?"#,
|
||||
info.id
|
||||
)
|
||||
.fetch_all(&data.db)
|
||||
.await
|
||||
.expect("Failed to query");
|
||||
|
||||
let mut hash_records = HashMap::new();
|
||||
for record in records {
|
||||
hash_records.insert(record.key, record.value);
|
||||
}
|
||||
|
||||
let process = Process {
|
||||
info,
|
||||
records: hash_records,
|
||||
};
|
||||
|
||||
Ok(web::Json(process))
|
||||
}
|
||||
|
||||
pub fn config(conf: &mut web::ServiceConfig) {
|
||||
let scope = web::scope("/api").service(get_degredo);
|
||||
conf.service(scope);
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
let pool = MySqlPoolOptions::new()
|
||||
.max_connections(5)
|
||||
.connect(dotenv!("DATABASE_URL"))
|
||||
.await
|
||||
.expect("Failed to create pool");
|
||||
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.app_data(web::Data::new(AppState { db: pool.clone() }))
|
||||
.configure(config)
|
||||
})
|
||||
.bind(("127.0.0.1", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
Loading…
Reference in New Issue