Server API v1 #1

Merged
luke merged 30 commits from development into main 2024-05-04 20:22:45 +00:00
3 changed files with 39 additions and 24 deletions
Showing only changes of commit f94e356f8e - Show all commits

View File

@ -1,33 +1,16 @@
// SPDX-License-Identifier: GPL-3.0-Only // SPDX-License-Identifier: GPL-3.0-Only
// Copyright (C) 2024 Luke Harding // Copyright (C) 2024 Luke Harding
use actix_web::{get, HttpResponse, Responder, web}; use actix_web::web;
use uuid::Uuid;
use crate::db; pub mod v1;
pub struct AppState { pub struct AppState {
pub database_url: String, pub database_url: String,
} }
#[get("/get_tasks")] pub fn configure(cfg: &mut web::ServiceConfig) {
pub async fn get_tasks(data: web::Data<AppState>) -> impl Responder { let scope = web::scope("/api").configure(v1::configure);
let tasks = match db::get_tasks(data.database_url.clone()).await {
Ok(tasks) => tasks,
Err(e) => return HttpResponse::InternalServerError().body(e.to_string()),
};
HttpResponse::Ok().json(tasks) cfg.service(scope);
}
#[get("/get_task/{uuid}")]
pub async fn get_task(data: web::Data<AppState>, path: web::Path<Uuid>) -> impl Responder {
let uuid = path.into_inner();
let task = match db::get_task(data.database_url.clone(), uuid).await {
Ok(task) => task,
Err(e) => return HttpResponse::InternalServerError().body(e.to_string()),
};
HttpResponse::Ok().json(task)
} }

33
server/src/api/v1.rs Normal file
View File

@ -0,0 +1,33 @@
use actix_web::{get, HttpResponse, Responder, web};
use uuid::Uuid;
use crate::api::AppState;
use crate::db;
pub fn configure(cfg: &mut web::ServiceConfig) {
let scope = web::scope("/v1").service(get_task).service(get_tasks);
cfg.service(scope);
}
#[get("/get_tasks")]
pub async fn get_tasks(data: web::Data<AppState>) -> impl Responder {
let tasks = match db::get_tasks(data.database_url.clone()).await {
Ok(tasks) => tasks,
Err(e) => return HttpResponse::InternalServerError().body(e.to_string()),
};
HttpResponse::Ok().json(tasks)
}
#[get("/get_task/{uuid}")]
pub async fn get_task(data: web::Data<AppState>, path: web::Path<Uuid>) -> impl Responder {
let uuid = path.into_inner();
let task = match db::get_task(data.database_url.clone(), uuid).await {
Ok(task) => task,
Err(e) => return HttpResponse::InternalServerError().body(e.to_string()),
};
HttpResponse::Ok().json(task)
}

View File

@ -76,8 +76,7 @@ async fn main() -> io::Result<()> {
.app_data(web::Data::new(AppState { .app_data(web::Data::new(AppState {
database_url: database_url.clone(), database_url: database_url.clone(),
})) }))
.service(api::get_tasks) .configure(api::configure)
.service(api::get_task)
}) })
.bind((bind_addr, port))? .bind((bind_addr, port))?
.run() .run()