Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit 09f7f09

Browse files
committed
changes to libsqlx
1 parent 110c5fa commit 09f7f09

File tree

17 files changed

+203
-317
lines changed

17 files changed

+203
-317
lines changed

Cargo.lock

Lines changed: 31 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libsqlx-server/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ edition = "2021"
99
axum = "0.6.18"
1010
clap = { version = "4.3.11", features = ["derive"] }
1111
color-eyre = "0.6.2"
12+
futures = "0.3.28"
1213
hyper = { version = "0.14.27", features = ["h2"] }
14+
libsqlx = { version = "0.1.0", path = "../libsqlx" }
15+
serde = { version = "1.0.166", features = ["derive"] }
1316
tokio = { version = "1.29.1", features = ["full"] }
1417
tracing = "0.1.37"
1518
tracing-subscriber = "0.3.17"
19+
uuid = { version = "1.4.0", features = ["v4"] }

libsqlx-server/src/allocation/mod.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::collections::HashMap;
22

3-
use libsqlx::Database;
43
use tokio::{sync::{mpsc, oneshot}, task::{JoinSet, block_in_place}};
54

65
pub mod config;
@@ -26,9 +25,17 @@ enum AllocationMessage {
2625
}
2726
}
2827

28+
enum Database {}
29+
30+
impl Database {
31+
fn connect(&self) -> Box<dyn libsqlx::Connection + Send> {
32+
todo!();
33+
}
34+
}
35+
2936
pub struct Allocation {
3037
inbox: mpsc::Receiver<AllocationMessage>,
31-
database: Box<dyn Database>,
38+
database: Database,
3239
/// senders to the spawned connections
3340
connections: HashMap<u32, mpsc::Sender<ExecFn>>,
3441
/// spawned connection futures, returning their connection id on completion.
@@ -69,7 +76,7 @@ impl Allocation {
6976

7077
async fn new_conn_exec(&mut self, exec: ExecFn) -> ConnectionId {
7178
let id = self.next_conn_id();
72-
let conn = block_in_place(|| self.database.connect()).unwrap();
79+
let conn = block_in_place(|| self.database.connect());
7380
let (close_sender, exit) = mpsc::channel(1);
7481
let (exec_sender, exec_receiver) = mpsc::channel(1);
7582
let conn = Connection {
@@ -103,7 +110,7 @@ impl Allocation {
103110

104111
struct Connection {
105112
id: u32,
106-
conn: Box<dyn libsqlx::Connection>,
113+
conn: Box<dyn libsqlx::Connection + Send>,
107114
exit: mpsc::Receiver<()>,
108115
exec: mpsc::Receiver<ExecFn>,
109116
}

libsqlx-server/src/http/admin.rs

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,53 @@
11
use std::sync::Arc;
22

3-
use axum::Router;
3+
use axum::{extract::State, routing::post, Json, Router};
44
use color_eyre::eyre::Result;
55
use hyper::server::accept::Accept;
6+
use serde::{Deserialize, Serialize};
67
use tokio::io::{AsyncRead, AsyncWrite};
78

8-
pub struct AdminServerConfig { }
9+
use crate::{meta::MetaStore, allocation::config::AllocConfig};
910

10-
struct AdminServerState { }
11+
pub struct AdminServerConfig {}
12+
13+
struct AdminServerState {
14+
meta_store: Arc<MetaStore>,
15+
}
1116

1217
pub async fn run_admin_server<I>(_config: AdminServerConfig, listener: I) -> Result<()>
13-
where I: Accept<Error = std::io::Error>,
14-
I::Conn: AsyncRead + AsyncWrite + Send + Unpin + 'static,
18+
where
19+
I: Accept<Error = std::io::Error>,
20+
I::Conn: AsyncRead + AsyncWrite + Send + Unpin + 'static,
1521
{
16-
let state = AdminServerState { };
17-
let app = Router::new().with_state(Arc::new(state));
18-
axum::Server::builder(listener).serve(app.into_make_service()).await?;
22+
let state = AdminServerState {
23+
meta_store: todo!(),
24+
};
25+
let app = Router::new()
26+
.route("/manage/allocation/create", post(allocate))
27+
.with_state(Arc::new(state));
28+
axum::Server::builder(listener)
29+
.serve(app.into_make_service())
30+
.await?;
1931

2032
Ok(())
2133
}
34+
35+
#[derive(Serialize, Debug)]
36+
struct ErrorResponse {}
37+
38+
#[derive(Serialize, Debug)]
39+
struct AllocateResp { }
40+
41+
#[derive(Deserialize, Debug)]
42+
struct AllocateReq {
43+
alloc_id: String,
44+
config: AllocConfig,
45+
}
46+
47+
async fn allocate(
48+
State(state): State<Arc<AdminServerState>>,
49+
Json(req): Json<AllocateReq>,
50+
) -> Result<Json<AllocateResp>, Json<ErrorResponse>> {
51+
state.meta_store.allocate(&req.alloc_id, &req.config).await;
52+
todo!();
53+
}

libsqlx-server/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ use color_eyre::eyre::Result;
22
use tracing::metadata::LevelFilter;
33
use tracing_subscriber::prelude::*;
44

5+
mod allocation;
6+
mod databases;
57
mod http;
8+
mod meta;
69

710
#[tokio::main]
811
async fn main() -> Result<()> {

libsqlx/src/connection.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,26 @@ pub struct DescribeCol {
2222

2323
pub trait Connection {
2424
/// Executes a query program
25-
fn execute_program<B: ResultBuilder>(
25+
fn execute_program(
2626
&mut self,
2727
pgm: Program,
28-
result_builder: B,
29-
) -> crate::Result<B>;
28+
result_builder: &mut dyn ResultBuilder,
29+
) -> crate::Result<()>;
3030

3131
/// Parse the SQL statement and return information about it.
3232
fn describe(&self, sql: String) -> crate::Result<DescribeResponse>;
3333
}
34+
35+
impl Connection for Box<dyn Connection> {
36+
fn execute_program(
37+
&mut self,
38+
pgm: Program,
39+
result_builder: &mut dyn ResultBuilder,
40+
) -> crate::Result<()> {
41+
self.as_mut().execute_program(pgm, result_builder)
42+
}
43+
44+
fn describe(&self, sql: String) -> crate::Result<DescribeResponse> {
45+
self.as_ref().describe(sql)
46+
}
47+
}

0 commit comments

Comments
 (0)