Add database (no schemata yet, only axum state)
This commit is contained in:
parent
aae1225d2d
commit
7c453b6808
9 changed files with 1108 additions and 76 deletions
1094
Cargo.lock
generated
1094
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
10
Cargo.toml
10
Cargo.toml
|
|
@ -20,7 +20,15 @@ serde = "1.0.199"
|
|||
serde_json = "1.0.116"
|
||||
json-patch = "1.2.0"
|
||||
|
||||
utoipa = { version = "4.2.0", features = ["axum_extras"] }
|
||||
# utoipa = { version = "4.2.0", features = ["axum_extras"] }
|
||||
sqlx = { version = "0.7.4", features = [
|
||||
"sqlite",
|
||||
"postgres",
|
||||
"any",
|
||||
"macros",
|
||||
"runtime-tokio",
|
||||
"tls-rustls",
|
||||
] }
|
||||
|
||||
[workspace.lints.clippy]
|
||||
uninlined_format_args = "warn"
|
||||
|
|
|
|||
|
|
@ -1,11 +1,12 @@
|
|||
use axum::Router;
|
||||
use sqlx::{Any, Pool};
|
||||
|
||||
// route prefix: `/auth/token/`
|
||||
// mod token;
|
||||
|
||||
// use self::token::token_auth_router;
|
||||
|
||||
pub fn auth_router() -> Router {
|
||||
Router::new()
|
||||
pub fn auth_router(pool: Pool<Any>) -> Router<Pool<Any>> {
|
||||
Router::new().with_state(pool)
|
||||
// .nest("/token", token_auth_router())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
// hello world
|
||||
// hello world
|
||||
|
|
|
|||
|
|
@ -9,11 +9,12 @@ use axum::{
|
|||
Router,
|
||||
};
|
||||
use log::*;
|
||||
use sqlx::{Any, Pool};
|
||||
|
||||
pub fn secrets_router() -> Router {
|
||||
pub fn secrets_router(pool: Pool<Any>) -> Router<Pool<Any>> {
|
||||
// Router::new().layer(map_request(handler))
|
||||
|
||||
Router::new()
|
||||
Router::new().with_state(pool)
|
||||
// .nest("/:path", kv2::asdasdsadsd())
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
use axum::Router;
|
||||
use sqlx::{Any, Pool};
|
||||
|
||||
pub fn identity_router() -> Router {
|
||||
Router::new()
|
||||
pub fn identity_router(pool: Pool<Any>) -> Router<Pool<Any>> {
|
||||
Router::new().with_state(pool)
|
||||
}
|
||||
|
|
|
|||
39
src/main.rs
39
src/main.rs
|
|
@ -1,10 +1,11 @@
|
|||
use axum::{extract::Request, http::StatusCode, routing::get, Router};
|
||||
use log::*;
|
||||
use serde::Deserialize;
|
||||
use sqlx::{Any, Pool};
|
||||
use std::{env, net::SocketAddr, str::FromStr};
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
use crate::engines::kv::logic::body_to_json;
|
||||
use crate::{engines::kv::logic::body_to_json, storage::create_pool};
|
||||
|
||||
mod auth;
|
||||
mod common;
|
||||
|
|
@ -16,22 +17,28 @@ mod sys;
|
|||
#[tokio::main]
|
||||
async fn main() {
|
||||
// To be configured via environment variables
|
||||
env::set_var("RUST_LOG", "trace"); // Used to set level of logging -> choose from (highest to lowest): error, warn, info, debug, trace, off
|
||||
// choose from (highest to lowest): error, warn, info, debug, trace, off
|
||||
env::set_var("RUST_LOG", "trace"); // TODO: Remove to respect user configuration
|
||||
env_logger::init();
|
||||
|
||||
// Listen on all IPv4 and IPv6 interfaces on port 8200
|
||||
let listen_addr = env::var("LISTEN_ADDR").unwrap_or("[::]:8200".to_string()); // Do not change
|
||||
let listen_addr = SocketAddr::from_str(&listen_addr).expect("Failed to parse LISTEN_ADDR");
|
||||
|
||||
let db_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
||||
|
||||
let pool: Pool<Any> = create_pool(db_url).await;
|
||||
|
||||
// build our application with routes
|
||||
let app = Router::new()
|
||||
.route("/", get(root))
|
||||
.nest("/v1/auth", auth::auth_router())
|
||||
.nest("/v1/identity", identity::identity_router())
|
||||
.nest("/v1/sys", sys::sys_router())
|
||||
.nest("/v1", engines::secrets_router()) // mountable secret backends
|
||||
.nest("/v1/auth", auth::auth_router(pool.clone()))
|
||||
.nest("/v1/identity", identity::identity_router(pool.clone()))
|
||||
.nest("/v1/sys", sys::sys_router(pool.clone()))
|
||||
.nest("/v1", engines::secrets_router(pool.clone())) // mountable secret backends
|
||||
// .route("/v1/kv-v2/data/foo", post(baz))
|
||||
.fallback(fallback_route_unknown);
|
||||
.fallback(fallback_route_unknown)
|
||||
.with_state(pool);
|
||||
|
||||
warn!("Listening on {}", listen_addr.to_string());
|
||||
// Start listening
|
||||
|
|
@ -39,24 +46,6 @@ async fn main() {
|
|||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct CreateSecret {
|
||||
password1: String,
|
||||
password2: String,
|
||||
}
|
||||
|
||||
/// Routing handler for path "/v1/kw_mount_path/data/foo"
|
||||
/// expects payload as JSON, prints payload into struct
|
||||
async fn baz(body: String) -> String {
|
||||
let mut body_json = body_to_json(body);
|
||||
let secret: CreateSecret = CreateSecret {
|
||||
password1: body_json["data"]["password1"].take().to_string(),
|
||||
password2: body_json["data"]["password2"].take().to_string(),
|
||||
};
|
||||
log::debug!("Pass1: {}, Pass2: {}", secret.password1, secret.password2);
|
||||
String::from("RoutingTest baz successful")
|
||||
}
|
||||
|
||||
async fn fallback_route_unknown(req: Request) -> (StatusCode, &'static str) {
|
||||
log::error!(
|
||||
"Route not found: {} {}, payload {:?}",
|
||||
|
|
|
|||
|
|
@ -1 +1,18 @@
|
|||
use sqlx::{
|
||||
any::{install_default_drivers, AnyPoolOptions},
|
||||
AnyPool,
|
||||
};
|
||||
|
||||
pub async fn create_pool(connection_string: String) -> AnyPool {
|
||||
// TODO: Change to solely install applicable driver
|
||||
install_default_drivers();
|
||||
|
||||
let pool = AnyPoolOptions::new()
|
||||
.max_connections(5)
|
||||
.test_before_acquire(true)
|
||||
.connect(&connection_string)
|
||||
.await
|
||||
.expect(&connection_string);
|
||||
|
||||
pool
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
use axum::Router;
|
||||
use sqlx::{Any, Pool};
|
||||
|
||||
/// System routes
|
||||
///
|
||||
pub fn sys_router() -> Router {
|
||||
Router::new()
|
||||
///
|
||||
pub fn sys_router(pool: Pool<Any>) -> Router<Pool<Any>> {
|
||||
Router::new().with_state(pool)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue