This repository has been archived by the owner on Mar 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
b642cd1
commit 103ad1f
Showing
6 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") | ||
} |