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

Move models to owning services and simplify lib imports #15

Merged
merged 2 commits into from
Jul 25, 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
2 changes: 1 addition & 1 deletion cronback-api-model/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use chrono::{DateTime, Utc};
#[cfg(feature = "dto")]
use dto::{FromProto, IntoProto};
#[cfg(feature = "dto")]
use lib::types::RunId;
use lib::prelude::*;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, skip_serializing_none};
use strum::Display;
Expand Down
2 changes: 1 addition & 1 deletion cronback-api-model/src/trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub struct Trigger {
// or outputed in the API. This is here just for IntoProto to work
#[cfg_attr(feature = "server", serde(skip))]
#[cfg(feature = "dto")]
pub id: Option<lib::types::TriggerId>,
pub id: Option<lib::prelude::TriggerId>,
#[cfg_attr(
feature = "validation",
validate(length(
Expand Down
10 changes: 7 additions & 3 deletions cronback-lib/clients/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
pub mod dispatcher_client;
pub mod metadata_srv_client;
pub mod scheduler_client;
mod dispatcher_client;
mod metadata_srv_client;
mod scheduler_client;

pub use dispatcher_client::*;
pub use metadata_srv_client::*;
pub use scheduler_client::*;
6 changes: 2 additions & 4 deletions cronback-lib/database/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
pub mod attempt_log_store;
mod errors;
pub mod models;
pub mod pagination;
pub mod run_store;
mod pagination;

pub use errors::DatabaseError;
use migration::{Migrator, MigratorTrait};
pub use pagination::*;
use sea_orm::TransactionTrait;

#[derive(Clone)]
Expand Down
7 changes: 0 additions & 7 deletions cronback-lib/database/models/mod.rs

This file was deleted.

5 changes: 0 additions & 5 deletions cronback-lib/database/models/prelude.rs

This file was deleted.

3 changes: 1 addition & 2 deletions cronback-lib/events.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub use proto::events::*;
/// e!(context = ctx, TriggerRunCreated { meta: run.meta().into() });
#[macro_export]
macro_rules! e {
Expand Down Expand Up @@ -31,7 +30,7 @@ macro_rules! e {
}

/// Emits the event to the current events subscriber
pub fn log_event(event: Event) {
pub fn log_event(event: proto::events::Event) {
// serialize the event to JSON and log it to target `events`
let event = serde_json::to_string(&event).unwrap();
tracing::info!(target: "events", "{}", event);
Expand Down
2 changes: 1 addition & 1 deletion cronback-lib/grpc_client_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl<T: GrpcClientType> GrpcClientFactory for GrpcClientProvider<T> {
}
}

pub mod test_helpers {
pub mod grpc_test_helpers {
use std::sync::Arc;

use hyper::Uri;
Expand Down
27 changes: 18 additions & 9 deletions cronback-lib/lib.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
pub mod clients;
pub mod config;
mod config;
mod consts;
pub mod database;
pub mod grpc_client_provider;
mod database;
mod grpc_client_provider;
mod grpc_helpers;
pub mod model;
mod model;
mod rpc_middleware;
mod shutdown;
mod types;

pub mod clients;
pub mod events;
pub mod netutils;
pub mod rpc_middleware;
pub mod service;
pub mod shutdown;
pub mod types;

pub mod events;
mod ext;

pub use grpc_client_provider::*;
pub use rpc_middleware::*;
pub use shutdown::*;

pub use crate::config::*;

pub mod prelude {
pub use crate::consts::*;
pub use crate::database::*;
pub use crate::events::e;
pub use crate::ext::*;
pub use crate::grpc_helpers::*;
pub use crate::model::*;
pub use crate::types::*;
}
11 changes: 0 additions & 11 deletions cronback-lib/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,3 @@ pub use ids::*;
pub use payload::*;
pub use request::*;
pub use webhook::*;

// Re-export the database models from this lib as well to reduce the amount
// of changes in the same PR. TODO: Move the models out of the shared lib
// and into their own components
pub use crate::database::models::attempts::{
AttemptDetails,
AttemptStatus,
Model as Attempt,
WebhookAttemptDetails,
};
pub use crate::database::models::runs::{Model as Run, RunStatus};
12 changes: 4 additions & 8 deletions cronback-services/src/api/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ use std::str::FromStr;
use base64::Engine;
use chrono::Utc;
use cronback_api_model::admin::CreateAPIkeyRequest;
use lib::database::models::api_keys;
use lib::database::DatabaseError;
use lib::prelude::ValidShardedId;
use lib::types::ProjectId;
use lib::prelude::*;
use once_cell::sync::Lazy;
use regex::Regex;
use sha2::{Digest, Sha512};
Expand All @@ -16,6 +13,7 @@ use tracing::error;
use uuid::Uuid;

use super::auth_store::AuthStore;
use super::db_model::{api_keys, ApiKey};
use super::errors::ApiError;

pub static API_KEY_PREFIX: &str = "sk_";
Expand Down Expand Up @@ -63,7 +61,7 @@ impl Authenticator {
let key = SecretApiKey::generate();
let hashed = key.hash(HashVersion::default());

let model = api_keys::Model {
let model = ApiKey {
key_id: hashed.key_id,
hash: hashed.hash,
hash_version: hashed.hash_version.to_string(),
Expand Down Expand Up @@ -123,7 +121,7 @@ impl Authenticator {
pub async fn list_keys(
&self,
project: &ValidShardedId<ProjectId>,
) -> Result<Vec<api_keys::Model>, AuthError> {
) -> Result<Vec<ApiKey>, AuthError> {
let res = self.store.list_keys(project).await?;
Ok(res)
}
Expand Down Expand Up @@ -235,8 +233,6 @@ mod tests {
use std::str::FromStr;

use cronback_api_model::admin::CreateAPIkeyRequest;
use lib::database::Database;
use lib::types::ProjectId;

use super::*;
use crate::api::auth_store::SqlAuthStore;
Expand Down
3 changes: 1 addition & 2 deletions cronback-services/src/api/auth_middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use axum::extract::State;
use axum::http::{self, HeaderMap, HeaderValue, Request};
use axum::middleware::Next;
use axum::response::IntoResponse;
use lib::model::{ModelId, ValidShardedId};
use lib::types::ProjectId;
use lib::prelude::*;

use super::auth::{AuthError, SecretApiKey};
use super::errors::ApiError;
Expand Down
43 changes: 14 additions & 29 deletions cronback-services/src/api/auth_store.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
use async_trait::async_trait;
use lib::database::models::api_keys;
use lib::database::models::prelude::ApiKeys;
use lib::database::{Database, DatabaseError};
use lib::model::ValidShardedId;
use lib::types::ProjectId;
use lib::prelude::*;
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter};

use crate::api::db_model::{api_keys, ApiKey, ApiKeys};

pub type AuthStoreError = DatabaseError;

#[async_trait]
pub trait AuthStore {
async fn save_key(
&self,
key: api_keys::Model,
) -> Result<(), AuthStoreError>;
async fn save_key(&self, key: ApiKey) -> Result<(), AuthStoreError>;

async fn get_key(
&self,
key: &str,
) -> Result<Option<api_keys::Model>, AuthStoreError>;
) -> Result<Option<ApiKey>, AuthStoreError>;

/// Returns true if the key got deleted, false if the key didn't exist
async fn delete_key(
Expand All @@ -30,7 +25,7 @@ pub trait AuthStore {
async fn list_keys(
&self,
project: &ValidShardedId<ProjectId>,
) -> Result<Vec<api_keys::Model>, AuthStoreError>;
) -> Result<Vec<ApiKey>, AuthStoreError>;
}

pub struct SqlAuthStore {
Expand All @@ -45,21 +40,16 @@ impl SqlAuthStore {

#[async_trait]
impl AuthStore for SqlAuthStore {
async fn save_key(
&self,
key: api_keys::Model,
) -> Result<(), AuthStoreError> {
async fn save_key(&self, key: ApiKey) -> Result<(), AuthStoreError> {
let active_model: api_keys::ActiveModel = key.into();
api_keys::Entity::insert(active_model)
.exec(&self.db.orm)
.await?;
ApiKeys::insert(active_model).exec(&self.db.orm).await?;
Ok(())
}

async fn get_key(
&self,
key_id: &str,
) -> Result<Option<api_keys::Model>, AuthStoreError> {
) -> Result<Option<ApiKey>, AuthStoreError> {
let res = ApiKeys::find_by_id(key_id).one(&self.db.orm).await?;
Ok(res)
}
Expand All @@ -80,7 +70,7 @@ impl AuthStore for SqlAuthStore {
async fn list_keys(
&self,
project: &ValidShardedId<ProjectId>,
) -> Result<Vec<api_keys::Model>, AuthStoreError> {
) -> Result<Vec<ApiKey>, AuthStoreError> {
let results = ApiKeys::find()
.filter(api_keys::Column::ProjectId.eq(project.clone()))
.all(&self.db.orm)
Expand All @@ -91,27 +81,22 @@ impl AuthStore for SqlAuthStore {

#[cfg(test)]
mod tests {

use chrono::Utc;
use lib::database::models::api_keys::{self, Metadata};
use lib::database::Database;
use lib::prelude::ValidShardedId;
use lib::types::ProjectId;

use super::{AuthStore, SqlAuthStore};
use super::*;

fn build_model(
key_id: &str,
project: &ValidShardedId<ProjectId>,
) -> api_keys::Model {
api_keys::Model {
) -> ApiKey {
ApiKey {
key_id: key_id.to_string(),
hash: "hashash".to_string(),
hash_version: "v1".to_string(),
project_id: project.clone(),
name: key_id.to_string(),
created_at: Utc::now(),
metadata: Metadata {
metadata: api_keys::Metadata {
creator_user_id: None,
},
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3

use chrono::{DateTime, Utc};
use lib::prelude::*;
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

use crate::model::ValidShardedId;
use crate::types::ProjectId;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "api_keys")]
pub struct Model {
Expand Down
2 changes: 2 additions & 0 deletions cronback-services/src/api/db_model/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub(crate) mod api_keys;
pub(crate) use api_keys::{Entity as ApiKeys, Model as ApiKey};
2 changes: 1 addition & 1 deletion cronback-services/src/api/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use axum::extract::rejection::JsonRejection;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::Json;
use lib::grpc_client_provider::GrpcClientError;
use lib::GrpcClientError;
use serde::Serialize;
use serde_with::skip_serializing_none;
use thiserror::Error;
Expand Down
2 changes: 1 addition & 1 deletion cronback-services/src/api/extractors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use axum::extract::{FromRequest, FromRequestParts, Path};
use axum::http::request::Parts;
use axum::http::Request;
use axum::Json;
use lib::model::{ModelId, ValidShardedId};
use lib::prelude::*;
use serde::de::DeserializeOwned;
use validator::Validate;

Expand Down
3 changes: 1 addition & 2 deletions cronback-services/src/api/handlers/admin/api_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use cronback_api_model::admin::{
CreateAPIKeyResponse,
CreateAPIkeyRequest,
};
use lib::model::ValidShardedId;
use lib::types::ProjectId;
use lib::prelude::*;
use proto::common::PaginationOut;

use crate::api::errors::ApiError;
Expand Down
3 changes: 1 addition & 2 deletions cronback-services/src/api/handlers/admin/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use axum::response::IntoResponse;
use axum::{Extension, Json};
use cronback_api_model::admin::CreateProjectResponse as CreateProjectHttpResponse;
use hyper::StatusCode;
use lib::prelude::ModelId;
use lib::types::{ProjectId, RequestId};
use lib::prelude::*;
use proto::metadata_svc::{CreateProjectRequest, SetProjectStatusRequest};
use proto::projects::ProjectStatus;

Expand Down
5 changes: 2 additions & 3 deletions cronback-services/src/api/handlers/triggers/cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ use std::sync::Arc;

use axum::extract::{Path, State};
use axum::{Extension, Json};
use lib::model::ValidShardedId;
use lib::types::{ProjectId, RequestId};
use lib::prelude::*;
use proto::scheduler_svc::CancelTriggerRequest;

use crate::api::api_model::Trigger;
use crate::api::errors::ApiError;
use crate::api::model::Trigger;
use crate::api::AppState;

#[tracing::instrument(skip(state))]
Expand Down
3 changes: 1 addition & 2 deletions cronback-services/src/api/handlers/triggers/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use axum::extract::{Path, State};
use axum::http::StatusCode;
use axum::response::IntoResponse;
use axum::{debug_handler, Extension};
use lib::model::ValidShardedId;
use lib::types::{ProjectId, RequestId};
use lib::prelude::*;
use proto::scheduler_svc::DeleteTriggerRequest;

use crate::api::errors::ApiError;
Expand Down
5 changes: 2 additions & 3 deletions cronback-services/src/api/handlers/triggers/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ use axum::http::StatusCode;
use axum::response::IntoResponse;
use axum::{debug_handler, Extension, Json};
use axum_extra::extract::Query;
use lib::model::ValidShardedId;
use lib::types::{ProjectId, RequestId};
use lib::prelude::*;
use proto::scheduler_svc::{GetTriggerRequest, ListTriggersRequest};
use validator::Validate;

use crate::api::api_model::{Trigger, TriggersFilter};
use crate::api::errors::ApiError;
use crate::api::model::{Trigger, TriggersFilter};
use crate::api::paginated::{Paginated, Pagination};
use crate::api::AppState;

Expand Down
Loading