mirror of
https://codeberg.org/selfsigned-ash/antifed
synced 2026-07-21 18:49:34 +02:00
✨ Adds fetching actions
This commit is contained in:
@@ -1 +1,2 @@
|
||||
DATABASE_URL=postgres://user:password@localhost/db
|
||||
API_KEY=helloworld
|
||||
|
||||
+1
-1
@@ -8,7 +8,7 @@ rocket = { version = "0.5.1", features = ["json"] }
|
||||
serde = { version = "1.0.228", features = ["derive"] }
|
||||
dotenv = "0.15.0"
|
||||
uuid = { version = "1.18.1", features = ["v4", "serde"] }
|
||||
diesel = { version = "2.3.2", features = ["postgres", "uuid", "chrono"] }
|
||||
diesel = { version = "2.3.2", features = ["postgres", "uuid", "chrono", "r2d2"] }
|
||||
chrono = "0.4.42"
|
||||
log = "0.4.28"
|
||||
fern = "0.7.1"
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
use crate::auth::Authenticated;
|
||||
use crate::dtos::action::ActionDTO;
|
||||
use crate::models::action::Action;
|
||||
use crate::models::{AppState, DEFAULT_QUERY_LIMIT};
|
||||
use crate::schema::actions::dsl::actions;
|
||||
use diesel::{QueryDsl, RunQueryDsl, SelectableHelper};
|
||||
use rocket::State;
|
||||
use rocket::http::Status;
|
||||
use rocket::serde::json::Json;
|
||||
|
||||
#[get("/?<offset>&<limit>")]
|
||||
pub fn index(
|
||||
offset: Option<i64>,
|
||||
limit: Option<i64>,
|
||||
_auth: Authenticated,
|
||||
state: &State<AppState>,
|
||||
) -> Result<Json<Vec<ActionDTO>>, Status> {
|
||||
let mut db = state.db.get().unwrap();
|
||||
|
||||
let offset = offset.unwrap_or(0);
|
||||
let limit = limit.unwrap_or(DEFAULT_QUERY_LIMIT);
|
||||
|
||||
let results = actions
|
||||
.offset(offset)
|
||||
.limit(limit)
|
||||
.select(Action::as_select())
|
||||
.load(&mut db)
|
||||
.ok();
|
||||
|
||||
match results {
|
||||
Some(results) => Ok(Json(results.iter().map(ActionDTO::from).collect())),
|
||||
None => Err(Status::InternalServerError),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
pub mod action;
|
||||
@@ -0,0 +1,23 @@
|
||||
use crate::models::action::Action;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct ActionDTO {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub source: String,
|
||||
pub created_at: String,
|
||||
pub updated_at: String,
|
||||
}
|
||||
|
||||
impl ActionDTO {
|
||||
pub fn from(action: &Action) -> Self {
|
||||
Self {
|
||||
id: action.id.to_string(),
|
||||
name: action.name.clone(),
|
||||
source: action.source.clone(),
|
||||
created_at: action.created_at.to_string(),
|
||||
updated_at: action.updated_at.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
pub mod action;
|
||||
+16
-10
@@ -1,15 +1,18 @@
|
||||
extern crate fern;
|
||||
extern crate log;
|
||||
#[macro_use]
|
||||
extern crate rocket;
|
||||
mod api;
|
||||
mod auth;
|
||||
mod dtos;
|
||||
mod models;
|
||||
mod schema;
|
||||
|
||||
#[macro_use]
|
||||
extern crate rocket;
|
||||
extern crate fern;
|
||||
extern crate log;
|
||||
|
||||
use crate::api::action;
|
||||
use auth::CORS;
|
||||
use chrono::Local;
|
||||
use diesel::{Connection, PgConnection};
|
||||
use diesel::PgConnection;
|
||||
use diesel::r2d2::{ConnectionManager, Pool};
|
||||
use models::AppState;
|
||||
use rocket::State;
|
||||
use std::env;
|
||||
@@ -42,7 +45,7 @@ fn setup_logging(log_file: Option<String>) -> Result<(), fern::InitError> {
|
||||
}
|
||||
|
||||
#[get("/")]
|
||||
fn index(state: &State<AppState>) -> String {
|
||||
fn index(_state: &State<AppState>) -> String {
|
||||
"Hello world!".to_string()
|
||||
}
|
||||
|
||||
@@ -54,12 +57,15 @@ fn rocket() -> _ {
|
||||
setup_logging(log_file).expect("Failed to setup logging");
|
||||
|
||||
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
||||
let db = PgConnection::establish(&database_url)
|
||||
.expect(&format!("Error connecting to {}", database_url));
|
||||
let manager = ConnectionManager::<PgConnection>::new(database_url);
|
||||
let pool = Pool::builder()
|
||||
.build(manager)
|
||||
.expect("Failed to create pool");
|
||||
|
||||
let app_data = AppState::new(db);
|
||||
let app_data = AppState::new(pool);
|
||||
rocket::build()
|
||||
.manage(app_data)
|
||||
.attach(CORS {})
|
||||
.mount("/", routes![index])
|
||||
.mount("/action", routes![action::index])
|
||||
}
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
use diesel::r2d2::{ConnectionManager, Pool};
|
||||
use diesel::PgConnection;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
mod action;
|
||||
pub const DEFAULT_QUERY_LIMIT: i64 = 100;
|
||||
|
||||
pub mod action;
|
||||
|
||||
pub struct AppState {
|
||||
pub db: Arc<Mutex<PgConnection>>,
|
||||
pub db: Pool<ConnectionManager<PgConnection>>,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
pub fn new(db: PgConnection) -> Self {
|
||||
Self {
|
||||
db: Arc::new(Mutex::new(db)),
|
||||
}
|
||||
pub fn new(pool: Pool<ConnectionManager<PgConnection>>) -> Self {
|
||||
Self { db: pool }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user