Skip to content

Commit

Permalink
refactor!: split in modules instead of a single lib file
Browse files Browse the repository at this point in the history
  • Loading branch information
Yag000 committed Aug 27, 2023
1 parent d575d00 commit eb93a7d
Show file tree
Hide file tree
Showing 10 changed files with 662 additions and 43 deletions.
612 changes: 605 additions & 7 deletions Cargo.lock

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ reqwest = "0.11.20"
serde = { version = "1.0.188", features = ["derive"] }
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }

[dependencies.sqlx]
version = "0.5.7"
default-features = false
features = [
"runtime-actix-rustls",
"macros",
"postgres",
"uuid",
"chrono",
"migrate"
]

[[bin]]
path = "src/main.rs"
name = "zero2prod"
2 changes: 2 additions & 0 deletions src/configurations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@


36 changes: 3 additions & 33 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,3 @@
use actix_web::{dev::Server, web, App, HttpRequest, HttpResponse, HttpServer, Responder};
use std::net::TcpListener;

async fn greet(req: HttpRequest) -> impl Responder {
let name = req.match_info().get("name").unwrap_or("World");
format!("Hello {name}!")
}

async fn health_check() -> impl Responder {
HttpResponse::Ok()
}

#[derive(serde::Deserialize)]
struct FormData {
email: String,
name: String,
}

async fn subscribe(_form: web::Form<FormData>) -> HttpResponse {
HttpResponse::Ok().finish()
}

pub fn run(listener: TcpListener) -> Result<Server, std::io::Error> {
let server = HttpServer::new(|| {
App::new()
.route("/health_check", web::get().to(health_check))
.route("/subscriptions", web::post().to(subscribe))
})
.listen(listener)?
.run();

Ok(server)
}
pub mod configurations;
pub mod routes;
pub mod startup;
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::net::TcpListener;

use zero2prod::run;
use zero2prod::startup::run;

#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
Expand Down
5 changes: 5 additions & 0 deletions src/routes/health_check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use actix_web::{HttpResponse, Responder};

pub async fn health_check() -> impl Responder {
HttpResponse::Ok()
}
5 changes: 5 additions & 0 deletions src/routes/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod health_check;
mod subscriptions;

pub use health_check::*;
pub use subscriptions::*;
11 changes: 11 additions & 0 deletions src/routes/subscriptions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use actix_web::{web, HttpResponse};

#[derive(serde::Deserialize)]
pub struct FormData {
email: String,
name: String,
}

pub async fn subscribe(_form: web::Form<FormData>) -> HttpResponse {
HttpResponse::Ok().finish()
}
17 changes: 17 additions & 0 deletions src/startup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use std::net::TcpListener;

use actix_web::{dev::Server, web, App, HttpServer};

use crate::routes::{health_check, subscribe};

pub fn run(listener: TcpListener) -> Result<Server, std::io::Error> {
let server = HttpServer::new(|| {
App::new()
.route("/health_check", web::get().to(health_check))
.route("/subscriptions", web::post().to(subscribe))
})
.listen(listener)?
.run();

Ok(server)
}
2 changes: 1 addition & 1 deletion tests/health_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::net::TcpListener;
fn spawn_app() -> String {
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
let port = listener.local_addr().unwrap().port();
let server = zero2prod::run(listener).expect("Failed to bind address");
let server = zero2prod::startup::run(listener).expect("Failed to bind address");
tokio::spawn(server);
format!("http://127.0.0.1:{port}")
}
Expand Down

0 comments on commit eb93a7d

Please sign in to comment.