2018-03-05 22:33:44 +03:00
|
|
|
// Copyright 2018 The Grin Developers
|
2017-03-08 04:00:34 +03:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
//! RESTful API server to easily expose services as RESTful JSON/HTTP endpoints.
|
|
|
|
//! Fairly constrained on what the service API must look like by design.
|
|
|
|
//!
|
|
|
|
//! To use it, just have your service(s) implement the ApiEndpoint trait and
|
|
|
|
//! register them on a ApiServer.
|
|
|
|
|
2018-04-17 00:18:28 +03:00
|
|
|
use std::fmt::{self, Display};
|
2018-04-16 12:00:32 +03:00
|
|
|
use std::mem;
|
2017-03-08 04:00:34 +03:00
|
|
|
use std::net::ToSocketAddrs;
|
|
|
|
use std::string::ToString;
|
|
|
|
|
2018-04-17 00:18:28 +03:00
|
|
|
use failure::{Backtrace, Context, Fail};
|
2018-04-16 12:00:32 +03:00
|
|
|
use iron::middleware::Handler;
|
2018-06-14 15:16:14 +03:00
|
|
|
use iron::prelude::Iron;
|
2018-04-17 00:18:28 +03:00
|
|
|
use iron::Listening;
|
2017-10-25 20:57:48 +03:00
|
|
|
use mount::Mount;
|
2018-04-16 12:00:32 +03:00
|
|
|
use router::Router;
|
2017-03-08 04:00:34 +03:00
|
|
|
|
|
|
|
/// Errors that can be returned by an ApiEndpoint implementation.
|
2018-04-16 12:00:32 +03:00
|
|
|
|
2017-03-08 04:00:34 +03:00
|
|
|
#[derive(Debug)]
|
2018-04-16 12:00:32 +03:00
|
|
|
pub struct Error {
|
|
|
|
inner: Context<ErrorKind>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Eq, PartialEq, Debug, Fail)]
|
|
|
|
pub enum ErrorKind {
|
|
|
|
#[fail(display = "Internal error: {}", _0)]
|
2017-03-08 04:00:34 +03:00
|
|
|
Internal(String),
|
2018-04-16 12:00:32 +03:00
|
|
|
#[fail(display = "Bad arguments: {}", _0)]
|
2017-03-08 04:00:34 +03:00
|
|
|
Argument(String),
|
2018-04-16 12:00:32 +03:00
|
|
|
#[fail(display = "Not found.")]
|
2017-06-13 02:41:27 +03:00
|
|
|
NotFound,
|
2017-03-08 04:00:34 +03:00
|
|
|
}
|
|
|
|
|
2018-04-16 12:00:32 +03:00
|
|
|
impl Fail for Error {
|
|
|
|
fn cause(&self) -> Option<&Fail> {
|
|
|
|
self.inner.cause()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn backtrace(&self) -> Option<&Backtrace> {
|
|
|
|
self.inner.backtrace()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-26 03:21:56 +03:00
|
|
|
impl Display for Error {
|
2018-04-16 12:00:32 +03:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
Display::fmt(&self.inner, f)
|
2017-03-08 04:00:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-16 12:00:32 +03:00
|
|
|
impl Error {
|
|
|
|
pub fn kind(&self) -> &ErrorKind {
|
|
|
|
self.inner.get_context()
|
2017-03-08 04:00:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-16 12:00:32 +03:00
|
|
|
impl From<ErrorKind> for Error {
|
|
|
|
fn from(kind: ErrorKind) -> Error {
|
|
|
|
Error {
|
|
|
|
inner: Context::new(kind),
|
2017-06-13 02:41:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-16 12:00:32 +03:00
|
|
|
impl From<Context<ErrorKind>> for Error {
|
|
|
|
fn from(inner: Context<ErrorKind>) -> Error {
|
|
|
|
Error { inner: inner }
|
2017-03-08 04:00:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// HTTP server allowing the registration of ApiEndpoint implementations.
|
|
|
|
pub struct ApiServer {
|
|
|
|
root: String,
|
|
|
|
router: Router,
|
2017-10-25 20:57:48 +03:00
|
|
|
mount: Mount,
|
2017-06-16 19:47:29 +03:00
|
|
|
server_listener: Option<Listening>,
|
2017-03-08 04:00:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ApiServer {
|
|
|
|
/// Creates a new ApiServer that will serve ApiEndpoint implementations
|
|
|
|
/// under the root URL.
|
|
|
|
pub fn new(root: String) -> ApiServer {
|
|
|
|
ApiServer {
|
|
|
|
root: root,
|
|
|
|
router: Router::new(),
|
2017-10-25 20:57:48 +03:00
|
|
|
mount: Mount::new(),
|
2017-06-16 19:47:29 +03:00
|
|
|
server_listener: None,
|
2017-03-08 04:00:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Starts the ApiServer at the provided address.
|
2017-06-16 19:47:29 +03:00
|
|
|
pub fn start<A: ToSocketAddrs>(&mut self, addr: A) -> Result<(), String> {
|
2017-09-29 21:44:25 +03:00
|
|
|
// replace this value to satisfy borrow checker
|
2017-06-16 19:47:29 +03:00
|
|
|
let r = mem::replace(&mut self.router, Router::new());
|
2017-10-25 20:57:48 +03:00
|
|
|
let mut m = mem::replace(&mut self.mount, Mount::new());
|
|
|
|
m.mount("/", r);
|
|
|
|
let result = Iron::new(m).http(addr);
|
2017-06-16 19:47:29 +03:00
|
|
|
let return_value = result.as_ref().map(|_| ()).map_err(|e| e.to_string());
|
|
|
|
self.server_listener = Some(result.unwrap());
|
|
|
|
return_value
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Stops the API server
|
2017-09-29 21:44:25 +03:00
|
|
|
pub fn stop(&mut self) {
|
2017-06-16 19:47:29 +03:00
|
|
|
let r = mem::replace(&mut self.server_listener, None);
|
|
|
|
r.unwrap().close().unwrap();
|
2017-03-08 04:00:34 +03:00
|
|
|
}
|
|
|
|
|
2017-10-25 20:57:48 +03:00
|
|
|
/// Registers an iron handler (via mount)
|
2017-11-01 02:32:33 +03:00
|
|
|
pub fn register_handler<H: Handler>(&mut self, handler: H) -> &mut Mount {
|
|
|
|
self.mount.mount(&self.root, handler)
|
2017-03-08 04:00:34 +03:00
|
|
|
}
|
|
|
|
}
|