Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
Add initial Rest API
Browse files Browse the repository at this point in the history
Adds initial setup for Rest API which adds a basic Hello World
route and begins a server using Actix.

Signed-off-by: Shannyn Telander <telander@bitwise.io>
  • Loading branch information
shannynalayna committed Mar 26, 2019
1 parent b642cd1 commit 103ad1f
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 0 deletions.
2 changes: 2 additions & 0 deletions daemon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ authors = ["Contributors to Hyperledger Grid"]
edition = "2018"

[dependencies]
actix = "0.7"
actix-web = "0.7"
clap = "2"
log = "0.4"
simple_logger = "1.0"
Expand Down
11 changes: 11 additions & 0 deletions daemon/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,21 @@ use std::fmt;

use log;

use crate::rest_api::RestApiError;

#[derive(Debug)]
pub enum DaemonError {
LoggingInitializationError(Box<log::SetLoggerError>),
ConfigurationError(Box<ConfigurationError>),
RestApiError(RestApiError),
}

impl Error for DaemonError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
DaemonError::LoggingInitializationError(err) => Some(err),
DaemonError::ConfigurationError(err) => Some(err),
DaemonError::RestApiError(err) => Some(err),
}
}
}
Expand All @@ -40,6 +44,7 @@ impl fmt::Display for DaemonError {
write!(f, "Logging initialization error: {}", e)
}
DaemonError::ConfigurationError(e) => write!(f, "Configuration error: {}", e),
DaemonError::RestApiError(e) => write!(f, "Rest API error: {}", e),
}
}
}
Expand Down Expand Up @@ -72,3 +77,9 @@ impl From<ConfigurationError> for DaemonError {
DaemonError::ConfigurationError(Box::new(err))
}
}

impl From<RestApiError> for DaemonError {
fn from(err: RestApiError) -> DaemonError {
DaemonError::RestApiError(err)
}
}
3 changes: 3 additions & 0 deletions daemon/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extern crate log;

mod config;
mod error;
mod rest_api;

use simple_logger;

Expand Down Expand Up @@ -50,6 +51,8 @@ fn run() -> Result<(), DaemonError> {

info!("Connecting to validator at {}", config.validator_endpoint());

let _ = rest_api::run()?;

Ok(())
}

Expand Down
43 changes: 43 additions & 0 deletions daemon/src/rest_api/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2019 Bitwise IO, Inc.
//
// 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 std::error::Error;
use std::fmt;

#[derive(Debug)]
pub enum RestApiError {
StdError(std::io::Error),
}

impl Error for RestApiError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
RestApiError::StdError(err) => Some(err),
}
}
}

impl fmt::Display for RestApiError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
RestApiError::StdError(e) => write!(f, "Std Error: {}", e),
}
}
}

impl From<std::io::Error> for RestApiError {
fn from(err: std::io::Error) -> RestApiError {
RestApiError::StdError(err)
}
}
54 changes: 54 additions & 0 deletions daemon/src/rest_api/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2019 Bitwise IO, Inc.
//
// 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.

mod error;
mod route_handler;

use actix_web::{http::Method, server, App};

pub use crate::rest_api::error::RestApiError;
use crate::rest_api::route_handler::index;

fn create_app() -> App {
App::new().resource("/", |r| r.method(Method::GET).f(index))
}

pub fn run() -> Result<i32, RestApiError> {
let sys = actix::System::new("Grid-Rest-API");

server::new(create_app).bind("127.0.0.1:8080")?.start();

Ok(sys.run())
}

#[cfg(test)]
mod test {
use super::*;
use actix_web::test::TestServer;
use actix_web::HttpMessage;
use std::str;

#[test]
fn index_test() {
let mut srv = TestServer::new(|app| app.handler(index));

let req = srv.get().finish().unwrap();
let resp = srv.execute(req.send()).unwrap();
assert!(resp.status().is_success());

let body_bytes = srv.execute(resp.body()).unwrap();
let body_str = str::from_utf8(&body_bytes).unwrap();
assert_eq!(body_str, "Hello world!");
}
}
19 changes: 19 additions & 0 deletions daemon/src/rest_api/route_handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2019 Bitwise IO, Inc.
//
// 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 actix_web::{HttpRequest, HttpResponse};

pub fn index(_req: &HttpRequest) -> HttpResponse {
HttpResponse::Ok().body("Hello world!")
}

0 comments on commit 103ad1f

Please sign in to comment.