Skip to content

Commit

Permalink
refactor: add standalone server as a binary target (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
kentSarmiento authored Dec 27, 2023
1 parent 0a167e9 commit 155e07f
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 118 deletions.
1 change: 0 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"rust-analyzer.linkedProjects": [
"./link-for-later/Cargo.toml",
"./link-for-later-axum/Cargo.toml",
"./link-for-later-lambda/Cargo.toml",
"./link-for-later-shuttle/Cargo.toml",
]
Expand Down
59 changes: 12 additions & 47 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ Link for Later Service provides an API to save links in your personal library fo

- [`Axum`](https://github.com/tokio-rs/axum) as web application framework
- Multiple deployment options:
- [Shuttle](https://github.com/shuttle-hq/shuttle). Refer [here](./link-for-later-shuttle/) for details.
- [Shuttle](https://github.com/shuttle-hq/shuttle) application. Refer [here](./link-for-later-shuttle/) for details.
- [Cargo Lambda](https://www.cargo-lambda.info/) to deploy the service as an AWS Lambda Function. Refer [here](./link-for-later-lambda/) for details.
- Standalone server using axum for local development. Refer [here](./link-for-later-axum/) for details.
- Standalone server using axum for local development. Refer [here](./link-for-later/src/bin/) for details.
- Multiple repository options:
- MongoDB
- InMemory database
Expand Down
19 changes: 0 additions & 19 deletions link-for-later-axum/Cargo.toml

This file was deleted.

1 change: 1 addition & 0 deletions link-for-later-shuttle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ shuttle-runtime = "0.35.0"
shuttle-shared-db = { version = "0.35.0", features = ["mongodb"] }
tokio = { version = "1", features = ["macros"] }
tracing = { version = "0.1", features = ["log"] }
tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt"] }
6 changes: 6 additions & 0 deletions link-for-later-shuttle/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ use mongodb::Database;

#[shuttle_runtime::main]
async fn main(#[shuttle_shared_db::MongoDb] db: Database) -> shuttle_axum::ShuttleAxum {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::DEBUG)
.with_target(false)
.without_time()
.init();

let app = link_for_later::app::new(link_for_later::DatabaseType::MongoDb(db));
Ok(app.into())
}
6 changes: 5 additions & 1 deletion link-for-later/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ description = "Link for Later Service"
repository = "https://github.com/kentSarmiento/link-for-later-service"
publish = false

[[bin]]
name = "link-for-later"
path = "src/bin/main.rs"

[dependencies]
argon2 = "0.5.2"
axum = "0.7.2"
Expand All @@ -21,11 +25,11 @@ serde_json = "1.0.108"
tokio = { version = "1", features = ["macros"] }
tower = "0.4.13"
tracing = { version = "0.1", features = ["log"] }
tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt"] }
validator = { version = "0.16.1", features = ["derive"] }

[dev-dependencies]
mockall = "0.12.0"
rand = "0.8.5"
rstest = "0.18.2"
tracing-test = "0.2.4"

Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ For local development, a standalone server using axum can be used with either an
* To use In-memory database, set `INMEMORY_DB` before running the server

```sh
INMEMORY_DB=true cargo run --bin link-for-later-axum
INMEMORY_DB=true cargo run --bin link-for-later
```

* To use MongoDb, set the MongoDB server and database name before running the server

```sh
MONGODB_URI="mongodb://localhost:23288" MONGODB_DATABASE_NAME="test" cargo run --bin link-for-later-axum
MONGODB_URI="mongodb://localhost:27017" MONGODB_DATABASE_NAME="test" cargo run --bin link-for-later
```

You will be able to send requests to the server using port 8080.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ const MONGODB_DATABASE_NAME_KEY: &str = "MONGODB_DATABASE_NAME";
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::DEBUG)
// disable printing the name of the module in every log line.
.with_target(false)
// disabling time is handy because CloudWatch will add the ingestion time.
.without_time()
.init();

let app = if std::env::var(INMEMORY_DB_KEY).is_ok() {
Expand Down
14 changes: 0 additions & 14 deletions link-for-later/src/controller/routes/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ mod tests {
use axum::{extract::State, http::StatusCode};
use http_body_util::BodyExt;
use serde_json::json;
use tracing_test::traced_test;

use crate::{
entity::LinkItem,
Expand All @@ -147,7 +146,6 @@ mod tests {

use super::*;

#[traced_test]
#[tokio::test]
async fn test_get_links_empty() {
let repo_query = LinkQueryBuilder::default().owner("user-id").build();
Expand All @@ -169,7 +167,6 @@ mod tests {
assert_eq!(&body[..], b"[]");
}

#[traced_test]
#[tokio::test]
async fn test_get_links_non_empty() {
let repo_query = LinkQueryBuilder::default().owner("user-id").build();
Expand Down Expand Up @@ -199,7 +196,6 @@ mod tests {
assert!(body[0].url() == "http://link");
}

#[traced_test]
#[tokio::test]
async fn test_get_links_service_error() {
let repo_query = LinkQueryBuilder::default().owner("user-id").build();
Expand All @@ -222,7 +218,6 @@ mod tests {
assert_eq!(body, json!({"error": "test error"}).to_string());
}

#[traced_test]
#[tokio::test]
async fn test_post_link() {
let request = LinkItemRequest::new("http://link");
Expand Down Expand Up @@ -258,7 +253,6 @@ mod tests {
assert!(body.url() == "http://link");
}

#[traced_test]
#[tokio::test]
async fn test_post_link_invalid_url() {
let request = LinkItemRequest::new("invalid-link");
Expand All @@ -282,7 +276,6 @@ mod tests {
assert_eq!(body, json!({"error": "invalid request"}).to_string());
}

#[traced_test]
#[tokio::test]
async fn test_post_link_service_error() {
let request = LinkItemRequest::new("http://link");
Expand Down Expand Up @@ -311,7 +304,6 @@ mod tests {
assert_eq!(body, json!({"error": "test error"}).to_string());
}

#[traced_test]
#[tokio::test]
async fn test_get_link() {
let repo_query = LinkQueryBuilder::new("1", "user-id").build();
Expand Down Expand Up @@ -346,7 +338,6 @@ mod tests {
assert!(body.url() == "http://link");
}

#[traced_test]
#[tokio::test]
async fn test_get_link_service_error() {
let repo_query = LinkQueryBuilder::new("1", "user-id").build();
Expand Down Expand Up @@ -374,7 +365,6 @@ mod tests {
assert_eq!(body, json!({"error": "test error"}).to_string());
}

#[traced_test]
#[tokio::test]
async fn test_put_link() {
let request = LinkItemRequest::new("http://link");
Expand Down Expand Up @@ -414,7 +404,6 @@ mod tests {
assert!(body.url() == "http://link");
}

#[traced_test]
#[tokio::test]
async fn test_put_link_invalid_url() {
let request = LinkItemRequest::new("invalid-link");
Expand All @@ -439,7 +428,6 @@ mod tests {
assert_eq!(body, json!({"error": "invalid request"}).to_string());
}

#[traced_test]
#[tokio::test]
async fn test_put_link_service_error() {
let request = LinkItemRequest::new("http://link");
Expand Down Expand Up @@ -472,7 +460,6 @@ mod tests {
assert_eq!(body, json!({"error": "test error"}).to_string());
}

#[traced_test]
#[tokio::test]
async fn test_delete_link() {
let item_to_delete = LinkItemBuilder::default().id("1").owner("user-id").build();
Expand All @@ -496,7 +483,6 @@ mod tests {
assert_eq!(StatusCode::NO_CONTENT, parts.status);
}

#[traced_test]
#[tokio::test]
async fn test_delete_link_service_error() {
let item_to_delete = LinkItemBuilder::default().id("1").owner("user-id").build();
Expand Down
7 changes: 0 additions & 7 deletions link-for-later/src/controller/routes/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ mod tests {

use http_body_util::BodyExt;
use serde_json::json;
use tracing_test::traced_test;

use crate::{
dto::Token,
Expand All @@ -89,7 +88,6 @@ mod tests {

use super::*;

#[traced_test]
#[tokio::test]
async fn test_register_user() {
let request = UserInfoRequest::new("[email protected]", "test");
Expand All @@ -113,7 +111,6 @@ mod tests {
assert_eq!(&body[..], b"");
}

#[traced_test]
#[tokio::test]
async fn test_register_user_invalid_email() {
let request = UserInfoRequest::new("user", "test");
Expand All @@ -132,7 +129,6 @@ mod tests {
assert_eq!(body, json!({"error": "invalid request"}).to_string());
}

#[traced_test]
#[tokio::test]
async fn test_register_user_service_error() {
let request = UserInfoRequest::new("[email protected]", "test");
Expand All @@ -156,7 +152,6 @@ mod tests {
assert_eq!(body, json!({"error": "test error"}).to_string());
}

#[traced_test]
#[tokio::test]
async fn test_login_user() {
let request = UserInfoRequest::new("[email protected]", "test");
Expand All @@ -181,7 +176,6 @@ mod tests {
assert_eq!(body, json!({"token": "test"}).to_string());
}

#[traced_test]
#[tokio::test]
async fn test_login_user_invalid_email() {
let request = UserInfoRequest::new("user", "test");
Expand All @@ -200,7 +194,6 @@ mod tests {
assert_eq!(body, json!({"error": "invalid request"}).to_string());
}

#[traced_test]
#[tokio::test]
async fn test_login_user_service_error() {
let request = UserInfoRequest::new("[email protected]", "test");
Expand Down
Loading

0 comments on commit 155e07f

Please sign in to comment.