Skip to content

Commit

Permalink
Merge pull request #47 from spiralover/feat/http
Browse files Browse the repository at this point in the history
feat(http): added separate serializer & deserializer for JsonResponse
  • Loading branch information
Ahmard authored Aug 20, 2024
2 parents 9ed872d + 97f8e2e commit 4b284dc
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Medullah Changelog
medullah-web changelog file

## 0.14.6 (2024-08-20)
* feat(http): added separate serializer & deserializer
* feat(app-state): added 'helpers()' helper func to easily acquire struct of helpers

## 0.14.5 (2024-08-18)
* bump: upgraded packages to their latest versions

Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "medullah-web"
version = "0.14.5"
version = "0.14.6"
edition = "2021"
license = "MIT"
description = "Micro-Framework Base on Ntex"
Expand Down
12 changes: 8 additions & 4 deletions src/helpers/once_lock.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
#[allow(unused_imports)]
use std::sync::{Arc, OnceLock};

#[cfg(feature = "feat-database")]
use diesel::r2d2::ConnectionManager;
#[cfg(feature = "feat-database")]
use diesel::PgConnection;
#[cfg(feature = "feat-database")]
use diesel::r2d2::ConnectionManager;

use crate::app_state::MedullahState;
use crate::app_state::{AppHelpers, MedullahState};
#[cfg(feature = "feat-database")]
use crate::database::DatabaseConnectionHelper;
use crate::MEDULLAH;
#[cfg(feature = "feat-redis")]
use crate::services::cache_service::CacheService;
use crate::MEDULLAH;

pub trait OnceLockHelper<'a> {
fn app(&self) -> &'a MedullahState {
MEDULLAH.get().unwrap()
}

fn helpers(&self) -> &AppHelpers {
&MEDULLAH.get().unwrap().helpers
}

fn front_url(&self, url: &str) -> String {
self.app().frontend(url)
}
Expand Down
20 changes: 20 additions & 0 deletions src/helpers/responder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@ pub struct JsonResponse<T: Serialize> {
pub data: T,
}

#[derive(Debug, Serialize)]
pub struct SeJsonResponse<T> {
pub code: u16,
pub success: bool,
pub status: String,
pub timestamp: u64,
pub message: Option<String>,
pub data: T,
}

#[derive(Debug, Deserialize)]
pub struct DeJsonResponse<T> {
pub code: u16,
pub success: bool,
pub status: String,
pub timestamp: u64,
pub message: Option<String>,
pub data: T,
}

impl<T: Serialize> Display for JsonResponse<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(serde_json::to_string(self).unwrap().as_str())
Expand Down

0 comments on commit 4b284dc

Please sign in to comment.