* Fixing some json serialization/parsing weirdness in the meta::BASIC_CONFIG initialization

This is part of a bigger change to stop using environment variables around as state as its mega cheese
This commit is contained in:
shockrah 2021-05-09 23:15:09 -07:00
parent adc5b261e8
commit b3c27b86ce

View File

@ -1,15 +1,14 @@
// Basic handler for getting meta data about the server
use std::env::var;
use std::collections::HashMap;
use crate::http::set_json_body;
use mysql_async::Pool;
use hyper::{Response, Body, StatusCode};
use serde_json::{json, to_string};
use serde_json::{json, to_string, Result as JsonResult};
use serde::{Serialize, Deserialize};
use lazy_static::lazy_static;
#[derive(Debug, Serialize)]
#[derive(Debug, Deserialize, Serialize)]
pub struct Config {
pub name: String,
pub description: String,
@ -22,20 +21,20 @@ lazy_static! {
// NOTE: this object must be access by proxy through get_config()
#[derive(Deserialize, Serialize)]
static ref BASIC_CONFIG: Config = {
let tags = var("SERVER_TAGS").unwrap();
let tags: Vec<String> = match serde_json::from_str(tags.as_str()).expect("Unable to parse tags") {
Some(tags) => tags,
None => vec![]
};
use std::fs::File;
use std::io::BufReader;
match File::open("config.json") {
Ok(file) => {
let reader = BufReader::new(file);
let rr: JsonResult<Config> = serde_json::from_reader(reader);
match rr {
Ok(meta) => meta,
Err(e) => panic!("{}", e)
}
let cfg = Config {
name: var("SERVER_NAME").unwrap_or("No name".into()),
description: var("SERVER_DESCRIPTION").unwrap_or("No description".into()),
url: var("PUBLIC_URL").unwrap(),
wsurl: var("PUBLIC_WS_URL").unwrap(),
tags
};
cfg
},
Err(e) => panic!("{}", e)
}
};
}
@ -60,7 +59,7 @@ pub async fn server_meta(response: &mut Response<Body>) {
pub async fn server_neighbors(p: &Pool, response: &mut Response<Body>) {
// This method refers to what servers have been added as **related** by the admins
// It returns a list of servers meta objects which converted to JSON
match db::Neighbor::get_all(p).await {
match db::neighbors::get_all(p).await {
Ok(neighbors) => set_json_body(response, json!({"neighbors": neighbors})),
Err(e) => {
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;