-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
kirkbyers
committed
Mar 18, 2024
1 parent
7c6355e
commit 88c53cb
Showing
10 changed files
with
57 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,3 @@ | ||
pub mod db; | ||
|
||
use std::net::TcpListener; | ||
|
||
use actix_web::{dev::Server, web, App, HttpResponse, HttpServer}; | ||
use serde::Deserialize; | ||
|
||
async fn health_check() -> HttpResponse { | ||
HttpResponse::Ok().finish() | ||
} | ||
|
||
#[derive(Deserialize)] | ||
struct SubscriberData { | ||
_email: String, | ||
_name: String, | ||
} | ||
|
||
async fn subscribe(_json: web::Json<SubscriberData>) -> 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 routes; | ||
pub mod startup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,8 @@ | ||
use zero2prod::{db, run}; | ||
use zero2prod::startup::run; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), std::io::Error> { | ||
db::new_local_db("./.data/mydb") | ||
.await | ||
.expect("Failed to create database"); | ||
let listener = | ||
std::net::TcpListener::bind("127.0.0.1:8000").expect("Failed to bind to port 8000"); | ||
run(listener)?.await | ||
run(listener).await?.await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mod subscriptions; | ||
mod health_check; | ||
|
||
pub use subscriptions::*; | ||
pub use health_check::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use serde::Deserialize; | ||
use actix_web::{web, HttpResponse}; | ||
|
||
#[derive(Deserialize)] | ||
#[allow(dead_code)] | ||
pub struct SubscriberData { | ||
email: String, | ||
name: String, | ||
} | ||
|
||
pub async fn subscribe(_json: web::Json<SubscriberData>) -> HttpResponse { | ||
HttpResponse::Ok().finish() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use crate::{db, routes::{health_check, subscribe}}; | ||
|
||
use std::net::TcpListener; | ||
|
||
use actix_web::{dev::Server, web, App, HttpServer}; | ||
|
||
pub async fn run(listener: TcpListener) -> Result<Server, std::io::Error> { | ||
db::new_local_db("./.data/mydb") | ||
.await | ||
.expect("Failed to create database"); | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
pub fn spawn_app() -> String { | ||
pub async fn spawn_app() -> String { | ||
let listener = std::net::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).await.expect("Failed to bind address"); | ||
tokio::spawn(server); | ||
format!("http://127.0.0.1:{}", port) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters