Skip to content

Commit

Permalink
Project structure major refactor - Splitting things in modules
Browse files Browse the repository at this point in the history
  • Loading branch information
ICavlek committed Sep 28, 2023
1 parent 11fb3ad commit 73cbd8a
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 32 deletions.
12 changes: 12 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,17 @@ actix-web = "4"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
serde = { version = "1", features = ["derive"]}

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

[dev-dependencies]
reqwest = "0.11"
1 change: 1 addition & 0 deletions src/configuration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

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

async fn health_check() -> HttpResponse {
HttpResponse::Ok().finish()
}

#[derive(serde::Deserialize)]
struct FormData {
#[allow(dead_code)]
email: String,
#[allow(dead_code)]
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 configuration;
pub mod routes;
pub mod startup;
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::net::TcpListener;
use zero2prod::run;
use zero2prod::startup::run;

#[tokio::main]
async fn main() -> std::io::Result<()> {
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;

pub async fn health_check() -> HttpResponse {
HttpResponse::Ok().finish()
}
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::*;
13 changes: 13 additions & 0 deletions src/routes/subscriptions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use actix_web::{web, HttpResponse};

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

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

use crate::routes::{health_check, subscribe};
use actix_web::{dev::Server, web, App, HttpServer};

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)
}
3 changes: 2 additions & 1 deletion tests/health_check.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::net::TcpListener;
use zero2prod::startup::run;

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 = run(listener).expect("Failed to bind address");
let _ = tokio::spawn(server);
format!("http://127.0.0.1:{}", port)
}
Expand Down

0 comments on commit 73cbd8a

Please sign in to comment.