From f94e356f8e2f29c40281aaa8ca04070f7319b162 Mon Sep 17 00:00:00 2001 From: Luke Harding Date: Fri, 3 May 2024 12:48:12 -0400 Subject: [PATCH] Add v1 namespace for versioned api --- server/src/api/mod.rs | 27 +++++---------------------- server/src/api/v1.rs | 33 +++++++++++++++++++++++++++++++++ server/src/main.rs | 3 +-- 3 files changed, 39 insertions(+), 24 deletions(-) create mode 100644 server/src/api/v1.rs diff --git a/server/src/api/mod.rs b/server/src/api/mod.rs index 8e8c0f2..26b7892 100644 --- a/server/src/api/mod.rs +++ b/server/src/api/mod.rs @@ -1,33 +1,16 @@ // SPDX-License-Identifier: GPL-3.0-Only // Copyright (C) 2024 Luke Harding -use actix_web::{get, HttpResponse, Responder, web}; -use uuid::Uuid; +use actix_web::web; -use crate::db; +pub mod v1; pub struct AppState { pub database_url: String, } -#[get("/get_tasks")] -pub async fn get_tasks(data: web::Data) -> 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()), - }; +pub fn configure(cfg: &mut web::ServiceConfig) { + let scope = web::scope("/api").configure(v1::configure); - HttpResponse::Ok().json(tasks) -} - -#[get("/get_task/{uuid}")] -pub async fn get_task(data: web::Data, path: web::Path) -> 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) + cfg.service(scope); } diff --git a/server/src/api/v1.rs b/server/src/api/v1.rs new file mode 100644 index 0000000..b5c92d8 --- /dev/null +++ b/server/src/api/v1.rs @@ -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) -> 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, path: web::Path) -> 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) +} diff --git a/server/src/main.rs b/server/src/main.rs index 8cbd5cd..5271d28 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -76,8 +76,7 @@ async fn main() -> io::Result<()> { .app_data(web::Data::new(AppState { database_url: database_url.clone(), })) - .service(api::get_tasks) - .service(api::get_task) + .configure(api::configure) }) .bind((bind_addr, port))? .run()