Crates.io | service_utils_rs |
lib.rs | service_utils_rs |
version | 0.1.3 |
source | src |
created_at | 2024-08-08 07:12:23.144795 |
updated_at | 2024-08-16 11:03:49.188721 |
description | A brief description of your crate |
homepage | https://github.com/code-serenade/service_utils_rs |
repository | https://github.com/code-serenade/service_utils_rs |
max_upload_size | |
id | 1329199 |
size | 61,520 |
service_utils_rs
is a Rust library that provides utility functions to simplify and speed up service development.
Add the following dependency to your Cargo.toml
file:
[dependencies]
service_utils_rs = "0.1.3"
Create a config
directory at the root of your project and add a config.toml
file with the following content:
# config/config.toml
[jwt]
access_secret = "3a5df12e1fc87ad045e1767e3f6a285da64139de0199f3d7b1d869f03d8eae30e130bacc2018d8c2e1dced55eac6fbb45f0cf283a5f64dc75a886ac8fd3937e5"
refresh_secret = "b26f570b5d72795815f898cea04a4234a932cded824081767698e58e13ff849f3b6e23fe34efb4f6d78e342f1be4eace18135994e51a070c605c6dc9698a5fab"
audience = "test"
access_token_duration = 86400
refresh_token_duration = 172800
Ensure your settings.rs
file reads the configuration file and deserializes the settings:
use config::{Config, ConfigError};
use serde::Deserialize;
#[derive(Debug, Deserialize)]
pub struct Settings {
pub jwt: JwtCfg,
}
#[derive(Debug, Deserialize)]
pub struct JwtCfg {
pub access_secret: String,
pub refresh_secret: String,
pub audience: String,
pub access_token_duration: usize,
pub refresh_token_duration: usize,
}
impl Settings {
pub fn new(config_path: &str) -> Result<Self, ConfigError> {
let c = Config::builder()
.add_source(config::File::with_name(config_path))
.build()?;
c.try_deserialize()
}
}
In your project, use the service_utils_rs
library and pass the configuration file path:
fn main() -> Result<(), Box<dyn std::error::Error>> {
let config_path = "config/config.toml";
let settings = Settings::new(config_path)?;
println!("{:?}", settings);
Ok(())
}
By following these steps, you can configure and use the service_utils_rs
library in your project as intended.