-
-
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.
Add example worker and api to the
minimal
example
- Loading branch information
1 parent
ab8a4f5
commit 92f0101
Showing
8 changed files
with
99 additions
and
1 deletion.
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
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,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(()) | ||
} | ||
} |
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,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 {})) | ||
} |
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,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)) | ||
} |
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,3 +1,5 @@ | ||
pub mod app; | ||
pub mod app_state; | ||
pub mod cli; | ||
pub mod controller; | ||
pub mod worker; |
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,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 {} | ||
} | ||
} |
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 @@ | ||
pub mod example; |