Add v1 namespace for versioned api

This commit is contained in:
Luke Harding 2024-05-03 12:48:12 -04:00
parent 2074d4b476
commit f94e356f8e
3 changed files with 39 additions and 24 deletions

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()