feat: base https server

This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-03-27 04:58:45 +03:00
parent 2b4b1999d3
commit 4c68348d4e
6 changed files with 78 additions and 0 deletions

7
.gitignore vendored
View file

@ -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

14
Cargo.toml Normal file
View file

@ -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"] }

0
src/db.rs Normal file
View file

5
src/lib.rs Normal file
View file

@ -0,0 +1,5 @@
pub mod routing;
pub mod db;
#[derive(Debug, Clone)]
pub struct AppState {}

33
src/main.rs Normal file
View file

@ -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::<SocketAddr>().unwrap());
axum_server::bind_rustls(addr, config)
.serve(app.into_make_service())
.await
.unwrap();
}

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 routing(State(state): State<AppState>) -> 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))
}