Browse Source

WIP: basic API

develop-api-fb
chodak166 4 months ago
parent
commit
d0e3299f67
  1. 32
      apps/app_api/src/api.rs
  2. 30
      apps/app_api/src/api/dictionary.rs
  3. 22
      apps/app_api/src/api/health.rs
  4. 26
      apps/app_api/src/api/major_pl.rs
  5. 6
      apps/app_api/src/container.rs

32
apps/app_api/src/api.rs

@ -1,5 +1,6 @@
use crate::state::AppState; use crate::state::AppState;
use axum::Router; use axum::{Json, Router, http::StatusCode, response::IntoResponse};
use serde::Serialize;
use std::sync::Arc; use std::sync::Arc;
pub mod dictionary; pub mod dictionary;
@ -12,3 +13,32 @@ pub fn routes() -> Router<Arc<AppState>> {
.nest("/api", dictionary::routes()) .nest("/api", dictionary::routes())
.nest("/api", major_pl::routes()) .nest("/api", major_pl::routes())
} }
// --- Error Response ---
#[derive(Debug, Serialize)]
pub struct ErrorResponse {
pub error: String,
}
impl IntoResponse for ErrorResponse {
fn into_response(self) -> axum::response::Response {
(StatusCode::INTERNAL_SERVER_ERROR, Json(self)).into_response()
}
}
impl From<anyhow::Error> for ErrorResponse {
fn from(err: anyhow::Error) -> Self {
Self {
error: err.to_string(),
}
}
}
impl From<applib::RepositoryError> for ErrorResponse {
fn from(err: applib::RepositoryError) -> Self {
Self {
error: err.to_string(),
}
}
}

30
apps/app_api/src/api/dictionary.rs

@ -1,7 +1,8 @@
use axum::{Json, Router, extract::State, http::StatusCode, response::IntoResponse, routing::get}; use axum::{Json, Router, extract::State, routing::get};
use serde::Serialize; use serde::Serialize;
use std::sync::Arc; use std::sync::Arc;
use super::ErrorResponse;
use crate::state::AppState; use crate::state::AppState;
// --- DTOs --- // --- DTOs ---
@ -17,33 +18,6 @@ pub struct DictListEntryResponse {
pub entry_count: u64, pub entry_count: u64,
} }
#[derive(Debug, Serialize)]
pub struct ErrorResponse {
pub error: String,
}
impl IntoResponse for ErrorResponse {
fn into_response(self) -> axum::response::Response {
(StatusCode::INTERNAL_SERVER_ERROR, Json(self)).into_response()
}
}
impl From<anyhow::Error> for ErrorResponse {
fn from(err: anyhow::Error) -> Self {
Self {
error: err.to_string(),
}
}
}
impl From<applib::RepositoryError> for ErrorResponse {
fn from(err: applib::RepositoryError) -> Self {
Self {
error: err.to_string(),
}
}
}
// --- Handlers --- // --- Handlers ---
pub async fn list_dicts_handler( pub async fn list_dicts_handler(

22
apps/app_api/src/api/health.rs

@ -1,8 +1,6 @@
use axum::{ use axum::{
Json, Router, Json, Router,
extract::State, extract::State,
http::StatusCode,
response::IntoResponse,
routing::{get, post}, routing::{get, post},
}; };
use chrono::Utc; use chrono::Utc;
@ -10,6 +8,7 @@ use serde::Serialize;
use serde_json::Value; use serde_json::Value;
use std::sync::Arc; use std::sync::Arc;
use super::ErrorResponse;
use crate::state::AppState; use crate::state::AppState;
// --- DTOs --- // --- DTOs ---
@ -26,25 +25,6 @@ pub struct VersionResponse {
pub version: String, pub version: String,
} }
#[derive(Debug, Serialize)]
pub struct ErrorResponse {
pub error: String,
}
impl IntoResponse for ErrorResponse {
fn into_response(self) -> axum::response::Response {
(StatusCode::INTERNAL_SERVER_ERROR, Json(self)).into_response()
}
}
impl<E: std::error::Error> From<E> for ErrorResponse {
fn from(err: E) -> Self {
Self {
error: err.to_string(),
}
}
}
// --- Handlers --- // --- Handlers ---
pub async fn echo_handler( pub async fn echo_handler(

26
apps/app_api/src/api/major_pl.rs

@ -1,8 +1,7 @@
use super::ErrorResponse;
use axum::{ use axum::{
Json, Router, Json, Router,
extract::{Path, Query, State}, extract::{Path, Query, State},
http::StatusCode,
response::IntoResponse,
routing::get, routing::get,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -16,9 +15,7 @@ pub struct EncodeQuery {
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct DecodeQuery { pub struct DecodeQuery {}
pub dict: Option<String>,
}
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub struct EncodeResponse { pub struct EncodeResponse {
@ -39,25 +36,6 @@ pub struct DecodeResponse {
pub result: String, pub result: String,
} }
#[derive(Debug, Serialize)]
pub struct ErrorResponse {
pub error: String,
}
impl IntoResponse for ErrorResponse {
fn into_response(self) -> axum::response::Response {
(StatusCode::INTERNAL_SERVER_ERROR, Json(self)).into_response()
}
}
impl From<anyhow::Error> for ErrorResponse {
fn from(err: anyhow::Error) -> Self {
Self {
error: err.to_string(),
}
}
}
pub async fn encode_handler( pub async fn encode_handler(
State(state): State<Arc<AppState>>, State(state): State<Arc<AppState>>,
Path(input): Path<String>, Path(input): Path<String>,

6
apps/app_api/src/container.rs

@ -1,6 +1,5 @@
use std::sync::Arc; use std::sync::Arc;
use applib::DictImporter;
use applib::DictRepository; use applib::DictRepository;
use applib::SqliteDictRepository; use applib::SqliteDictRepository;
use applib::SystemDecoder; use applib::SystemDecoder;
@ -15,11 +14,6 @@ impl Container {
Ok(Self) Ok(Self)
} }
pub async fn create_dict_importer(&self, dict_name: &str) -> anyhow::Result<DictImporter> {
let repo = self.create_dict_repo(dict_name).await?;
Ok(DictImporter::new(repo))
}
pub async fn create_dict_repo( pub async fn create_dict_repo(
&self, &self,
dict_name: &str, dict_name: &str,

Loading…
Cancel
Save