mirror of
https://github.com/mimblewimble/grin.git
synced 2025-02-08 12:21:09 +03:00
63 lines
1.6 KiB
Rust
63 lines
1.6 KiB
Rust
|
// Copyright 2018 The Grin Developers
|
||
|
//
|
||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
// you may not use this file except in compliance with the License.
|
||
|
// You may obtain a copy of the License at
|
||
|
//
|
||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||
|
//
|
||
|
// Unless required by applicable law or agreed to in writing, software
|
||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
// See the License for the specific language governing permissions and
|
||
|
// limitations under the License.
|
||
|
|
||
|
use super::utils::w;
|
||
|
use chain;
|
||
|
use hyper::{Body, Request};
|
||
|
use p2p;
|
||
|
use rest::*;
|
||
|
use router::{Handler, ResponseFuture};
|
||
|
use std::sync::Weak;
|
||
|
use types::*;
|
||
|
use web::*;
|
||
|
|
||
|
// RESTful index of available api endpoints
|
||
|
// GET /v1/
|
||
|
pub struct IndexHandler {
|
||
|
pub list: Vec<String>,
|
||
|
}
|
||
|
|
||
|
impl IndexHandler {}
|
||
|
|
||
|
impl Handler for IndexHandler {
|
||
|
fn get(&self, _req: Request<Body>) -> ResponseFuture {
|
||
|
json_response_pretty(&self.list)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// Status handler. Post a summary of the server status
|
||
|
/// GET /v1/status
|
||
|
pub struct StatusHandler {
|
||
|
pub chain: Weak<chain::Chain>,
|
||
|
pub peers: Weak<p2p::Peers>,
|
||
|
}
|
||
|
|
||
|
impl StatusHandler {
|
||
|
fn get_status(&self) -> Result<Status, Error> {
|
||
|
let head = w(&self.chain)
|
||
|
.head()
|
||
|
.map_err(|e| ErrorKind::Internal(format!("can't get head: {}", e)))?;
|
||
|
Ok(Status::from_tip_and_peers(
|
||
|
head,
|
||
|
w(&self.peers).peer_count(),
|
||
|
))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Handler for StatusHandler {
|
||
|
fn get(&self, _req: Request<Body>) -> ResponseFuture {
|
||
|
result_to_response(self.get_status())
|
||
|
}
|
||
|
}
|