2019-06-16 23:37:14 -04:00
|
|
|
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
|
2019-06-12 19:23:51 -04:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
struct Event {
|
|
|
|
id: Option<i32>,
|
|
|
|
timestamp: f64,
|
|
|
|
kind: String,
|
|
|
|
tags: Vec<String>,
|
|
|
|
}
|
|
|
|
|
2019-06-22 09:18:08 -04:00
|
|
|
fn store_in_db(timestamp: f64, kind: &str, tags: &[String]) -> Event {
|
2019-06-12 19:23:51 -04:00
|
|
|
// store item in db and get new_event
|
|
|
|
// use id to lookup item
|
|
|
|
Event {
|
|
|
|
id: Some(1),
|
2019-06-22 09:18:08 -04:00
|
|
|
timestamp,
|
2019-06-19 14:24:31 -04:00
|
|
|
kind: kind.to_string(),
|
|
|
|
tags: tags.to_vec(),
|
2019-06-12 19:23:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-29 02:05:27 +09:00
|
|
|
async fn capture_event(evt: web::Json<Event>) -> impl Responder {
|
2019-06-19 14:24:31 -04:00
|
|
|
let new_event = store_in_db(evt.timestamp, &evt.kind, &evt.tags);
|
2019-06-16 23:37:14 -04:00
|
|
|
format!("got event {}", new_event.id.unwrap())
|
2019-06-12 19:23:51 -04:00
|
|
|
}
|
|
|
|
|
2019-12-29 02:05:27 +09:00
|
|
|
async fn index() -> HttpResponse {
|
2019-06-12 19:23:51 -04:00
|
|
|
HttpResponse::Ok()
|
|
|
|
.content_type("text/html; charset=utf-8")
|
|
|
|
.body(include_str!("../static/form.html"))
|
|
|
|
}
|
|
|
|
|
2020-09-12 16:21:54 +01:00
|
|
|
#[actix_web::main]
|
2019-12-29 02:05:27 +09:00
|
|
|
async fn main() -> std::io::Result<()> {
|
2019-06-12 19:23:51 -04:00
|
|
|
HttpServer::new(|| {
|
|
|
|
App::new()
|
|
|
|
.route("/", web::get().to(index))
|
|
|
|
.route("/event", web::post().to(capture_event))
|
|
|
|
})
|
2020-09-12 16:21:54 +01:00
|
|
|
.bind("127.0.0.1:8080")?
|
2019-06-12 19:23:51 -04:00
|
|
|
.run()
|
2019-12-29 02:05:27 +09:00
|
|
|
.await
|
2019-06-12 18:14:10 -04:00
|
|
|
}
|