Skip to content

Commit

Permalink
move server from main to lib
Browse files Browse the repository at this point in the history
Signed-off-by: remzi <[email protected]>
  • Loading branch information
HaoYang670 committed Apr 30, 2024
1 parent 5cff365 commit 34b780e
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 33 deletions.
2 changes: 1 addition & 1 deletion rucat_server/src/authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ enum Credentials {
}

/// Bear authentication
pub async fn auth(
pub(crate) async fn auth(
headers: HeaderMap,
request: Request,
next: Next,
Expand Down
2 changes: 1 addition & 1 deletion rucat_server/src/cluster_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ async fn list_clusters(State(state): State<AppState<'_>>) {
}

/// Pass the data store endpoint later
pub fn get_cluster_router() -> Router<AppState<'static>> {
pub(crate) fn get_cluster_router() -> Router<AppState<'static>> {
Router::new()
.route("/", post(create_cluster))
.route("/:id", get(get_cluster))
Expand Down
31 changes: 28 additions & 3 deletions rucat_server/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
pub mod authentication;
pub mod cluster_router;
pub mod state;
use authentication::auth;
use axum::{middleware, Router};
use cluster_router::get_cluster_router;
use rucat_common::error::Result;
use state::{data_store::DataStore, AppState};
use surrealdb::{engine::local::Mem, Surreal};
use tower_http::trace::TraceLayer;

pub(crate) mod authentication;
pub(crate) mod cluster_router;
pub(crate) mod state;

/// This is the only entry for users to get the rucat server.
pub async fn get_server() -> Result<Router> {
// setup tracing
tracing_subscriber::fmt::init();

let db = Surreal::new::<Mem>(()).await?;
db.use_ns("test").use_db("test").await?;

let app_state = AppState::new(DataStore::connect_embedded_db(db));

Ok(Router::new()
.nest("/cluster", get_cluster_router())
.layer(middleware::from_fn(auth))
.layer(TraceLayer::new_for_http())
.with_state(app_state))
}
23 changes: 2 additions & 21 deletions rucat_server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,11 @@
use axum::{middleware, Router};
use rucat_common::error::Result;
use rucat_server::{
authentication::auth,
cluster_router::get_cluster_router,
state::{data_store::DataStore, AppState},
};
use surrealdb::{engine::local::Mem, Surreal};
use tower_http::trace::TraceLayer;
use rucat_server::get_server;

#[tokio::main]
/// Start Rucat server
async fn main() -> Result<()> {
static ENDPOINT: &str = "127.0.0.1:3000";
// setup tracing
tracing_subscriber::fmt::init();

let db = Surreal::new::<Mem>(()).await?;
db.use_ns("test").use_db("test").await?;

let app_state = AppState::new(DataStore::connect_embedded_db(db));

let app = Router::new()
.nest("/cluster", get_cluster_router())
.layer(middleware::from_fn(auth))
.layer(TraceLayer::new_for_http())
.with_state(app_state);
let app = get_server().await?;

// run it
let listener = tokio::net::TcpListener::bind(ENDPOINT).await?;
Expand Down
6 changes: 3 additions & 3 deletions rucat_server/src/state/data_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct Record {
/// Store the metadata of Cluster
/// The lifetime here reprensent that of the URI of the DB server.
#[derive(Clone)]
pub enum DataStore<'a> {
pub(crate) enum DataStore<'a> {
/// embedded database in memory
Embedded {
store: Surreal<Db>, //embedded surrealdb?
Expand All @@ -31,12 +31,12 @@ impl<'a> DataStore<'a> {
const TABLE: &'static str = "clusters";

/// use an in memory data store
pub fn connect_embedded_db(db: Surreal<Db>) -> Self {
pub(crate) fn connect_embedded_db(db: Surreal<Db>) -> Self {
Self::Embedded { store: db }
}

/// data store that connects to a SurrealDB
pub fn connect_serreal_db(uri: SurrealDBURI<'a>) -> Self {
pub(crate) fn connect_serreal_db(uri: SurrealDBURI<'a>) -> Self {
Self::Server { uri }
}

Expand Down
8 changes: 4 additions & 4 deletions rucat_server/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
use self::data_store::DataStore;

pub mod data_store;
pub(crate) mod data_store;

#[derive(Clone)]
pub struct AppState<'a> {
pub(crate) struct AppState<'a> {
data_store: DataStore<'a>,
}

impl<'a> AppState<'a> {
pub fn new(data_store: DataStore<'a>) -> Self {
pub(crate) fn new(data_store: DataStore<'a>) -> Self {
Self { data_store }
}

pub fn get_data_store(&self) -> &DataStore {
pub(crate) fn get_data_store(&self) -> &DataStore {
&self.data_store
}
}

0 comments on commit 34b780e

Please sign in to comment.