feat: db connection and alive endpoint

This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-11-20 00:16:31 +03:00
parent b3e49f414e
commit c61ebe127a
6 changed files with 55 additions and 11 deletions

View file

@ -10,4 +10,4 @@ serde_json = "1.0.128"
sha3 = "0.10.8"
surrealdb = "2.0.4"
tokio = { version = "1.40.0", features = ["full"] }
tower-http = "0.6.1"
tower-http = { version = "0.6.1", features = ["cors"] }

View file

@ -1 +1,8 @@
use surrealdb::{
engine::remote::ws::{Client, Ws},
Surreal,
};
pub async fn establish_connection() -> Surreal<Client> {
Surreal::new::<Ws>("localhost:8000").await.unwrap()
}

View file

@ -1,4 +1,12 @@
pub(crate) mod database;
pub(crate) mod http;
pub(crate) mod package;
pub(crate) mod utils;
use surrealdb::{engine::remote::ws::Client, Surreal};
pub mod database;
pub mod http;
pub mod package;
pub mod routing;
pub mod utils;
#[derive(Debug, Clone)]
pub struct AppState {
pub db_client: Surreal<Client>,
}

View file

@ -1,3 +1,15 @@
fn main() {
use rust_package_manager::{database, routing, AppState};
// use axum::routing::{self};
use tokio::net::TcpListener;
#[tokio::main]
async fn main() {
println!("Hello, world!");
let listener = TcpListener::bind("127.0.0.1:2345").await.unwrap();
let app_state = AppState {
db_client: database::establish_connection().await,
};
let router = routing::route(axum::extract::State(app_state)).await;
axum::serve(listener, router).await.unwrap();
}

View file

@ -54,7 +54,7 @@ pub(crate) struct Version {
}
impl Version {
fn new(first:u8, second:u8, third: u8) -> Self {
fn new(first: u8, second: u8, third: u8) -> Self {
Version {
first,
second,
@ -62,7 +62,7 @@ impl Version {
}
}
fn update(&mut self, first:u8, second:u8, third: u8) -> &Self {
fn update(&mut self, first: u8, second: u8, third: u8) -> &Self {
self.first = first;
self.second = second;
self.third = third;
@ -82,9 +82,7 @@ pub(crate) struct Publisher {
impl Publisher {
fn new(name: String) -> Self {
Publisher {
name,
}
Publisher { name }
}
fn get_name(&self) -> String {

19
src/routing.rs Normal file
View file

@ -0,0 +1,19 @@
use axum::{extract::State, http::StatusCode, response::IntoResponse, routing::get, Json, Router};
use tower_http::cors::CorsLayer;
use crate::AppState;
pub async fn route(app_state: State<AppState>) -> Router {
Router::new()
.route("/", get(alive))
.layer(CorsLayer::permissive())
.with_state(app_state)
}
async fn alive() -> impl IntoResponse {
let alive_json = Json(serde_json::json!({
"server_status": "alive"
}));
(StatusCode::OK, alive_json)
}