Skip to content

Commit

Permalink
migrated the main api to sqlx
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Sep 27, 2023
1 parent 9a2b0e3 commit ba51918
Show file tree
Hide file tree
Showing 15 changed files with 274 additions and 148 deletions.

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

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

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

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

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

3 changes: 0 additions & 3 deletions server/Cargo.lock

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

1 change: 0 additions & 1 deletion server/main-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ unicode-truncate = "0.2.0"
serde_yaml = "0.9"

# database
diesel = { version = "2.1.1", features = ["default", "sqlite"] }
libsqlite3-sys = { version = "*", features = ["bundled"] }
sqlx = { version = "0.7.1", features = ["sqlite", "runtime-tokio", "migrate", "macros"] }

Expand Down
8 changes: 0 additions & 8 deletions server/main-api/diesel.toml

This file was deleted.

64 changes: 34 additions & 30 deletions server/main-api/src/entries/get.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,43 @@
use crate::models::DBRoomKeyAlias;
use crate::utils;
use crate::{utils, AppState};
use actix_web::{get, web, HttpResponse};
use diesel::prelude::*;
use log::error;
use sqlx::SqlitePool;

#[derive(Debug, Clone)]
pub struct DBRoomKeyAlias {
pub key: String,
pub visible_id: String,
pub r#type: String,
}

#[get("/api/get/{id}")]
pub async fn get_handler(
params: web::Path<String>,
web::Query(args): web::Query<utils::LangQueryArgs>,
data: web::Data<AppState>,
) -> HttpResponse {
let conn = &mut utils::establish_connection();
let (probable_id, redirect_url) = match get_alias_and_redirect(conn, &params.into_inner()) {
Some(alias_and_redirect) => alias_and_redirect,
None => return HttpResponse::NotFound().body("Not found"),
};
let (probable_id, redirect_url) =
match get_alias_and_redirect(&data.db, &params.into_inner()).await {
Some(alias_and_redirect) => alias_and_redirect,
None => return HttpResponse::NotFound().body("Not found"),
};
let result = match args.should_use_english() {
true => {
use crate::schema::en::dsl;
dsl::en
.filter(dsl::key.eq(&probable_id))
.select(dsl::data)
.load::<String>(conn)
sqlx::query_scalar!("SELECT data FROM en WHERE key = ?", probable_id)
.fetch_optional(&data.db)
.await
}
false => {
use crate::schema::de::dsl;
dsl::de
.filter(dsl::key.eq(&probable_id))
.select(dsl::data)
.load::<String>(conn)
sqlx::query_scalar!("SELECT data FROM de WHERE key = ?", probable_id)
.fetch_optional(&data.db)
.await
}
};
match result {
Ok(d) => match d.len() {
0 => HttpResponse::NotFound().body("Not found"),
_ => {
let mut response_json = d[0].clone();
Ok(d) => match d {
None => HttpResponse::NotFound().body("Not found"),
Some(s) => {
let mut response_json = s.clone();
// We don not want to serialise this data at any point in the server.
// This just flows through the server, but adding redirect_url to the response is necessary
response_json.pop(); // remove last }
Expand All @@ -53,18 +56,19 @@ pub async fn get_handler(
}
}

fn get_alias_and_redirect(conn: &mut SqliteConnection, query: &str) -> Option<(String, String)> {
use crate::schema::aliases::dsl::{alias, aliases, key, type_, visible_id};
let result = aliases
.filter(alias.eq(query).or(key.eq(query)))
.select((key, visible_id, type_))
.distinct()
.load::<DBRoomKeyAlias>(conn);
async fn get_alias_and_redirect(conn: &SqlitePool, query: &str) -> Option<(String, String)> {
let result = sqlx::query_as!(
DBRoomKeyAlias,
"SELECT DISTINCT key,visible_id,type FROM aliases WHERE alias = ?",
query
)
.fetch_all(conn)
.await;
match result {
Ok(d) => {
let redirect_url = match d.len() {
0 => return None, // not key or alias
1 => extract_redirect_exact_match(&d[0].type_, &d[0].visible_id),
1 => extract_redirect_exact_match(&d[0].r#type, &d[0].visible_id),
_ => {
let keys = d
.clone()
Expand Down
11 changes: 10 additions & 1 deletion server/main-api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@ use actix_web::{get, middleware, web, App, HttpResponse, HttpServer};
use actix_web_prom::PrometheusMetricsBuilder;
use futures::try_join;
use log::info;
use sqlx::sqlite::SqlitePoolOptions;
use sqlx::SqlitePool;
use std::collections::HashMap;

mod entries;
mod maps;
mod models;
mod schema;
mod search;
mod setup;
mod utils;

const MAX_JSON_PAYLOAD: usize = 1024 * 1024; // 1 MB
pub struct AppState {
db: SqlitePool,
}

#[get("/api/status")]
async fn health_status_handler() -> HttpResponse {
Expand Down Expand Up @@ -47,6 +51,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.build()
.unwrap();

let uri = std::env::var("DB_LOCATION").unwrap_or_else(|_| "api_data.db".to_string());
let uri = format!("{uri}?mode=ro");
let pool = SqlitePoolOptions::new().connect(&uri).await?;

HttpServer::new(move || {
let cors = Cors::default()
.allow_any_origin()
Expand All @@ -59,6 +67,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.wrap(cors)
.wrap(middleware::Logger::default().exclude("/api/status"))
.wrap(middleware::Compress::default())
.app_data(web::Data::new(pool.clone()))
.app_data(web::JsonConfig::default().limit(MAX_JSON_PAYLOAD))
.service(health_status_handler)
.service(web::scope("/api/preview").configure(maps::configure))
Expand Down
Loading

0 comments on commit ba51918

Please sign in to comment.