Convert to monorepo with utils library

This commit is contained in:
Elnu 2023-08-08 13:43:50 -07:00
parent befc0c0ee3
commit dad460ad37
8 changed files with 38 additions and 13 deletions

13
tatoeba/Cargo.toml Normal file
View file

@ -0,0 +1,13 @@
[package]
name = "tatoeba-api"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
utils = { path = "../utils" }
actix-web = "4.3.1"
derive_more = "0.99.17"
mime = "0.3.17"
reqwest = "0.11.18"

42
tatoeba/src/error.rs Normal file
View file

@ -0,0 +1,42 @@
use actix_web::{HttpResponse, ResponseError};
use derive_more::From;
use std::fmt::{self, Display};
pub type Result<T> = std::result::Result<T, Error>;
#[derive(From, Debug)]
pub enum Error {
TatoebaApi(reqwest::Error),
NotHuman { target: String },
}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl ResponseError for Error {
fn status_code(&self) -> reqwest::StatusCode {
use reqwest::StatusCode;
use Error::*;
match self {
// 503 Service Unavailable
TatoebaApi(error) => error.status().unwrap_or(StatusCode::SERVICE_UNAVAILABLE),
// 403 Forbidden
NotHuman { .. } => StatusCode::FORBIDDEN,
}
}
fn error_response(&self) -> HttpResponse<actix_web::body::BoxBody> {
HttpResponse::build(self.status_code()).body(match self {
Self::NotHuman { target } => format!(
"It looks like you're accessing the Tatoeba API proxy from a script!\n\
Tatoeba CORS restrictions do not apply outside of browsers, so please access the API directly:\n\
{target}\n\
If you feel this is mistake, please open an issue:\n\
https://codeberg.org/ElnuDev/tatoeba-api-rs"),
_ => self.to_string(),
})
}
}

28
tatoeba/src/main.rs Normal file
View file

@ -0,0 +1,28 @@
mod error;
pub use error::{Error, Result};
use utils::is_human;
use actix_web::{get, http::header, App, HttpRequest, HttpResponse, HttpServer, Responder};
pub const TATOEBA_API: &str = "https://tatoeba.org/en/api_v0/search";
#[get("/")]
async fn root(request: HttpRequest) -> Result<impl Responder> {
let target = format!("{TATOEBA_API}?{}", request.query_string());
if !is_human(&request) {
return Err(Error::NotHuman { target });
}
let resp = reqwest::get(target).await?;
Ok(HttpResponse::Ok()
.append_header(header::ContentType(mime::APPLICATION_JSON))
.body(resp.text().await?))
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new().service(root))
.bind(("127.0.0.1", 3001))?
.run()
.await
}