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

feat: Add registration/voter endpoint to poem service | NPG-8137 #559

Merged
merged 35 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
8b8b275
add health tests
Mr-Leshiy Sep 12, 2023
d20c45a
refactor
Mr-Leshiy Sep 12, 2023
27e596a
add get_voter endpoint
Mr-Leshiy Sep 12, 2023
98fa4e0
update
Mr-Leshiy Sep 12, 2023
ba891e3
fix
Mr-Leshiy Sep 12, 2023
d4ba8cb
add tests
Mr-Leshiy Sep 12, 2023
7ecff29
fix clippy
Mr-Leshiy Sep 12, 2023
d3147f9
return back Responses sections
Mr-Leshiy Sep 14, 2023
6499a49
update
Mr-Leshiy Sep 14, 2023
ed2939d
update
Mr-Leshiy Sep 14, 2023
b42867c
fix visibility
Mr-Leshiy Sep 18, 2023
bced403
fix
Mr-Leshiy Sep 18, 2023
6a8f3f4
update
Mr-Leshiy Sep 18, 2023
aed00f9
move axum types under the legacy_service module
Mr-Leshiy Sep 18, 2023
4bfb3a0
Update src/cat-data-service/src/service/api/registration/mod.rs
Mr-Leshiy Sep 19, 2023
91eb0d8
Update src/cat-data-service/src/service/common/objects/stake_public_k…
Mr-Leshiy Sep 19, 2023
66aea47
Update src/cat-data-service/src/service/api/registration/mod.rs
Mr-Leshiy Sep 19, 2023
2c26280
Update src/cat-data-service/src/service/api/registration/mod.rs
Mr-Leshiy Sep 19, 2023
bf7fa31
Update src/cat-data-service/src/service/api/registration/mod.rs
Mr-Leshiy Sep 19, 2023
40a25f1
Update src/cat-data-service/src/service/api/registration/mod.rs
Mr-Leshiy Sep 19, 2023
c00fbe9
Update src/cat-data-service/src/service/docs/mod.rs
Mr-Leshiy Sep 19, 2023
ebb9702
Update src/cat-data-service/src/state/mod.rs
Mr-Leshiy Sep 19, 2023
4dd6e6a
add jorm_mock docs
Mr-Leshiy Sep 19, 2023
8161ea4
Update src/cat-data-service/src/service/poem_service.rs
Mr-Leshiy Sep 19, 2023
1f27b80
Update src/cat-data-service/src/service/common/objects/delegator_addr…
Mr-Leshiy Sep 19, 2023
1423ea9
Update src/cat-data-service/src/service/common/objects/delegator_addr…
Mr-Leshiy Sep 19, 2023
2ea7f3f
Update src/cat-data-service/src/service/common/objects/event_id.rs
Mr-Leshiy Sep 19, 2023
e7152b6
Update src/cat-data-service/src/service/common/objects/stake_public_k…
Mr-Leshiy Sep 19, 2023
621d32c
Update src/cat-data-service/src/service/common/objects/voter.rs
Mr-Leshiy Sep 19, 2023
55dbdcc
fix
Mr-Leshiy Sep 19, 2023
bd103f0
Update src/cat-data-service/src/service/common/objects/voter_group_id.rs
Mr-Leshiy Sep 19, 2023
562413a
Update src/cat-data-service/src/service/common/objects/voter_info.rs
Mr-Leshiy Sep 19, 2023
e9ff201
Update src/cat-data-service/src/service/common/objects/voting_key.rs
Mr-Leshiy Sep 19, 2023
731340a
fix
Mr-Leshiy Sep 19, 2023
be11f69
fix visibility
Mr-Leshiy Sep 19, 2023
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
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
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
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
Mr-Leshiy marked this conversation as resolved.
Show resolved Hide resolved
///
/// * 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
Loading