Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Warp Adapter #83

Merged
merged 10 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ jobs:
DATABASE_URL_ASYNC_GRAPHQL: postgresql://nakago:nakago@localhost:1701/async-graphql
CONFIG_PATH_ASYNC_GRAPHQL: examples/async-graphql/config/ci.toml
CONFIG_PATH_SIMPLE: examples/simple/config/ci.toml
CONFIG_PATH_SIMPLE_WARP: examples/simple-warp/config/ci.toml

steps:
- name: Checkout repository
Expand Down
18 changes: 14 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Updated

- Updated `mockall` and `tokio-tungstenite` requirements, and removed temporary tokio-tungstenite fork.
## [0.19.0]

### Added

- `nakago-warp`: A new Warp adapter that works in a similar way to the Axum adapter.
- `nakago-examples-simple-warp`: A new example project that uses the Warp adapter.
- `nakago`: Added a copy of Axum's `FromRef` utility, so that it can be used without importing Axum itself.
- `nakago-derive`: Updated to support the FromRef utility.

### Changed

- Updated `mockall` and `tokio-tungstenite` requirements, and removed temporary tokio-tungstenite fork.
- `nakago-axum`: Simplified the route Init Hook.
- `nakago-axum`, `nakago-async-graphql`, `nakago-sea-orm`: Updated to use the new FromRef utility.

## [0.18.0]

### Changed
Expand Down Expand Up @@ -315,7 +324,8 @@ Expect major changes to the Application and Lifecycle systems going forward, bui
- Injection Providers
- Documentation

[unreleased]: https://github.com/bkonkle/nakago/compare/0.18.0...HEAD
[unreleased]: https://github.com/bkonkle/nakago/compare/0.19.0...HEAD
[0.19.0]: https://github.com/bkonkle/nakago/compare/0.18.0...0.19.0
[0.18.0]: https://github.com/bkonkle/nakago/compare/0.17.0...0.18.0
[0.17.0]: https://github.com/bkonkle/nakago/compare/0.16.0...0.17.0
[0.16.0]: https://github.com/bkonkle/nakago/compare/0.15.0...0.16.0
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"nakago",
"nakago/derive",
"nakago_axum",
"nakago_warp",
"nakago_sea_orm",
"nakago_async_graphql",
"examples/*",
Expand All @@ -12,13 +13,15 @@ default-members = [
"nakago",
"nakago/derive",
"nakago_axum",
"nakago_warp",
"nakago_sea_orm",
"nakago_async_graphql",
]

[patch.crates-io]
nakago = { path = "./nakago" }
nakago-axum = { path = "./nakago_axum" }
nakago-warp = { path = "./nakago_warp" }
nakago-derive = { path = "./nakago/derive" }
nakago-sea-orm = { path = "./nakago_sea_orm" }
nakago-async-graphql = { path = "./nakago_async_graphql" }
Expand Down
19 changes: 19 additions & 0 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ command = "cargo"
args = ["run", "-p", "nakago-examples-simple"]
watch = true

[tasks.run-simple-warp]
env = { "RUST_LOG" = "info,sqlx::query=warn" }
command = "cargo"
args = ["run", "-p", "nakago-examples-simple-warp"]
watch = true

[tasks.run-async-graphql]
env = { "RUST_LOG" = "info,sqlx::query=warn" }
command = "cargo"
Expand Down Expand Up @@ -169,6 +175,19 @@ args = [
"${@}",
]

[tasks.docker-simple-warp]
cwd = "./examples/simple-warp"
command = "docker-compose"
args = [
"-f",
"../../docker-compose.yml",
"-f",
"docker-compose.app.yml",
"--env-file",
"../../.env",
"${@}",
]

[tasks.docker-async-graphql]
cwd = "./examples/async-graphql"
command = "docker-compose"
Expand Down
10 changes: 5 additions & 5 deletions examples/async-graphql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ figment = { version = "0.10", features = ["env", "toml", "yaml", "json"] }
futures = "0.3"
hyper = "1.0"
log = "0.4"
nakago = "0.18"
nakago-axum = "0.18"
nakago-derive = "0.18"
nakago-sea-orm = "0.18"
nakago-async-graphql = "0.18"
nakago = "0.19"
nakago-axum = "0.19"
nakago-derive = "0.19"
nakago-sea-orm = "0.19"
nakago-async-graphql = "0.19"
oso = "0.27"
pico-args = "0.5.0"
pretty_env_logger = "0.5"
Expand Down
2 changes: 1 addition & 1 deletion examples/async-graphql/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use axum::extract::FromRef;
use nakago::Tag;
use nakago_axum::{self, auth};
use nakago_derive::FromRef;
use nakago_sea_orm::{self, config::DatabasePool};
use serde::Serialize;
use serde_derive::Deserialize;
Expand Down
31 changes: 12 additions & 19 deletions examples/async-graphql/src/http/init.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use async_trait::async_trait;
use hyper::Method;
use axum::{
routing::{get, post},
Router,
};
use nakago::{hooks, Hook, Inject};
use nakago_axum::routes;

Expand Down Expand Up @@ -33,35 +36,25 @@ impl Hook for Init {
let events_controller = i.get(&events::CONTROLLER).await?;

i.handle(routes::Init::new(
Method::GET,
"/health",
health::health_check,
Router::new().route("/health", get(health::health_check)),
))
.await?;

i.handle(routes::Init::new(
Method::GET,
"/graphql",
graphql::Controller::graphiql,
Router::new().route("/graphql", get(graphql::Controller::graphiql)),
))
.await?;

i.handle(routes::Init::new(
Method::POST,
i.handle(routes::Init::new(Router::new().route(
"/graphql",
move |sub, req| async move {
graphql::Controller::resolve(graphql_controller, sub, req).await
},
))
post(move |sub, req| async move { graphql_controller.resolve(sub, req).await }),
)))
.await?;

i.handle(routes::Init::new(
Method::GET,
i.handle(routes::Init::new(Router::new().route(
"/events",
move |sub, ws| async move {
events::Controller::upgrade(events_controller, sub, ws).await
},
))
get(move |sub, ws| async move { events_controller.upgrade(sub, ws).await }),
)))
.await?;

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion examples/cqrs-es/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pretty_env_logger = "0.5"
serde = { version = "1.0", features = ["derive"] }
serde_derive = "1.0"
serde_json = "1.0"
nakago = "0.18"
nakago = "0.19"

[dev-dependencies]
criterion = "0.5"
Expand Down
63 changes: 63 additions & 0 deletions examples/simple-warp/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
[package]
name = "nakago-examples-simple-warp"
version = "0.19.0"
authors = ["Brandon Konkle <brandon@konkle.us>"]
edition = "2021"
description = "A lightweight Rust framework for sharp services 😎"
repository = "https://github.com/bkonkle/nakago"
license = "MIT"

publish = false

[features]
integration = []

[dependencies]
anyhow = "1.0"
async-trait = "0.1"
chrono = { version = "0.4.19", features = ["serde"] }
fake = { version = "2.9", features = [
'derive',
'chrono',
'http',
'uuid',
], git = "https://github.com/cksac/fake-rs.git" }
figment = { version = "0.10", features = ["env", "toml", "yaml", "json"] }
futures = "0.3"
hyper = "1.0"
log = "0.4"
nakago = "0.19"
nakago-warp = "0.19"
nakago-derive = "0.19"
pico-args = "0.5.0"
pretty_env_logger = "0.5"
rand = "0.8"
reqwest = { version = "0.11", features = ["json"] }
sea-orm = { version = "0.12", features = [
"macros",
"mock",
"runtime-tokio-rustls",
"sqlx-postgres",
"with-chrono",
"with-json",
], default-features = false }
serde = { version = "1.0", features = ["derive"] }
serde_derive = "1.0"
serde_json = "1.0"
tokio = { version = "1", features = ["full"] }
tokio-stream = "0.1"
ulid = "1.0"
warp = "0.3"

[dev-dependencies]
biscuit = "0.7.0"
criterion = "0.5"
futures-util = { version = "0.3", default-features = false, features = [
"sink",
"std",
] }
hyper-tls = "0.6"
maplit = { version = "1" }
mockall = "0.12"
pretty_assertions = "1.2"
url = "2.0.0"
27 changes: 27 additions & 0 deletions examples/simple-warp/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM debian:bullseye-slim

ARG RUN_MODE=production
ENV TZ=Etc/UTC \
RUN_MODE=${RUN_MODE} \
RUST_LOG=info

EXPOSE 3000

# Install some Debian packages
RUN apt update && \
apt-get -y install --no-install-recommends ca-certificates tzdata && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* && \
# Set up the app user
groupadd simple-warp && \
useradd -g simple-warp simple-warp && \
mkdir -p /usr/src/app && \
chown simple-warp:simple-warp /usr/src/app

COPY --chown=simple-warp:simple-warp ../../target/release/nakago-examples-simple-warp /usr/src/app/simple-warp
COPY --chown=simple-warp:simple-warp ../../examples/simple-warp/config/*.toml /usr/src/app/config/

USER simple-warp
WORKDIR /usr/src/app

CMD ["./simple-warp"]
2 changes: 2 additions & 0 deletions examples/simple-warp/config/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
local.toml
test.toml
3 changes: 3 additions & 0 deletions examples/simple-warp/config/ci.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# CI testing

port = 0
7 changes: 7 additions & 0 deletions examples/simple-warp/config/local.toml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Local development

port = 8000

[auth.client]
id = "client_id"
secret = "client_secret"
7 changes: 7 additions & 0 deletions examples/simple-warp/config/test.toml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Local testing

port = 0

[auth.client]
id = "client_id"
secret = "client_secret"
10 changes: 10 additions & 0 deletions examples/simple-warp/docker-compose.app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: "3.1"

services:
api:
image: simple_warp
container_name: simple-warp
depends_on:
- postgres
ports:
- 8000:8000
20 changes: 20 additions & 0 deletions examples/simple-warp/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use nakago::{self, Tag};
use nakago_derive::FromRef;
use nakago_warp::{self, auth};
use serde::Serialize;
use serde_derive::Deserialize;

/// Tag(app::Config)
pub const CONFIG: Tag<Config> = Tag::new("app::Config");

/// Server Config
#[derive(Default, Debug, Serialize, Deserialize, Clone, FromRef)]
pub struct Config {
/// HTTP config
pub http: nakago_warp::Config,

/// HTTP Auth Config
pub auth: auth::Config,
}

impl nakago::Config for Config {}
41 changes: 41 additions & 0 deletions examples/simple-warp/src/http/health.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::convert::Infallible;

use nakago::Inject;
use nakago_warp::Route;
use serde::{Deserialize, Serialize};
use warp::{filters::BoxedFilter, reply::Reply, Filter};

/// A Health Check Response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthResponse {
/// The Status code
code: usize,

/// Whether the check was successful or not
success: bool,
}

impl Reply for HealthResponse {
fn into_response(self) -> warp::reply::Response {
warp::reply::with_status(warp::reply::json(&self), warp::http::StatusCode::OK)
.into_response()
}
}

/// Create a Health Check Route
pub fn health_check(filter: BoxedFilter<(Inject,)>) -> Route {
warp::path("health")
.and(warp::get())
.and(filter)
.and_then(health_handler)
.map(|a| Box::new(a) as Box<dyn Reply>)
.boxed()
}

/// Handle Health Check requests
pub async fn health_handler(_: Inject) -> Result<HealthResponse, Infallible> {
Ok(HealthResponse {
code: 200,
success: true,
})
}
20 changes: 20 additions & 0 deletions examples/simple-warp/src/http/init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use async_trait::async_trait;
use nakago::{hooks, Hook, Inject};
use nakago_warp::routes;

use super::{health, user};

/// Init all handlers
#[derive(Default)]
pub struct Init {}

#[async_trait]
impl Hook for Init {
async fn handle(&self, i: Inject) -> hooks::Result<()> {
i.handle(routes::Init::new(health::health_check)).await?;

i.handle(routes::Init::new(user::get_username)).await?;

Ok(())
}
}
Loading