Add v1 namespace for versioned api
This commit is contained in:
parent
2074d4b476
commit
f94e356f8e
@ -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<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()),
|
||||
};
|
||||
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<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)
|
||||
cfg.service(scope);
|
||||
}
|
||||
|
33
server/src/api/v1.rs
Normal file
33
server/src/api/v1.rs
Normal 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)
|
||||
}
|
@ -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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user