move rpc environment implementation to separate files

This commit is contained in:
Dietmar Maurer 2019-01-26 15:08:02 +01:00
parent 32f3db27bd
commit a0a545c720
3 changed files with 29 additions and 24 deletions

25
src/server/environment.rs Normal file
View File

@ -0,0 +1,25 @@
use crate::api::router::*;
use std::collections::HashMap;
use serde_json::Value;
pub struct RestEnvironment {
result_attributes: HashMap<String, Value>,
}
impl RestEnvironment {
pub fn new() -> Self {
Self { result_attributes: HashMap::new() }
}
}
impl RpcEnvironment for RestEnvironment {
fn set_result_attrib(&mut self, name: &str, value: Value) {
self.result_attributes.insert(name.into(), value);
}
fn get_result_attrib(&self, name: &str) -> Option<&Value> {
self.result_attributes.get(name)
}
}

View File

@ -13,7 +13,7 @@ pub struct OutputFormatter {
pub format_error: fn(err: Error) -> Response<Body>,
}
static json_content_type: &str = "application/json;charset=UTF-8";
static JSON_CONTENT_TYPE: &str = "application/json;charset=UTF-8";
fn json_response(result: Value) -> Response<Body> {
@ -25,7 +25,7 @@ fn json_response(result: Value) -> Response<Body> {
let mut response = Response::new(raw.into());
response.headers_mut().insert(
header::CONTENT_TYPE,
header::HeaderValue::from_static(json_content_type));
header::HeaderValue::from_static(JSON_CONTENT_TYPE));
response
}
@ -52,7 +52,7 @@ fn json_format_error(err: Error) -> Response<Body> {
let mut response = Response::new(Body::from(err.to_string()));
response.headers_mut().insert(
header::CONTENT_TYPE,
header::HeaderValue::from_static(json_content_type));
header::HeaderValue::from_static(JSON_CONTENT_TYPE));
*response.status_mut() = StatusCode::BAD_REQUEST;
response

View File

@ -2,6 +2,7 @@ use crate::tools;
use crate::api::schema::*;
use crate::api::router::*;
use crate::api::config::*;
use super::environment::RestEnvironment;
use super::formatter::*;
use std::fmt;
@ -26,27 +27,6 @@ use hyper::service::{Service, NewService};
use hyper::rt::{Future, Stream};
use hyper::header;
struct RestEnvironment {
result_attributes: HashMap<String, Value>,
}
impl RestEnvironment {
fn new() -> Self {
Self { result_attributes: HashMap::new() }
}
}
impl RpcEnvironment for RestEnvironment {
fn set_result_attrib(&mut self, name: &str, value: Value) {
self.result_attributes.insert(name.into(), value);
}
fn get_result_attrib(&self, name: &str) -> Option<&Value> {
self.result_attributes.get(name)
}
}
pub struct RestServer {
pub api_config: Arc<ApiConfig>,
}