Switch from Pool<Any> to Pool<Sqlite>

This commit is contained in:
Laurenz 2024-05-02 13:40:02 +02:00
parent 876a784e90
commit 049e8374ab
8 changed files with 29 additions and 32 deletions

View file

@ -3,12 +3,6 @@ name = "server"
version = "0.1.0"
edition = "2021"
# [target.x86_64-unknown-linux-gnu]
# rustflags = ["-C", "linker=clang", "-C", "link-arg=-fuse-ld=lld"]
[lints]
workspace = true
[dependencies]
log = "0.4.21"
env_logger = "0.11.3"
@ -24,12 +18,15 @@ json-patch = "1.2.0"
sqlx = { version = "0.7.4", features = [
"sqlite",
# "postgres",
"any",
# "any",
"macros",
"runtime-tokio",
"tls-rustls",
] }
[lints]
workspace = true
[workspace.lints.clippy]
uninlined_format_args = "warn"
correctness = "warn"

View file

@ -1,12 +1,13 @@
use axum::Router;
use sqlx::{Any, Pool};
use crate::storage::DatabaseDriver;
// route prefix: `/auth/token/`
// mod token;
// use self::token::token_auth_router;
pub fn auth_router(pool: Pool<Any>) -> Router<Pool<Any>> {
pub fn auth_router(pool: DatabaseDriver) -> Router<DatabaseDriver> {
Router::new().with_state(pool)
// .nest("/token", token_auth_router())
}

View file

@ -7,15 +7,14 @@ use axum::{
Router,
};
use log::*;
use sqlx::{Any, Pool};
pub fn secrets_router(pool: Pool<Any>) -> Router<Pool<Any>> {
use crate::storage::DatabaseDriver;
pub fn secrets_router(pool: DatabaseDriver) -> Router<DatabaseDriver> {
Router::new().fallback(map_mount_points).with_state(pool)
}
async fn map_mount_points(State(_pool): State<Pool<Any>>, req: Request) -> Response<Body> {
// let mount_path = mount_path.trim_start_matches('/');
// debug!("Mount path: {}", mount_path);
async fn map_mount_points(State(pool): State<DatabaseDriver>, req: Request) -> Response<Body> {
debug!("Mount path: {:?}", req);
let mut mount_path: Vec<&str> = req.uri().path().split("/").collect();

View file

@ -1,14 +1,17 @@
// TODO: Remove
#![allow(dead_code)]
pub mod logic;
pub mod structs;
#[cfg(tests)]
#[cfg(test)]
mod tests;
use crate::engines::kv::logic::body_to_json;
use crate::{engines::kv::logic::body_to_json, storage::DatabaseDriver};
use crate::engines::kv::structs::KvSecret;
use axum::{routing::*, Router, extract::Path};
use sqlx::{Any, Pool};
pub fn kv_router(pool: Pool<Any>) -> Router<Pool<Any>> {
pub fn kv_router(pool: DatabaseDriver) -> Router<DatabaseDriver> {
Router::new()
.route("/:mount_path/config", get(get_config))
.route("/:mount_path/config", post(post_config))

View file

@ -1,6 +1,7 @@
use axum::Router;
use sqlx::{Any, Pool};
pub fn identity_router(pool: Pool<Any>) -> Router<Pool<Any>> {
use crate::storage::DatabaseDriver;
pub fn identity_router(pool: DatabaseDriver) -> Router<DatabaseDriver> {
Router::new().with_state(pool)
}

View file

@ -1,11 +1,8 @@
use axum::{extract::Request, http::StatusCode, routing::get, Router};
use log::*;
use sqlx::{Any, Pool};
use std::{env, net::SocketAddr, str::FromStr};
use tokio::net::TcpListener;
use crate::storage::create_pool;
mod auth;
mod common;
mod engines;
@ -27,7 +24,7 @@ async fn main() {
let db_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let pool: Pool<Any> = create_pool(db_url).await;
let pool = storage::create_pool(db_url).await;
// build our application with routes
let app = Router::new()

View file

@ -1,12 +1,11 @@
use std::{fs::File, path::Path};
use log::*;
use sqlx::{any::AnyPoolOptions, AnyPool};
use sqlx::{sqlite::SqlitePoolOptions, Pool, Sqlite};
pub async fn create_pool(db_url: String) -> AnyPool {
// TODO: Change to solely install applicable driver
sqlx::any::install_default_drivers();
pub(crate) type DatabaseDriver = Pool<Sqlite>;
pub async fn create_pool(db_url: String) -> DatabaseDriver {
// Create SQLite database file if it does not exist
if db_url.starts_with("sqlite:") && db_url != ("sqlite::memory:") {
let path = db_url.replace("sqlite:", "");
@ -16,7 +15,7 @@ pub async fn create_pool(db_url: String) -> AnyPool {
}
}
let pool = AnyPoolOptions::new()
let pool = SqlitePoolOptions::new()
.max_connections(5)
.test_before_acquire(true)
.connect(&db_url)

View file

@ -1,8 +1,8 @@
use axum::Router;
use sqlx::{Any, Pool};
use crate::storage::DatabaseDriver;
/// System routes
///
pub fn sys_router(pool: Pool<Any>) -> Router<Pool<Any>> {
pub fn sys_router(pool: DatabaseDriver) -> Router<DatabaseDriver> {
Router::new().with_state(pool)
}