Skip to content

Commit

Permalink
Refactor lib into separate files.
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkbyers committed Mar 18, 2024
1 parent 7c6355e commit 88c53cb
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 48 deletions.
12 changes: 5 additions & 7 deletions src/db.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use libsql::{params, Builder, Connection, Database, Error};
use libsql::{Builder, Connection, Database, Error};

pub async fn new_local_db(path: &str) -> Result<Database, Error> {
let db = Builder::new_local(path).build().await?;
let conn = db.connect()?;
init_schema(&conn).await?;
init_schema(&conn).await;

Ok(db)
}

async fn init_schema(conn: &Connection) -> Result<(), Error> {
async fn init_schema(conn: &Connection) {
conn.execute(
r#"
CREATE TABLE IF NOT EXISTS subscriptions (
Expand All @@ -18,8 +18,6 @@ async fn init_schema(conn: &Connection) -> Result<(), Error> {
subscribed_at timestampz NOT NULL
);
"#,
params!([])
)
.await?;
Ok(())
()
).await.unwrap();
}
32 changes: 2 additions & 30 deletions src/lib.rs
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;
7 changes: 2 additions & 5 deletions src/main.rs
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
}
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 subscriptions;
mod health_check;

pub use subscriptions::*;
pub use health_check::*;
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 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()
}
19 changes: 19 additions & 0 deletions src/startup.rs
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)
}
4 changes: 2 additions & 2 deletions tests/common/mod.rs
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)
}
2 changes: 1 addition & 1 deletion tests/health_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use common::spawn_app;
#[tokio::test]
async fn health_check_works() {
// Arrange
let address = spawn_app();
let address = spawn_app().await;

let client = reqwest::Client::new();

Expand Down
6 changes: 3 additions & 3 deletions tests/subscribe.rs → tests/subscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use common::spawn_app;
#[tokio::test]
async fn subscribe_returns_a_200_for_valid_form_data() {
// Arrange
let address = spawn_app();
let address = spawn_app().await;
let client = reqwest::Client::new();

let body = r#"
Expand All @@ -28,7 +28,7 @@ async fn subscribe_returns_a_200_for_valid_form_data() {
#[tokio::test]
async fn subscribe_returns_a_400_when_data_is_missing() {
// Arrange
let address = spawn_app();
let address = spawn_app().await;
let client = reqwest::Client::new();

let test_cases = vec![
Expand All @@ -49,7 +49,7 @@ async fn subscribe_returns_a_400_when_data_is_missing() {
),
];

for (invalid_body, error_message) in test_cases {
for (invalid_body, _error_message) in test_cases {
let response = client
.post(&format!("{}/subscriptions", &address))
.header("Content-Type", "application/json")
Expand Down

0 comments on commit 88c53cb

Please sign in to comment.