Hastic standalone https://hastic.io
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.2 KiB

use hastic::config::Config;
3 years ago
use hastic::services::analytic_service::AnalyticService;
3 years ago
use hastic::services::{metric_service, segments_service, user_service};
3 years ago
use warp::http::HeaderValue;
3 years ago
use warp::hyper::{Body, StatusCode};
use warp::reject::Reject;
3 years ago
use warp::{body, options, Rejection, Reply};
3 years ago
use warp::{http::Response, Filter};
3 years ago
mod analytics;
3 years ago
mod auth;
mod metric;
mod segments;
3 years ago
3 years ago
use serde::Serialize;
3 years ago
use parking_lot::RwLock;
use std::sync::Arc;
#[derive(Debug)]
struct BadQuery;
impl Reject for BadQuery {}
3 years ago
#[derive(Serialize)]
3 years ago
pub struct Message {
3 years ago
message: String,
}
pub struct API<'a> {
config: &'a Config,
user_service: Arc<RwLock<user_service::UserService>>,
3 years ago
metric_service: metric_service::MetricService,
3 years ago
segments_service: segments_service::SegmentsService,
}
3 years ago
impl API<'_> {
3 years ago
pub fn new(config: &Config) -> anyhow::Result<API<'_>> {
3 years ago
let ss = segments_service::SegmentsService::new()?;
let ms = metric_service::MetricService::new(&config.prom_url, &config.query);
3 years ago
3 years ago
Ok(API {
config: config,
user_service: Arc::new(RwLock::new(user_service::UserService::new())),
3 years ago
metric_service: ms.clone(),
3 years ago
segments_service: ss.clone(),
3 years ago
})
3 years ago
}
3 years ago
fn json<T: Serialize>(t: &T) -> Response<Body> {
3 years ago
API::json_with_code(t, StatusCode::OK)
}
fn json_with_code<T: Serialize>(t: &T, status_code: StatusCode) -> Response<Body> {
3 years ago
let j = warp::reply::json(t);
let mut rs = j.into_response();
let hs = rs.headers_mut();
hs.insert("Access-Control-Allow-Origin", HeaderValue::from_static("*"));
3 years ago
hs.insert(
"Access-Control-Allow-Methods",
HeaderValue::from_static("POST, GET, OPTIONS, DELETE"),
);
hs.insert(
"Access-Control-Allow-Headers",
HeaderValue::from_static("*"),
);
3 years ago
*rs.status_mut() = status_code;
3 years ago
rs
}
pub async fn serve(&self) {
3 years ago
let mut analytic_service = AnalyticService::new(self.metric_service.clone(), self.segments_service.clone());
3 years ago
let not_found =
warp::any().map(|| warp::reply::with_status("Not found", StatusCode::NOT_FOUND));
let options = warp::any().and(options()).map(|| {
3 years ago
API::json(&Message {
3 years ago
message: "ok".to_owned(),
})
});
3 years ago
let metrics = metric::get_route(self.metric_service.clone());
let login = auth::get_route(self.user_service.clone());
let segments = segments::filters::filters(
3 years ago
self.segments_service.clone(),
analytic_service.get_client(),
);
3 years ago
let analytics = analytics::filters::filters(analytic_service.get_client());
3 years ago
let public = warp::fs::dir("public");
3 years ago
3 years ago
println!("Start server on {} port", self.config.port);
3 years ago
// TODO: move it to "server"
3 years ago
let routes = warp::path("api")
3 years ago
.and(login.or(metrics).or(segments).or(analytics).or(options))
.or(public)
.or(not_found);
3 years ago
let s1= analytic_service.serve();
let s2 = warp::serve(routes)
.run(([127, 0, 0, 1], self.config.port));
futures::future::join(s1, s2).await;
3 years ago
}
3 years ago
}