fix: restructure the logic

This commit is contained in:
2024-05-03 15:43:34 -04:00
parent 53f20f16f4
commit f08def8e77
8 changed files with 44 additions and 32 deletions

0
src/configuration.rs Normal file
View File

View File

@@ -1,29 +1,4 @@
use actix_web::dev::Server;
use actix_web::{web, App, HttpResponse, HttpServer};
use std::net::TcpListener;
pub mod configuration;
pub mod routes;
pub mod startup;
#[derive(serde::Deserialize)]
struct FormData {
email: String,
name: String,
}
async fn healthcheck_route() -> HttpResponse {
HttpResponse::Ok().finish()
}
async fn subscribe_route(_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(healthcheck_route))
.route("/subscribe", web::post().to(subscribe_route))
})
.listen(listener)?
.run();
Ok(server)
}

View File

@@ -1,12 +1,12 @@
use std::net::TcpListener;
use email_newsletter_api::run;
use email_newsletter_api::startup;
#[tokio::main]
async fn main() -> Result<(), std::io::Error>{
async fn main() -> Result<(), std::io::Error> {
let listener = TcpListener::bind("127.0.0.1:8000").expect("Failed to bind to port 8000");
// Move the error up the call stack
// otherwise await for the HttpServer
run(listener)?.await
startup::run(listener)?.await
}

View File

@@ -0,0 +1,5 @@
use actix_web::HttpResponse;
pub async fn healthcheck_route() -> HttpResponse {
HttpResponse::Ok().finish()
}

5
src/routes/mod.rs Normal file
View File

@@ -0,0 +1,5 @@
mod health_check;
mod subscriptions;
pub use health_check::*;
pub use subscriptions::*;

View File

@@ -0,0 +1,11 @@
use actix_web::{web, HttpResponse};
#[derive(serde::Deserialize)]
pub struct FormData {
email: String,
name: String,
}
pub async fn subscribe_route(_form: web::Form<FormData>) -> HttpResponse {
HttpResponse::Ok().finish()
}

16
src/startup.rs Normal file
View File

@@ -0,0 +1,16 @@
use crate::routes::{healthcheck_route, subscribe_route};
use actix_web::dev::Server;
use actix_web::{web, App, HttpServer};
use std::net::TcpListener;
pub fn run(listener: TcpListener) -> Result<Server, std::io::Error> {
let server = HttpServer::new(|| {
App::new()
.route("/health_check", web::get().to(healthcheck_route))
.route("/subscribe", web::post().to(subscribe_route))
})
.listen(listener)?
.run();
Ok(server)
}