fix: restructure the logic
This commit is contained in:
parent
53f20f16f4
commit
f08def8e77
0
src/configuration.rs
Normal file
0
src/configuration.rs
Normal file
31
src/lib.rs
31
src/lib.rs
@ -1,29 +1,4 @@
|
|||||||
use actix_web::dev::Server;
|
pub mod configuration;
|
||||||
use actix_web::{web, App, HttpResponse, HttpServer};
|
pub mod routes;
|
||||||
use std::net::TcpListener;
|
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)
|
|
||||||
}
|
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
use std::net::TcpListener;
|
use std::net::TcpListener;
|
||||||
|
|
||||||
use email_newsletter_api::run;
|
use email_newsletter_api::startup;
|
||||||
|
|
||||||
#[tokio::main]
|
#[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");
|
let listener = TcpListener::bind("127.0.0.1:8000").expect("Failed to bind to port 8000");
|
||||||
|
|
||||||
// Move the error up the call stack
|
// Move the error up the call stack
|
||||||
// otherwise await for the HttpServer
|
// otherwise await for the HttpServer
|
||||||
run(listener)?.await
|
startup::run(listener)?.await
|
||||||
}
|
}
|
||||||
|
5
src/routes/health_check.rs
Normal file
5
src/routes/health_check.rs
Normal 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
5
src/routes/mod.rs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
mod health_check;
|
||||||
|
mod subscriptions;
|
||||||
|
|
||||||
|
pub use health_check::*;
|
||||||
|
pub use subscriptions::*;
|
11
src/routes/subscriptions.rs
Normal file
11
src/routes/subscriptions.rs
Normal 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
16
src/startup.rs
Normal 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)
|
||||||
|
}
|
@ -11,7 +11,7 @@ pub fn spawn_app() -> String {
|
|||||||
|
|
||||||
let port = listener.local_addr().unwrap().port();
|
let port = listener.local_addr().unwrap().port();
|
||||||
|
|
||||||
let server = email_newsletter_api::run(listener).expect("Failed to bind address");
|
let server = email_newsletter_api::startup::run(listener).expect("Failed to bind address");
|
||||||
|
|
||||||
/* `tokio::spawn(/*async task*/)` will spawn an async task to be run.
|
/* `tokio::spawn(/*async task*/)` will spawn an async task to be run.
|
||||||
We can continue executing other code concurrently while `task` runs in the background.
|
We can continue executing other code concurrently while `task` runs in the background.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user