Skip to content

Commit

Permalink
feat: Add registration/voter endpoint to poem service | NPG-8137 (#559
Browse files Browse the repository at this point in the history
)

# Description

- Added `registration/voter` endpoint implementation for poem service
- Made small refactoring

## Type of change

Please delete options that are not relevant.

- [ ] Bug fix (non-breaking change which fixes an issue)
- [X] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] This change requires a documentation update

## How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce.
Please also list any relevant details for your test configuration

- [x] voter_test

---------

Co-authored-by: Steven Johnson <[email protected]>
  • Loading branch information
Mr-Leshiy and stevenj authored Sep 19, 2023
1 parent b6ea6c9 commit 77185a1
Show file tree
Hide file tree
Showing 58 changed files with 698 additions and 130 deletions.
135 changes: 94 additions & 41 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions src/cat-data-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ chrono = { workspace = true }
jormungandr-lib = { workspace = true, optional = true }
chain-impl-mockchain = { workspace = true, optional = true }

poem = { version = "1.3.58", features = ["embed", "prometheus", "compression"] }
poem = { version = "1.3.58", features = ["embed", "prometheus", "compression", "test"] }
poem-openapi = { version = "3.0.4", features = [
"openapi-explorer",
"rapidoc",
"redoc",
"swagger-ui",
"uuid",
"url"
"url",
"chrono",
] }
poem-extensions = { version = "0.7.2" }

Expand Down
1 change: 1 addition & 0 deletions src/cat-data-service/src/legacy_service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use tokio::try_join;
use tower_http::cors::{Any, CorsLayer};

mod health;
pub(crate) mod types;
mod v0;
mod v1;

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ pub fn serialize_datetime_as_rfc3339<S: Serializer>(
serializer.serialize_str(&time.to_rfc3339())
}

