~starkingdoms/starkingdoms

ref: f8a7045d08bbed2a8f0b0d3c5e9291db0c09c355 starkingdoms/api/src/routes/select_realm.rs -rw-r--r-- 1.9 KiB
f8a7045d — ghostlyzsh merged auth and entities, some planet stuff may be slightly broken 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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,
                    }
                ],
            })
        }
    }
}