This repository has been archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d0e9bc0
commit 327074c
Showing
5 changed files
with
111 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use std::sync::Arc; | ||
use anyhow::Error; | ||
|
||
use crate::{redis::{Client, RedisClient}, team::{self, Team}}; | ||
use rand::{distributions::Alphanumeric, Rng}; | ||
|
||
pub fn random_string(prefix: &str, length: usize) -> String { | ||
let suffix: String = rand::thread_rng() | ||
.sample_iter(Alphanumeric) | ||
.take(length) | ||
.map(char::from) | ||
.collect(); | ||
format!("{}{}", prefix, suffix) | ||
} | ||
|
||
pub async fn insert_new_team_in_redis(client: Arc<RedisClient>) -> Result<Team, Error> { | ||
let id = rand::thread_rng().gen_range(0..10_000_000); | ||
let token = random_string("phc_", 12); | ||
let team = Team { | ||
id: id, | ||
name: "team".to_string(), | ||
api_token: token, | ||
}; | ||
|
||
let serialized_team = serde_json::to_string(&team)?; | ||
client | ||
.set( | ||
format!("{}{}", team::TEAM_TOKEN_CACHE_PREFIX, team.api_token.clone()), | ||
serialized_team, | ||
) | ||
.await?; | ||
|
||
Ok(team) | ||
} | ||
|
||
pub fn setup_redis_client(url: Option<String>) -> Arc<RedisClient> { | ||
let redis_url = match url { | ||
Some(value) => value, | ||
None => "redis://localhost:6379/".to_string(), | ||
}; | ||
let client = RedisClient::new(redis_url).expect("Failed to create redis client"); | ||
Arc::new(client) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters