Commit f10a29de authored by Aljaž Srebrnič's avatar Aljaž Srebrnič

🎉 initial commit

parents
/target
**/*.rs.bk
\ No newline at end of file
This diff is collapsed.
[package]
name = "mittelab-status"
version = "0.1.0"
authors = ["Aljaž \"g5pw\" Srebrnič <a2piratesoft@gmail.com>"]
edition = "2018"
[dependencies]
spaceapi = "^0.5"
serde_json = "*"
json-patch = "*"
iron = "0.6.*"
r2d2_redis = "*"
FROM rust:latest
WORKDIR /usr/src/mittelab-status
COPY . .
RUN cargo install --path .
CMD ["mittelab-status"]
{
"api": "0.13",
"space": "Mittelab",
"logo": "https://www.mittelab.org/images/logo.svg",
"url": "https://www.mittelab.org",
"location": {
"address": "Piazza Liberà 5/B, 34132 Trieste (TS), Italy",
"lon": 13.7733872,
"lat": 45.6566526
},
"contact": {
"email": "info@mittelab.org",
"irc": "irc://irc.freenode.net/#mittelab",
"twitter": "@mittelab",
"facebook": "https://facebook.com/mittelab",
"phone": "+390409776431",
"issue_mail": "sysadmin@mittelab.org"
},
"issue_report_channels": [
"email"
],
"state": {
"open": null
},
"projects": [
"https://git.mittelab.org",
"https://github.com/mittelab",
"https://wiki.mittelab.org/progetti/"
]
}
use spaceapi::Status;
use serde_json::{to_value, to_string, from_value, from_str};
use json_patch::merge;
use iron::prelude::*;
use iron::{status, headers, middleware};
use iron::modifiers::Header;
use r2d2_redis::{r2d2, redis, RedisConnectionManager};
pub struct ReadHandler {
status: Status,
redis_pool: r2d2::Pool<RedisConnectionManager>,
}
impl ReadHandler {
pub fn new(status: Status,
redis_pool: r2d2::Pool<RedisConnectionManager>)
-> ReadHandler {
ReadHandler {
status,
redis_pool,
}
}
fn build_response_json(&self) -> String {
// Create a mutable copy of the status struct
let mut status = to_value(self.status.clone()).unwrap();
let conn = self.redis_pool.get().unwrap();
let data: String = redis::cmd("JSON.GET").arg("spaceapi").query(&*conn).unwrap_or("".to_string());
let update = from_str(&data).unwrap();
merge(&mut status, &update);
let status: Status = from_value(status).expect("Cannot construct Status object");
// Serialize to JSON
to_string(&status)
.expect("Status object could not be serialized to JSON. \
Please open an issue at https://github.com/spaceapi-community/spaceapi-server-rs/issues")
}
}
impl middleware::Handler for ReadHandler {
/// Return the current status JSON.
fn handle(&self, _req: &mut Request) -> IronResult<Response> {
// info!("{} /{} from {}", req.method, req.url.path()[0], req.remote_addr);
// Get response body
let body = self.build_response_json();
// Create response
let response = Response::with((status::Ok, body))
// Set headers
.set(Header(headers::ContentType("application/json; charset=utf-8".parse().unwrap())))
.set(Header(headers::CacheControl(vec![headers::CacheDirective::NoCache])))
.set(Header(headers::AccessControlAllowOrigin::Any));
Ok(response)
}
}
\ No newline at end of file
use std::error::Error;
use std::fs::File;
use std::io::BufReader;
use std::path::Path;
use spaceapi::Status;
use serde_json::from_reader;
use iron::prelude::*;
use iron::status;
use r2d2_redis::{r2d2, RedisConnectionManager};
use crate::handlers::ReadHandler;
mod handlers;
fn read_spaceapi_from_file<P: AsRef<Path>>(path: P) -> Result<Status, Box<Error>> {
// Open the file in read-only mode with buffer.
let file = File::open(path)?;
let reader = BufReader::new(file);
let status = from_reader(reader)?;
Ok(status)
}
fn main() {
let manager = RedisConnectionManager::new("redis://localhost").unwrap();
let pool = r2d2::Pool::builder()
.build(manager)
.unwrap();
let base = read_spaceapi_from_file("base.json").expect("Cannot read file!");
let _server = Iron::new( ReadHandler::new(base, pool)).http("localhost:3000").unwrap();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment