Add new task endpoint
This commit is contained in:
23
src/lib.rs
23
src/lib.rs
@@ -1,11 +1,15 @@
|
||||
use actix_web::{dev::Server, middleware, web, App, HttpResponse, HttpServer};
|
||||
use listenfd::ListenFd;
|
||||
use serde::Deserialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
use std::env;
|
||||
use std::io::Error;
|
||||
use std::net::TcpListener;
|
||||
use tracing_actix_web::TracingLogger;
|
||||
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
|
||||
pub mod contextswitch;
|
||||
pub mod observability;
|
||||
pub mod taskwarrior;
|
||||
@@ -15,7 +19,12 @@ struct TaskQuery {
|
||||
filter: Option<String>,
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(task_query))]
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct TaskDefinition {
|
||||
pub definition: String,
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "debug", skip_all, fields(filter = %task_query.filter.as_ref().unwrap_or(&"".to_string())))]
|
||||
async fn list_tasks(task_query: web::Query<TaskQuery>) -> Result<HttpResponse, Error> {
|
||||
let filter = task_query
|
||||
.filter
|
||||
@@ -28,6 +37,15 @@ async fn list_tasks(task_query: web::Query<TaskQuery>) -> Result<HttpResponse, E
|
||||
.body(serde_json::to_string(&tasks)?))
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "debug", skip_all, fields(definition = %task_definition.definition))]
|
||||
async fn add_task(task_definition: web::Json<TaskDefinition>) -> Result<HttpResponse, Error> {
|
||||
let task_id = contextswitch::add(task_definition.definition.split(' ').collect())?;
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type("application/json")
|
||||
.body(json!({ "id": task_id }).to_string()))
|
||||
}
|
||||
|
||||
async fn health_check() -> HttpResponse {
|
||||
HttpResponse::Ok().finish()
|
||||
}
|
||||
@@ -45,6 +63,7 @@ pub fn run(listener: TcpListener) -> Result<Server, std::io::Error> {
|
||||
)
|
||||
.route("/ping", web::get().to(health_check))
|
||||
.route("/tasks", web::get().to(list_tasks))
|
||||
.route("/tasks", web::post().to(add_task))
|
||||
})
|
||||
.keep_alive(60)
|
||||
.shutdown_timeout(60);
|
||||
|
||||
Reference in New Issue
Block a user