Skip to content

Commit

Permalink
grafana-data-source: Pull hyper/tokio only in native environment
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanewok committed Feb 17, 2020
1 parent a2ed715 commit 10f0643
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
4 changes: 2 additions & 2 deletions utils/grafana-data-source/Cargo.toml
Expand Up @@ -8,8 +8,6 @@ edition = "2018"

[dependencies]
log = "0.4.8"
hyper = { version = "0.13.1", default-features = false, features = ["stream"] }
tokio = "0.2"
futures-util = { version = "0.3.1", default-features = false, features = ["io"] }
serde_json = "1"
serde = { version = "1", features = ["derive"] }
Expand All @@ -21,3 +19,5 @@ derive_more = "0.99"

[target.'cfg(not(target_os = "unknown"))'.dependencies]
async-std = { version = "1.0.1", features = ["unstable"] }
hyper = { version = "0.13.1", default-features = false, features = ["stream"] }
tokio = "0.2"
10 changes: 7 additions & 3 deletions utils/grafana-data-source/src/lib.rs
Expand Up @@ -72,11 +72,13 @@ pub fn record_metrics_slice(metrics: &[(&str, f32)]) -> Result<(), Error> {
#[derive(Debug, derive_more::Display, derive_more::From)]
pub enum Error {
/// Hyper internal error.
#[cfg(not(target_os = "unknown"))]
Hyper(hyper::Error),
/// Serialization/deserialization error.
Serde(serde_json::Error),
/// Http request error.
#[cfg(not(target_os = "unknown"))]
Http(hyper::http::Error),
/// Serialization/deserialization error.
Serde(serde_json::Error),
/// Timestamp error.
Timestamp(TryFromIntError),
/// i/o error.
Expand All @@ -86,9 +88,11 @@ pub enum Error {
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
#[cfg(not(target_os = "unknown"))]
Error::Hyper(error) => Some(error),
Error::Serde(error) => Some(error),
#[cfg(not(target_os = "unknown"))]
Error::Http(error) => Some(error),
Error::Serde(error) => Some(error),
Error::Timestamp(error) => Some(error),
Error::Io(error) => Some(error)
}
Expand Down
6 changes: 5 additions & 1 deletion utils/grafana-data-source/src/server.rs
Expand Up @@ -15,12 +15,15 @@
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

use serde::{Serialize, de::DeserializeOwned};
use hyper::{Body, Request, Response, header, service::{service_fn, make_service_fn}, Server};
use chrono::{Duration, Utc};
use futures_util::{FutureExt, TryStreamExt, future::{Future, select, Either}};
use futures_timer::Delay;
use crate::{DATABASE, Error, types::{Target, Query, TimeseriesData, Range}};

#[cfg(not(target_os = "unknown"))]
use hyper::{Body, Request, Response, header, service::{service_fn, make_service_fn}, Server};

#[cfg(not(target_os = "unknown"))]
async fn api_response(req: Request<Body>) -> Result<Response<Body>, Error> {
match req.uri().path() {
"/search" => {
Expand Down Expand Up @@ -57,6 +60,7 @@ async fn api_response(req: Request<Body>) -> Result<Response<Body>, Error> {
}
}

#[cfg(not(target_os = "unknown"))]
async fn map_request_to_response<Req, Res, T>(req: Request<Body>, transformation: T) -> Result<Response<Body>, Error>
where
Req: DeserializeOwned,
Expand Down

0 comments on commit 10f0643

Please sign in to comment.