use std::collections::HashMap;
use actix_web::{get, HttpResponse};
use actix_web::web::{Data, Query};
use log::error;
use serde::{Deserialize, Serialize};
use tera::Context;
use crate::AppState;
use crate::config::{CONFIG, StarkingdomsApiConfigRealm};
use crate::error::{APIError, APIErrorsResponse};
#[derive(Serialize)]
pub struct RealmsListTemplateContext {
pub realms: HashMap<String, StarkingdomsApiConfigRealm>,
pub back_to: String
}
#[get("/select-realm")]
pub async fn select_realm(state: Data<AppState>) -> HttpResponse {
let context = match Context::from_serialize(&RealmsListTemplateContext {
back_to: format!("{}/callback", CONFIG.base),
realms: CONFIG.realms.clone(),
}) {
Ok(r) => r,
Err(e) => {
error!("[context] error creating render context: {}", e);
return HttpResponse::InternalServerError().json(APIErrorsResponse {
errors: vec![
APIError {
code: "ERR_INTERNAL_SERVER_ERROR".to_string(),
message: "There was an error processing your request. Please try again later.".to_string(),
path: None,
}
],
})
}
};
match state.templates.render("select_realm.tera", &context) {
Ok(r) => HttpResponse::Ok().content_type("text/html").body(r),
Err(e) => {
error!("[context] error creating render context: {}", e);
HttpResponse::InternalServerError().json(APIErrorsResponse {
errors: vec![
APIError {
code: "ERR_INTERNAL_SERVER_ERROR".to_string(),
message: "There was an error processing your request. Please try again later.".to_string(),
path: None,
}
],
})
}
}
}