#[allow(dead_code)]
pub fn serialize_option_datetime_as_rfc3339<S: Serializer>(
time: &Option<DateTime<Utc>>,
serializer: S,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 5 additions & 1 deletion src/cat-data-service/src/legacy_service/v0/fund.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::{legacy_service::handle_result, service::Error, state::State, types::SerdeType};
use crate::{
legacy_service::{handle_result, types::SerdeType},
service::Error,
state::State,
};
use axum::{routing::get, Router};
use event_db::types::vit_ss::fund::FundWithNext;
use std::sync::Arc;
Expand Down
6 changes: 5 additions & 1 deletion src/cat-data-service/src/legacy_service/v1/event/ballots.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::{legacy_service::handle_result, service::Error, state::State, types::SerdeType};
use crate::{
legacy_service::{handle_result, types::SerdeType},
service::Error,
state::State,
};
use axum::{extract::Path, routing::get, Router};
use event_db::types::{ballot::ObjectiveBallots, event::EventId};
use std::sync::Arc;
Expand Down
6 changes: 5 additions & 1 deletion src/cat-data-service/src/legacy_service/v1/event/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use super::LimitOffset;
use crate::{legacy_service::handle_result, service::Error, state::State, types::SerdeType};
use crate::{
legacy_service::{handle_result, types::SerdeType},
service::Error,
state::State,
};
use axum::{
extract::{Path, Query},
routing::get,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::{legacy_service::handle_result, service::Error, state::State, types::SerdeType};
use crate::{
legacy_service::{handle_result, types::SerdeType},
service::Error,
state::State,
};
use axum::{extract::Path, routing::get, Router};
use event_db::types::{ballot::ProposalBallot, event::EventId, objective::ObjectiveId};
use std::sync::Arc;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::{
legacy_service::{handle_result, v1::LimitOffset},
legacy_service::{handle_result, types::SerdeType, v1::LimitOffset},
service::Error,
state::State,
types::SerdeType,
};
use axum::{
extract::{Path, Query},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::{legacy_service::handle_result, service::Error, state::State, types::SerdeType};
use crate::{
legacy_service::{handle_result, types::SerdeType},
service::Error,
state::State,
};
use axum::{extract::Path, routing::get, Router};
use event_db::types::{
ballot::Ballot, event::EventId, objective::ObjectiveId, proposal::ProposalId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::{
legacy_service::{handle_result, v1::LimitOffset},
legacy_service::{handle_result, types::SerdeType, v1::LimitOffset},
service::Error,
state::State,
types::SerdeType,
};
use axum::{
extract::{Path, Query},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::{
legacy_service::{handle_result, v1::LimitOffset},
legacy_service::{handle_result, types::SerdeType, v1::LimitOffset},
service::Error,
state::State,
types::SerdeType,
};
use axum::{
extract::{Path, Query},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::{
legacy_service::{handle_result, v1::LimitOffset},
legacy_service::{handle_result, types::SerdeType, v1::LimitOffset},
service::Error,
state::State,
types::SerdeType,
};
use axum::{
extract::{Path, Query},
Expand Down
6 changes: 4 additions & 2 deletions src/cat-data-service/src/legacy_service/v1/jorm_mock/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::{
legacy_service::handle_result,
legacy_service::{
handle_result,
types::jorm_mock::{AccountId, AccountVote, Fragments, FragmentsProcessingSummary},
},
service::Error,
state::State,
types::jorm_mock::{AccountId, AccountVote, Fragments, FragmentsProcessingSummary},
};
use axum::{
extract::Path,
Expand Down
6 changes: 5 additions & 1 deletion src/cat-data-service/src/legacy_service/v1/registration.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::{legacy_service::handle_result, service::Error, state::State, types::SerdeType};
use crate::{
legacy_service::{handle_result, types::SerdeType},
service::Error,
state::State,
};
use axum::{
extract::{Path, Query},
routing::get,
Expand Down
6 changes: 5 additions & 1 deletion src/cat-data-service/src/legacy_service/v1/search.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::{legacy_service::handle_result, service::Error, state::State, types::SerdeType};
use crate::{
legacy_service::{handle_result, types::SerdeType},
service::Error,
state::State,
};
use axum::{extract::Query, routing::post, Json, Router};
use event_db::types::search::{SearchQuery, SearchResult};
use serde::Deserialize;
Expand Down
1 change: 0 additions & 1 deletion src/cat-data-service/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ mod logger;
mod service;
mod settings;
mod state;
mod types;

#[tokio::main]
async fn main() -> Result<(), cli::Error> {
Expand Down
1 change: 0 additions & 1 deletion src/cat-data-service/src/service/api/health/live_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use crate::service::common::responses::resp_2xx::NoContent;
use crate::service::common::responses::resp_5xx::{ServerError, ServiceUnavailable};

use poem_extensions::response;
use poem_extensions::UniResponse::T204;
use tracing::{error, info, warn};
Expand Down
77 changes: 55 additions & 22 deletions src/cat-data-service/src/service/api/health/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use crate::service::common::tags::ApiTags;
use poem_openapi::OpenApi;

mod live_get;
mod ready_get;
mod started_get;

use crate::service::common::tags::ApiTags;

use poem_openapi::OpenApi;

pub(crate) struct HealthApi;

#[OpenApi(prefix_path = "/health", tag = "ApiTags::Health")]
Expand All @@ -16,17 +15,16 @@ impl HealthApi {
/// This endpoint is used to determine if the service has started properly
/// and is able to serve requests.
///
/// ## Responses
///
/// * 204 No Content - Service is Started and can serve requests.
/// * 500 Server Error - If anything within this function fails unexpectedly.
/// * 503 Service Unavailable - Service has not started, do not send other requests yet.
///
/// ## Note
///
/// *This endpoint is for internal use of the service deployment infrastructure.
/// It may not be exposed publicly.*
///
/// ## Responses
///
/// * 204 No Content - Service is Started and can serve requests.
/// * 500 Server Error - If anything within this function fails unexpectedly.
/// * 503 Service Unavailable - Service has not started, do not send other requests yet.
async fn started_get(&self) -> started_get::AllResponses {
started_get::endpoint().await
}
Expand All @@ -36,18 +34,17 @@ impl HealthApi {
///
/// This endpoint is used to determine if the service is ready and able to serve requests.
///
/// ## Note
///
/// *This endpoint is for internal use of the service deployment infrastructure.
/// It may not be exposed publicly.*
///
/// ## Responses
///
/// * 204 No Content - Service is Ready and can serve requests.
/// * 500 Server Error - If anything within this function fails unexpectedly.
/// * 503 Service Unavailable - Service is not ready, requests to other
/// endpoints should not be sent until the service becomes ready.
///
/// ## Note
///
/// *This endpoint is for internal use of the service deployment infrastructure.
/// It may not be exposed publicly.*
///
async fn ready_get(&self) -> ready_get::AllResponses {
ready_get::endpoint().await
}
Expand All @@ -57,18 +54,54 @@ impl HealthApi {
///
/// This endpoint is used to determine if the service is live.
///
/// ## Responses
///
/// * 204 No Content - Service is Live and can serve requests.
/// * 500 Server Error - If anything within this function fails unexpectedly.
/// * 503 Service Unavailable - Service is not Live. It may need to be restarted.
///
/// ## Note
///
/// *This endpoint is for internal use of the service deployment infrastructure.
/// It may not be exposed publicly. Refer to []*
///
/// ## Responses
///
/// * 204 No Content - Service is OK and can keep running.
/// * 500 Server Error - If anything within this function fails unexpectedly. (Possible but unlikely)
/// * 503 Service Unavailable - Service is possibly not running reliably.
async fn live_get(&self) -> live_get::AllResponses {
live_get::endpoint().await
}
}

/// Need to setup and run a test event db instance
/// To do it you can use the following commands:
/// Prepare docker images
/// ```
/// earthly ./containers/event-db-migrations+docker --data=test
/// ```
/// Run event-db container
/// ```
/// docker-compose -f src/event-db/docker-compose.yml up migrations
/// ```
/// Also need establish `EVENT_DB_URL` env variable with the following value
/// ```
/// EVENT_DB_URL="postgres://catalyst-event-dev:CHANGE_ME@localhost/CatalystEventDev"
/// ```
/// https://github.com/input-output-hk/catalyst-core/tree/main/src/event-db/Readme.md
#[cfg(test)]
mod tests {
use crate::{service::poem_service::tests::mk_test_app, state::State};
use poem::http::StatusCode;
use std::sync::Arc;

#[tokio::test]
async fn health_test() {
let state = Arc::new(State::new(None).await.unwrap());
let app = mk_test_app(state);

let resp = app.get("/api/health/started").send().await;
resp.assert_status(StatusCode::NO_CONTENT);

let resp = app.get("/api/health/ready").send().await;
resp.assert_status(StatusCode::NO_CONTENT);

let resp = app.get("/api/health/live").send().await;
resp.assert_status(StatusCode::NO_CONTENT);
}
}
1 change: 0 additions & 1 deletion src/cat-data-service/src/service/api/health/ready_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use crate::service::common::responses::resp_2xx::NoContent;
use crate::service::common::responses::resp_5xx::{ServerError, ServiceUnavailable};

use poem_extensions::response;
use poem_extensions::UniResponse::T204;

Expand Down
1 change: 0 additions & 1 deletion src/cat-data-service/src/service/api/health/started_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use crate::service::common::responses::resp_2xx::NoContent;
use crate::service::common::responses::resp_5xx::{ServerError, ServiceUnavailable};

use poem_extensions::response;
use poem_extensions::UniResponse::T204;

Expand Down
30 changes: 17 additions & 13 deletions src/cat-data-service/src/service/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
//!
//! This defines all endpoints for the Catalyst Data Service API.
//! It however does NOT contain any processing for them, that is defined elsewhere.
use crate::settings::API_URL_PREFIX;
use health::HealthApi;
use poem_openapi::{ContactObject, LicenseObject, OpenApiService, ServerObject};
use registration::RegistrationApi;
use test_endpoints::TestApi;

use crate::settings::API_URL_PREFIX;

mod health;
mod registration;
mod test_endpoints;

/// The name of the API
Expand Down Expand Up @@ -54,18 +55,21 @@ fn get_api_license() -> LicenseObject {
const TERMS_OF_SERVICE: &str =
"https://github.com/input-output-hk/catalyst-core/blob/main/book/src/98_CODE_OF_CONDUCT.md";

/// Combine all the API's into one
pub(crate) type OpenApiServiceT = OpenApiService<(TestApi, HealthApi), ()>;

/// Create the OpenAPI definition
pub(crate) fn mk_api(hosts: Vec<String>) -> OpenApiServiceT {
let mut service = OpenApiService::new((TestApi, HealthApi), API_TITLE, API_VERSION)
.contact(get_api_contact())
.description(API_DESCRIPTION)
.license(get_api_license())
.summary(API_SUMMARY)
.terms_of_service(TERMS_OF_SERVICE)
.url_prefix(API_URL_PREFIX.as_str());
pub(crate) fn mk_api(
hosts: Vec<String>,
) -> OpenApiService<(TestApi, HealthApi, RegistrationApi), ()> {
let mut service = OpenApiService::new(
(TestApi, HealthApi, RegistrationApi),
API_TITLE,
API_VERSION,
)
.contact(get_api_contact())
.description(API_DESCRIPTION)
.license(get_api_license())
.summary(API_SUMMARY)
.terms_of_service(TERMS_OF_SERVICE)
.url_prefix(API_URL_PREFIX.as_str());

// Add all the hosts where this API should be reachable.
for host in hosts {
Expand Down
Loading

0 comments on commit 77185a1

Please sign in to comment.