Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3769961
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
6 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..08040f5
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+# Rust / Cargo
+target/
+Cargo.lock
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..c7c0805
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,19 @@
+[package]
+name = "serverslog"
+version = "0.1.0"
+
+authors = ["Sébastien Santoro <dereckson@espace-win.org>"]
+description = "A microservice to accept through AMQP log entries, stores them and serve the log as JSON"
+repository="https://github.com/nasqueron/infra-serverslog"
+license="BSD-2-Clause"
+
+[dependencies]
+amqp = "0.0.19"
+env_logger = "^0.3"
+iron = "0.4.*"
+log = "^0.3.5"
+router = "*"
+rust-sqlite = "0.3.0"
+
+[dev-dependencies]
+iron-test = "0.4"
diff --git a/rustfmt.toml b/rustfmt.toml
new file mode 100644
index 0000000..d705efc
--- /dev/null
+++ b/rustfmt.toml
@@ -0,0 +1,10 @@
+# Nasqueron coding standard provides nice /* */ blocks for headers
+# and sections division. We would like to keep this format among
+# the different C language we use as much as possible.
+normalize_comments = false
+
+# Use the staging area to get a view before/after of the files :
+# git add <your files>
+# rustfmt ...
+# git diff
+write_mode = "Overwrite"
diff --git a/src/http_server.rs b/src/http_server.rs
new file mode 100644
index 0000000..1b1a081
--- /dev/null
+++ b/src/http_server.rs
@@ -0,0 +1,81 @@
+/* -------------------------------------------------------------
+ Servers log microservice
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ Project: Nasqueron
+ Created: 2016-11-10
+ License: BSD-2-Clause
+ ------------------------------------------------------------- */
+
+use iron::prelude::*;
+use router::Router;
+
+use std::env;
+
+/* -------------------------------------------------------------
+ HTTP server
+
+ Spawns a HTTP server listens to $BIND environment variable
+ or by default to *:3000.
+
+ The server is based on the Iron framework.
+
+ Controller logic is defined in the web_handlers module.
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
+
+/// Prepares an Iron handler.
+fn initialize_handler() -> Router {
+ return router!(
+ index: get "/" => ::web_handlers::get,
+ status: get "/status" => ::web_handlers::alive
+ );
+}
+
+/// Gets the address the HTTP server should bind to.
+fn get_bind_address() -> String {
+ return env::var("BIND").unwrap_or("0.0.0.0:3000".to_string());
+}
+
+/// Runs an HTTP server.
+pub fn run() {
+ let listen = get_bind_address();
+
+ info!("Starting HTTP server");
+ info!("Listen to http://{}", listen);
+
+ let _server = Iron::new(initialize_handler()).http(&*listen);
+ match _server {
+ Err(err) => error!("{}", err),
+ Ok(_) => {}
+ }
+}
+
+/* -------------------------------------------------------------
+ Tests
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
+
+#[cfg(test)]
+mod tests {
+ extern crate iron_test;
+
+ use super::get_bind_address;
+ use super::initialize_handler;
+
+ use iron::Headers;
+ use iron_test::request;
+ use iron_test::response::extract_body_to_bytes;
+
+ #[test]
+ fn get_bind_address_returns_expected_default_value() {
+ assert_eq!("0.0.0.0:3000", get_bind_address());
+ }
+
+ #[test]
+ fn test_router() {
+ let response = request::get("http://localhost:3000/status",
+ Headers::new(),
+ &initialize_handler());
+ let result = extract_body_to_bytes(response.unwrap());
+
+ assert_eq!(result, b"ALIVE");
+ }
+}
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..fc50b11
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,43 @@
+/* -------------------------------------------------------------
+ Servers log microservice
+
+ Collects servers log entries and publish them as JSON
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ Project: Nasqueron
+ Created: 2016-11-10
+ License: BSD-2-Clause
+ ------------------------------------------------------------- */
+
+/* -------------------------------------------------------------
+ Crates
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
+
+#[macro_use]
+extern crate log;
+extern crate env_logger;
+
+extern crate iron;
+#[macro_use]
+extern crate router;
+
+/* -------------------------------------------------------------
+ Modules
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
+
+mod http_server;
+mod web_handlers;
+
+/* -------------------------------------------------------------
+ Application entry point
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
+
+/// Initializes the main entry point of the service.
+fn main() {
+ env_logger::init().unwrap();
+
+ info!("Initializing service {} v{}",
+ env!("CARGO_PKG_NAME"),
+ env!("CARGO_PKG_VERSION"));
+
+ http_server::run();
+}
diff --git a/src/web_handlers.rs b/src/web_handlers.rs
new file mode 100644
index 0000000..f2f8772
--- /dev/null
+++ b/src/web_handlers.rs
@@ -0,0 +1,25 @@
+/* -------------------------------------------------------------
+ Servers log microservice
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+ Project: Nasqueron
+ Created: 2016-11-10
+ License: BSD-2-Clause
+ ------------------------------------------------------------- */
+
+use iron::prelude::*;
+use iron::status;
+
+/* -------------------------------------------------------------
+ Handlers for web requests
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
+
+/// Serves a 200 ALIVE reply to allow basic service monitoring.
+pub fn alive(_: &mut Request) -> IronResult<Response> {
+ Ok(Response::with((status::Ok, "ALIVE")))
+}
+
+/// Serves the log as a JSON document.
+/// Serves a 503 reply as it's not implemented yet.
+pub fn get(_: &mut Request) -> IronResult<Response> {
+ Ok(Response::with((status::ServiceUnavailable, "Once upon a time, a log was baked.\n")))
+}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Mon, Nov 25, 18:32 (9 h, 8 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2260711
Default Alt Text
(6 KB)
Attached To
Mode
rSERVERSLOG Servers log microservice
Attached
Detach File
Event Timeline
Log In to Comment