// Basic handler for getting meta data about the server
use std::env::var;

use hyper::{Response, Body};
use serde_json::to_string;
use serde::Serialize;

#[derive( Serialize)]
pub struct Config {
    pub name: String,
    pub description: String,
    pub hostname: String,
    pub port: u16,
    pub protocol: String,
}

pub fn get_config() -> Config {
    Config {
        name: var("SERVER_NAME").unwrap_or("No name".into()),
        description: var("SERVER_DESCRIPTION").unwrap_or("No description".into()),
        hostname: var("SERVER_HOSTNAME").expect("Couldn't get url from environment"),
        port: var("SERVER_PORT").expect("Couldn't get port from environment").parse::<u16>().unwrap(),
        protocol: var("SERVER_PROTOCOL").expect("Couldn't get protocol from environment")
    }
}

pub async fn server_meta(response: &mut Response<Body>) {
    *response.body_mut() = Body::from(to_string(&get_config()).unwrap());
}
