From 4c68348d4ef102f5038cae6bd7dcb96f3b21b083 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=9CM=C3=9C=C5=9E?= <96421894+Tahinli@users.noreply.github.com> Date: Wed, 27 Mar 2024 04:58:45 +0300 Subject: [PATCH] feat: :sparkles: base https server --- .gitignore | 7 +++++++ Cargo.toml | 14 ++++++++++++++ src/db.rs | 0 src/lib.rs | 5 +++++ src/main.rs | 33 +++++++++++++++++++++++++++++++++ src/routing.rs | 19 +++++++++++++++++++ 6 files changed, 78 insertions(+) create mode 100644 Cargo.toml create mode 100644 src/db.rs create mode 100644 src/lib.rs create mode 100644 src/main.rs create mode 100644 src/routing.rs diff --git a/.gitignore b/.gitignore index 6985cf1..0e1ea6d 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,8 @@ # will have compiled files and executables debug/ target/ +.vscode/ +certificates/ # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html @@ -12,3 +14,8 @@ Cargo.lock # MSVC Windows builds of rustc generate these, which store debugging information *.pdb + + +# Added by cargo + +/target diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..fa18289 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "acapair_chat_api" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +axum = "0.7.5" +axum-server = { version = "0.6.0", features = ["tls-rustls"] } +serde = { version = "1.0.197", features = ["derive"] } +serde_json = "1.0.115" +tokio = { version = "1.36.0", features = ["full"] } +tower-http = { version = "0.5.2", features = ["full"] } diff --git a/src/db.rs b/src/db.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..e46c61a --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,5 @@ +pub mod routing; +pub mod db; + +#[derive(Debug, Clone)] +pub struct AppState {} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..be22ea9 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,33 @@ +use std::{env, net::SocketAddr}; + +use acapair_chat_api::{routing, AppState}; +use axum_server::tls_rustls::RustlsConfig; + +fn take_args() -> String { + let mut bind_address: String = String::new(); + for element in env::args() { + bind_address = element; + } + println!("\n\n\tOn Air -> https://{}\n\n", bind_address); + bind_address +} + +#[tokio::main] +async fn main() { + println!("Hello, world!"); + + let config = + RustlsConfig::from_pem_file("certificates/fullchain.pem", "certificates/privkey.pem") + .await + .unwrap(); + + let state = AppState {}; + + let app = routing::routing(axum::extract::State(state)).await; + let addr = SocketAddr::from(take_args().parse::().unwrap()); + + axum_server::bind_rustls(addr, config) + .serve(app.into_make_service()) + .await + .unwrap(); +} diff --git a/src/routing.rs b/src/routing.rs new file mode 100644 index 0000000..5fdd45d --- /dev/null +++ b/src/routing.rs @@ -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 routing(State(state): State) -> Router { + Router::new() + .route("/", get(alive)) + .layer(CorsLayer::permissive()) + .with_state(state.clone()) +} + +async fn alive() -> impl IntoResponse { + let alive_json = serde_json::json!({ + "status":"Alive", + }); + println!("{}", alive_json); + (StatusCode::OK, Json(alive_json)) +}