Skip to content

Commit

Permalink
feat(gameprovider-service): add filter of game service environments
Browse files Browse the repository at this point in the history
  • Loading branch information
codingben committed May 13, 2023
1 parent 9e49684 commit 5147a2f
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/gameprovider-service/src/main.rs
@@ -1,7 +1,6 @@
use actix_web::{web::Data, App, Responder, HttpResponse, HttpServer};
use dotenv::dotenv;
use serde::{Serialize, Deserialize};
use serde_yaml::from_str;
use std::{env, io};

#[derive(Serialize, Deserialize)]
Expand All @@ -10,9 +9,10 @@ struct Config {
game_services: Vec<GameService>,
}

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Clone)]
struct GameService {
name: String,
environment: String,
protocol: String,
url: String,
}
Expand All @@ -22,17 +22,27 @@ async fn read_config_from_remote() -> Result<Config, reqwest::Error> {
"https://raw.githubusercontent.com/codingben/maple-fighters-configs/{}/game-services.yml",
env::var("CONFIG_SOURCE").unwrap()
);
let content = reqwest::get(config_url)
let content = reqwest::get(&config_url)
.await?
.text()
.await?;
let config: Config = serde_yaml::from_str(&content)
.unwrap();

Ok(config)
}

Ok(from_str(&content).unwrap())
fn config_to_game_services(config: Config, environment: &str) -> Vec<GameService> {
config
.game_services
.into_iter()
.filter(|service| service.environment == environment)
.collect()
}

#[actix_web::get("/games")]
async fn get_game_services(config: Data<Config>) -> impl Responder {
HttpResponse::Ok().json(&config.game_services)
async fn get_game_services(data: Data<Vec<GameService>>) -> impl Responder {
HttpResponse::Ok().json(data.get_ref())
}

#[actix_web::main]
Expand All @@ -41,11 +51,13 @@ async fn main() -> io::Result<()> {
env_logger::init();

let ip_address = env::var("IP_ADDRESS").expect("IP_ADDRESS not found");
let config = Data::new(read_config_from_remote().await.unwrap());
let environment = env::var("CONFIG_ENVIRONMENT").expect("CONFIG_ENVIRONMENT not found");
let config = read_config_from_remote().await.unwrap();
let data = Data::new(config_to_game_services(config, &environment));

HttpServer::new(move || {
App::new()
.app_data(config.clone())
.app_data(data.clone())
.service(get_game_services)
})
.bind(ip_address)?
Expand Down

0 comments on commit 5147a2f

Please sign in to comment.