stack_epic_api/handlers/
version_api.rs

1// Copyright 2020 The Grin Developers
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use super::utils::w;
16use crate::chain;
17use crate::rest::*;
18use crate::router::{Handler, ResponseFuture};
19use crate::types::Version;
20use crate::web::*;
21use hyper::{Body, Request};
22use std::sync::Weak;
23
24const CRATE_VERSION: &str = env!("CARGO_PKG_VERSION");
25
26/// Version handler. Get running node API version
27/// GET /v1/version
28pub struct VersionHandler {
29	pub chain: Weak<chain::Chain>,
30}
31
32impl VersionHandler {
33	pub fn get_version(&self) -> Result<Version, Error> {
34		let head = w(&self.chain)?
35			.head_header()
36			.map_err(|e| Error::Internal(format!("can't get head: {}", e)))?;
37
38		Ok(Version {
39			node_version: CRATE_VERSION.to_owned(),
40			block_header_version: head.version.into(),
41		})
42	}
43}
44
45impl Handler for VersionHandler {
46	fn get(&self, _req: Request<Body>) -> ResponseFuture {
47		result_to_response(self.get_version())
48	}
49}