mirror of
https://github.com/thanipro/Axum-Rust-Rest-Api-Template
synced 2024-11-10 06:04:16 +00:00
add required packages, env and database connection
This commit is contained in:
parent
2f3c5daaae
commit
0d53974bc1
10 changed files with 3304 additions and 6 deletions
4
.env
Normal file
4
.env
Normal file
|
@ -0,0 +1,4 @@
|
|||
PORT=8002
|
||||
JWT_SECRET=secret
|
||||
JWT_TTL_IN_MINUTES=30
|
||||
DATABASE_URL=mysql://test-api:test@localhost:3306/test
|
4
.env.example
Normal file
4
.env.example
Normal file
|
@ -0,0 +1,4 @@
|
|||
PORT=8002
|
||||
JWT_SECRET=secret
|
||||
JWT_TTL_IN_MINUTES=30
|
||||
DATABASE_URL=mysql://test-api:test@localhost:3306/test
|
3205
Cargo.lock
generated
3205
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
22
Cargo.toml
22
Cargo.toml
|
@ -1,8 +1,24 @@
|
|||
[package]
|
||||
name = "Axum-Rust-Api-Boilerplate"
|
||||
name = "axum_rust_api_boilerplate"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
axum = { version = "0.6.3"}
|
||||
hyper = { version = "0.14.23", features = ["full"] }
|
||||
tokio = { version = "1.24.2", features = ["full"] }
|
||||
tower = { version = "0.4.13", features = ["full"] }
|
||||
tower-http = { version = "0.4.0", features = ["full"] }
|
||||
dotenv = "0.15.0"
|
||||
serde = { version = "1.0.152", features = ["derive"] }
|
||||
jsonwebtoken = "8.3.0"
|
||||
thiserror = "1.0.40"
|
||||
chrono = {version = "0.4.24", features = ["serde"]}
|
||||
bcrypt = "0.14.0"
|
||||
serde_json = "1.0.95"
|
||||
mockall = "0.11.4"
|
||||
sqlx = { version = "0.6.3", features = [ "runtime-tokio-native-tls", "migrate", "chrono", "time"] }
|
||||
validator = { version = "0.16.0", features = ["derive"] }
|
||||
async-trait = "0.1.68"
|
||||
sqlx-cli = "0.6.3"
|
||||
|
||||
|
|
28
src/config/database.rs
Normal file
28
src/config/database.rs
Normal file
|
@ -0,0 +1,28 @@
|
|||
use crate::parameter;
|
||||
use async_trait::async_trait;
|
||||
use sqlx::{Error, MySql, MySqlPool, Pool};
|
||||
|
||||
pub struct Database {
|
||||
pool: Pool<MySql>,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait DatabaseTrait {
|
||||
async fn init() -> Result<Self, Error>
|
||||
where
|
||||
Self: Sized;
|
||||
fn get_pool(&self) -> &Pool<MySql>;
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl DatabaseTrait for Database {
|
||||
async fn init() -> Result<Self, Error> {
|
||||
let database_url = parameter::get("DATABASE_URL");
|
||||
let pool = MySqlPool::connect(&database_url).await?;
|
||||
Ok(Self { pool })
|
||||
}
|
||||
|
||||
fn get_pool(&self) -> &Pool<MySql> {
|
||||
&self.pool
|
||||
}
|
||||
}
|
2
src/config/mod.rs
Normal file
2
src/config/mod.rs
Normal file
|
@ -0,0 +1,2 @@
|
|||
pub mod database;
|
||||
pub mod parameter;
|
11
src/config/parameter.rs
Normal file
11
src/config/parameter.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
use dotenv;
|
||||
|
||||
pub fn init() {
|
||||
dotenv::dotenv().ok().expect("Failed to load .env file");
|
||||
}
|
||||
|
||||
pub fn get(parameter: &str) -> String {
|
||||
let env_parameter = std::env::var(parameter)
|
||||
.expect(&format!("{} is not defined in the environment.", parameter));
|
||||
return env_parameter;
|
||||
}
|
21
src/main.rs
21
src/main.rs
|
@ -1,3 +1,20 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
use std::sync::Arc;
|
||||
use crate::config::{database, parameter};
|
||||
use crate::config::database::DatabaseTrait;
|
||||
|
||||
mod config;
|
||||
mod routes;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
parameter::init();
|
||||
let connection = database::Database::init()
|
||||
.await
|
||||
.unwrap_or_else(|e| panic!("Database error: {}", e.to_string()));
|
||||
|
||||
let host = format!("0.0.0.0:{}", parameter::get("PORT"));
|
||||
axum::Server::bind(&host.parse().unwrap())
|
||||
.serve(routes::root::routes(Arc::new(connection)))
|
||||
.await
|
||||
.unwrap_or_else(|e| panic!("Server error: {}", e.to_string()));
|
||||
}
|
||||
|
|
1
src/routes/mod.rs
Normal file
1
src/routes/mod.rs
Normal file
|
@ -0,0 +1 @@
|
|||
pub mod root;
|
12
src/routes/root.rs
Normal file
12
src/routes/root.rs
Normal file
|
@ -0,0 +1,12 @@
|
|||
use std::sync::Arc;
|
||||
use crate::config::database::Database;
|
||||
use axum::routing::{get, IntoMakeService};
|
||||
use axum::{middleware, Router};
|
||||
|
||||
pub fn routes(db_conn: Arc<Database>) -> IntoMakeService<Router> {
|
||||
|
||||
let app_router = Router::new()
|
||||
.nest("/api", Router::new().route("/health", get(|| async { "Healthy..." })));
|
||||
|
||||
app_router.into_make_service()
|
||||
}
|
Loading…
Reference in a new issue