refactor: restructure project
This commit is contained in:
parent
699f8733e2
commit
89ea0995bb
9 changed files with 57 additions and 44 deletions
24
Cargo.lock
generated
24
Cargo.lock
generated
|
|
@ -468,7 +468,7 @@ dependencies = [
|
||||||
"http-body 1.0.0",
|
"http-body 1.0.0",
|
||||||
"hyper 1.1.0",
|
"hyper 1.1.0",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"socket2 0.5.4",
|
"socket2 0.5.5",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tower",
|
"tower",
|
||||||
"tower-service",
|
"tower-service",
|
||||||
|
|
@ -566,9 +566,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.147"
|
version = "0.2.151"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
|
checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "linux-raw-sys"
|
name = "linux-raw-sys"
|
||||||
|
|
@ -621,9 +621,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mio"
|
name = "mio"
|
||||||
version = "0.8.8"
|
version = "0.8.10"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "927a765cd3fc26206e66b296465fa9d3e5ab003e651c1b3c060e7956d96b19d2"
|
checksum = "8f3d0b296e374a4e6f3c7b0a1f5a51d748a0d34c85e7dc48fc3fa9a87657fe09"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"wasi",
|
"wasi",
|
||||||
|
|
@ -1006,9 +1006,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "socket2"
|
name = "socket2"
|
||||||
version = "0.5.4"
|
version = "0.5.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4031e820eb552adee9295814c0ced9e5cf38ddf1e8b7d566d6de8e2538ea989e"
|
checksum = "7b5fac59a5cb5dd637972e5fca70daf0523c9067fcdc4842f053dae04a18f8e9"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"windows-sys 0.48.0",
|
"windows-sys 0.48.0",
|
||||||
|
|
@ -1062,9 +1062,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio"
|
name = "tokio"
|
||||||
version = "1.32.0"
|
version = "1.35.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "17ed6077ed6cd6c74735e21f37eb16dc3935f96878b1fe961074089cc80893f9"
|
checksum = "c89b4efa943be685f629b149f53829423f8f5531ea21249408e8e2f8671ec104"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"backtrace",
|
"backtrace",
|
||||||
"bytes",
|
"bytes",
|
||||||
|
|
@ -1074,16 +1074,16 @@ dependencies = [
|
||||||
"parking_lot",
|
"parking_lot",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"signal-hook-registry",
|
"signal-hook-registry",
|
||||||
"socket2 0.5.4",
|
"socket2 0.5.5",
|
||||||
"tokio-macros",
|
"tokio-macros",
|
||||||
"windows-sys 0.48.0",
|
"windows-sys 0.48.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio-macros"
|
name = "tokio-macros"
|
||||||
version = "2.1.0"
|
version = "2.2.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e"
|
checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
|
|
|
||||||
0
src/configuration.rs
Normal file
0
src/configuration.rs
Normal file
32
src/lib.rs
32
src/lib.rs
|
|
@ -1,29 +1,3 @@
|
||||||
#![allow(unused)]
|
pub mod configuration;
|
||||||
use axum::extract::{Path, Query};
|
pub mod routes;
|
||||||
use axum::http::{Method, Uri};
|
pub mod startup;
|
||||||
use axum::response::{Html, IntoResponse, Response};
|
|
||||||
use axum::routing::{get, get_service, post, IntoMakeService};
|
|
||||||
use axum::serve::Serve;
|
|
||||||
use axum::{middleware, Json, Router};
|
|
||||||
use serde::Deserialize;
|
|
||||||
use serde_json::{json, Value};
|
|
||||||
use std::net::SocketAddr;
|
|
||||||
use tokio::net::TcpListener;
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
struct FormData {
|
|
||||||
email: String,
|
|
||||||
name: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// API routing
|
|
||||||
pub fn app() -> Router {
|
|
||||||
Router::new()
|
|
||||||
.route("/health_check", get(|| async {}))
|
|
||||||
.route("/subscriptions", post(|| async {}))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Start the server
|
|
||||||
pub fn run(listener: TcpListener) -> Serve<IntoMakeService<Router>, Router> {
|
|
||||||
axum::serve(listener, app().into_make_service())
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
use learn_axum::run;
|
|
||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
|
use learn_axum::startup;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let addr = format!("127.0.0.1:{}", "3000");
|
let addr = format!("127.0.0.1:{}", "3000");
|
||||||
let listener = TcpListener::bind(addr).await.unwrap(); //.expect("Unable to bind to port");
|
let listener = TcpListener::bind(addr).await.unwrap(); //.expect("Unable to bind to port");
|
||||||
run(listener).await.unwrap();
|
startup::run(listener).await.unwrap();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
6
src/routes/health_check.rs
Normal file
6
src/routes/health_check.rs
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
use axum::routing::get;
|
||||||
|
use axum::Router;
|
||||||
|
|
||||||
|
pub fn routes_health_check() -> Router {
|
||||||
|
Router::new().route("/health_check", get(|| async {}))
|
||||||
|
}
|
||||||
5
src/routes/mod.rs
Normal file
5
src/routes/mod.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
mod health_check;
|
||||||
|
mod subscriptions;
|
||||||
|
|
||||||
|
pub use health_check::*;
|
||||||
|
pub use subscriptions::*;
|
||||||
12
src/routes/subscriptions.rs
Normal file
12
src/routes/subscriptions.rs
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
use axum::routing::get;
|
||||||
|
use axum::Router;
|
||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct FormData {
|
||||||
|
email: String,
|
||||||
|
name: String,
|
||||||
|
}
|
||||||
|
pub fn routes_subscriptions() -> Router {
|
||||||
|
Router::new().route("/subscriptions", get(|| async {}))
|
||||||
|
}
|
||||||
16
src/startup.rs
Normal file
16
src/startup.rs
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
use axum::routing::IntoMakeService;
|
||||||
|
use axum::serve::Serve;
|
||||||
|
use axum::Router;
|
||||||
|
use tokio::net::TcpListener;
|
||||||
|
|
||||||
|
/// API routing
|
||||||
|
pub fn app() -> Router {
|
||||||
|
Router::new()
|
||||||
|
.merge(crate::routes::routes_health_check())
|
||||||
|
.merge(crate::routes::routes_subscriptions())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Start the server
|
||||||
|
pub fn run(listener: TcpListener) -> Serve<IntoMakeService<Router>, Router> {
|
||||||
|
axum::serve(listener, app().into_make_service())
|
||||||
|
}
|
||||||
|
|
@ -78,7 +78,7 @@ async fn spawn_app() -> TestApp {
|
||||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||||
let addr = listener.local_addr().unwrap();
|
let addr = listener.local_addr().unwrap();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
axum::serve(listener, learn_axum::app()).await.unwrap();
|
axum::serve(listener, learn_axum::startup::app()).await.unwrap();
|
||||||
});
|
});
|
||||||
TestApp { addr }
|
TestApp { addr }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue