Skip to content

Commit

Permalink
Add example worker and api to the minimal example
Browse files Browse the repository at this point in the history
  • Loading branch information
spencewenski committed Apr 26, 2024
1 parent ab8a4f5 commit 92f0101
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 1 deletion.
5 changes: 5 additions & 0 deletions examples/minimal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ tracing = { version = "0.1.40", features = ["async-await"] }
async-trait = "0.1.77"
aide = { version = "0.13.3", features = ["axum"] }
axum = "0.7.5"
schemars = "0.8.16"

# DB
entity = { path = "entity" }
migration = { path = "migration" }
clap = { version = "4.5.4", features = ["derive"] }

# The default `rss-stats` feature has a dependency that currently can't be satisfied (memchr: ~2.3)
rusty-sidekiq = { version = "0.10.4", default-features = false }
serde = { version = "1.0.198", features = ["derive"] }

[dev-dependencies]
cargo-husky = { version = "1.5.0", features = ["default", "run-cargo-check", "run-cargo-clippy", "run-cargo-fmt", "run-cargo-test"] }

Expand Down
3 changes: 3 additions & 0 deletions examples/minimal/config/default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ acquire-timeout = 5000
idle-timeout = 60
min-connections = 0
max-connections = 10

[worker.sidekiq]
queues = ["default"]
19 changes: 18 additions & 1 deletion examples/minimal/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
use aide::axum::ApiRouter;
use async_trait::async_trait;
use migration::Migrator;
use roadster::app::App as RoadsterApp;
use roadster::app_context::AppContext;
use roadster::config::app_config::AppConfig;
use roadster::controller::default_routes;
use roadster::worker::app_worker::AppWorker;
use roadster::worker::registry::WorkerRegistry;

use crate::app_state::AppState;
use crate::cli::AppCli;
use crate::controller;
use crate::worker::example::ExampleWorker;

const BASE: &str = "/api";

#[derive(Default)]
pub struct App;

#[async_trait]
impl RoadsterApp for App {
type State = AppState;
type Cli = AppCli;
type M = Migrator;

fn router(config: &AppConfig) -> ApiRouter<Self::State> {
default_routes(BASE, config)
default_routes(BASE, config).merge(controller::routes(BASE))
}

async fn workers(
registry: &mut WorkerRegistry<Self>,
_context: &AppContext,
state: &Self::State,
) -> anyhow::Result<()> {
registry.register_app_worker(ExampleWorker::build(state));
Ok(())
}
}
38 changes: 38 additions & 0 deletions examples/minimal/src/controller/example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use crate::app_state::AppState;
use crate::worker::example::ExampleWorker;
use aide::axum::routing::get_with;
use aide::axum::ApiRouter;
use aide::transform::TransformOperation;
use axum::extract::State;
use axum::Json;
use roadster::controller::build_path;
use roadster::view::app_error::AppError;
use roadster::worker::app_worker::AppWorker;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use tracing::instrument;

const BASE: &str = "/example";
const TAG: &str = "Example";

pub fn routes(parent: &str) -> ApiRouter<AppState> {
let root = build_path(parent, BASE);

ApiRouter::new().api_route(&root, get_with(example_get, example_get_docs))
}

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ExampleResponse {}

#[instrument(skip_all)]
async fn example_get(State(state): State<AppState>) -> Result<Json<ExampleResponse>, AppError> {
ExampleWorker::enqueue(&state, "Example".to_string()).await?;
Ok(Json(ExampleResponse {}))
}

fn example_get_docs(op: TransformOperation) -> TransformOperation {
op.description("Example API.")
.tag(TAG)
.response_with::<200, Json<ExampleResponse>, _>(|res| res.example(ExampleResponse {}))
}
8 changes: 8 additions & 0 deletions examples/minimal/src/controller/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::app_state::AppState;
use aide::axum::ApiRouter;

pub mod example;

pub fn routes(parent: &str) -> ApiRouter<AppState> {
ApiRouter::new().merge(example::routes(parent))
}
2 changes: 2 additions & 0 deletions examples/minimal/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod app;
pub mod app_state;
pub mod cli;
pub mod controller;
pub mod worker;
24 changes: 24 additions & 0 deletions examples/minimal/src/worker/example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::app::App;
use crate::app_state::AppState;
use async_trait::async_trait;
use roadster::worker::app_worker::AppWorker;
use sidekiq::Worker;
use tracing::{info, instrument};

pub struct ExampleWorker {}

#[async_trait]
impl Worker<String> for ExampleWorker {
#[instrument(skip_all)]
async fn perform(&self, args: String) -> sidekiq::Result<()> {
info!("Processing job with args: {args}");
Ok(())
}
}

#[async_trait]
impl AppWorker<App, String> for ExampleWorker {
fn build(_state: &AppState) -> Self {
Self {}
}
}
1 change: 1 addition & 0 deletions examples/minimal/src/worker/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod example;

0 comments on commit 92f0101

Please sign in to comment.