Skip to content

Commit cd52182

Browse files
committed
update: compatibility interface
1 parent 58ae5cc commit cd52182

File tree

2 files changed

+140
-11
lines changed

2 files changed

+140
-11
lines changed

src/endpoint.rs

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,53 @@ use axum::{Json, extract::State};
66
use serde::{Deserialize, Serialize};
77
use std::{sync::Arc, time::SystemTime};
88

9+
const VERSION: &str = "v3.0.0";
10+
11+
// Version endpoint
12+
#[derive(Debug, Serialize)]
13+
pub struct VersionResponse {
14+
version: String,
15+
}
16+
17+
pub async fn version() -> Json<VersionResponse> {
18+
Json(VersionResponse {
19+
version: VERSION.to_string(),
20+
})
21+
}
22+
23+
// Languages endpoint
24+
#[derive(Debug, Serialize)]
25+
pub struct LanguagesResponse {
26+
languages: Vec<String>,
27+
}
28+
29+
pub async fn languages(State(state): State<Arc<AppState>>) -> Json<LanguagesResponse> {
30+
let mut language_codes = std::collections::HashSet::new();
31+
32+
for (from_lang, to_lang) in &state.models {
33+
if let Some(from_code) = from_lang.to_639_1() {
34+
language_codes.insert(from_code.to_string());
35+
}
36+
if let Some(to_code) = to_lang.to_639_1() {
37+
language_codes.insert(to_code.to_string());
38+
}
39+
}
40+
41+
let mut languages: Vec<String> = language_codes.into_iter().collect();
42+
languages.sort();
43+
44+
Json(LanguagesResponse { languages })
45+
}
46+
47+
// Heartbeat endpoints
48+
pub async fn heartbeat() -> &'static str {
49+
"Ready"
50+
}
51+
52+
pub async fn lbheartbeat() -> &'static str {
53+
"Ready"
54+
}
55+
956
#[derive(Debug, Deserialize)]
1057
pub struct DetectLanguageRequest {
1158
text: String,
@@ -24,6 +71,7 @@ pub async fn detect_language(
2471
}))
2572
}
2673

74+
// Modified translate endpoint
2775
#[derive(Debug, Deserialize)]
2876
pub struct TranslationRequest {
2977
text: String,
@@ -36,6 +84,7 @@ pub struct TranslationResponse {
3684
text: String,
3785
from: String,
3886
to: String,
87+
result: String,
3988
}
4089

4190
pub async fn translate(
@@ -46,12 +95,81 @@ pub async fn translate(
4695
perform_translation(&state, &request.text, request.from, &request.to).await?;
4796

4897
Ok(Json(TranslationResponse {
49-
text,
98+
text: text.clone(),
5099
from: from_lang,
51100
to: to_lang,
101+
result: text,
102+
}))
103+
}
104+
105+
// Batch translate endpoint
106+
#[derive(Debug, Deserialize)]
107+
pub struct BatchTranslationRequest {
108+
texts: Vec<String>,
109+
from: Option<String>,
110+
to: String,
111+
}
112+
113+
#[derive(Debug, Serialize)]
114+
pub struct BatchTranslationResponse {
115+
results: Vec<String>,
116+
}
117+
118+
pub async fn translate_batch(
119+
State(state): State<Arc<AppState>>,
120+
Json(request): Json<BatchTranslationRequest>,
121+
) -> Result<Json<BatchTranslationResponse>, AppError> {
122+
let mut results = Vec::with_capacity(request.texts.len());
123+
124+
for text in request.texts {
125+
let (translated_text, _from_lang, _to_lang) =
126+
perform_translation(&state, &text, request.from.clone(), &request.to).await?;
127+
results.push(translated_text);
128+
}
129+
130+
Ok(Json(BatchTranslationResponse { results }))
131+
}
132+
133+
// Google Translate compatible endpoint
134+
#[derive(Debug, Deserialize)]
135+
pub struct GoogleTranslateRequest {
136+
q: String,
137+
source: Option<String>,
138+
target: String,
139+
format: Option<String>,
140+
}
141+
142+
#[derive(Debug, Serialize)]
143+
pub struct GoogleTranslateItem {
144+
#[serde(rename = "translatedText")]
145+
translated_text: String,
146+
}
147+
148+
#[derive(Debug, Serialize)]
149+
pub struct GoogleTranslateData {
150+
translations: Vec<GoogleTranslateItem>,
151+
}
152+
153+
#[derive(Debug, Serialize)]
154+
pub struct GoogleTranslateResponse {
155+
data: GoogleTranslateData,
156+
}
157+
158+
pub async fn translate_google(
159+
State(state): State<Arc<AppState>>,
160+
Json(request): Json<GoogleTranslateRequest>,
161+
) -> Result<Json<GoogleTranslateResponse>, AppError> {
162+
let (translated_text, _from_lang, _to_lang) =
163+
perform_translation(&state, &request.q, request.source, &request.target).await?;
164+
165+
Ok(Json(GoogleTranslateResponse {
166+
data: GoogleTranslateData {
167+
translations: vec![GoogleTranslateItem { translated_text }],
168+
},
52169
}))
53170
}
54171

172+
// Original endpoint handlers for backward compatibility
55173
#[derive(Debug, Deserialize)]
56174
pub struct KissTranslationRequest {
57175
text: String,

src/main.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,22 +182,33 @@ async fn main() -> anyhow::Result<()> {
182182
.allow_methods(Any)
183183
.allow_headers(Any);
184184

185-
let app = Router::new()
185+
// Routes that require authentication
186+
let authenticated_routes = Router::new()
186187
.route("/translate", post(endpoint::translate))
188+
.route("/translate/batch", post(endpoint::translate_batch))
189+
.route("/languages", get(endpoint::languages))
190+
.route("/language/translate/v2", post(endpoint::translate_google))
187191
.route("/kiss", post(endpoint::translate_kiss))
188192
.route("/imme", post(endpoint::translate_immersive))
189193
.route("/hcfy", post(endpoint::translate_hcfy))
190194
.route("/deeplx", post(endpoint::translate_deeplx))
191195
.route("/detect", post(endpoint::detect_language))
192-
.route(
193-
"/health",
194-
get(async || {
195-
Json(serde_json::json!({
196-
"status": "ok",
197-
}))
198-
}),
199-
)
200-
.route_layer(middleware::from_fn(auth_middleware))
196+
.route_layer(middleware::from_fn(auth_middleware));
197+
198+
// Routes that don't require authentication
199+
let public_routes = Router::new()
200+
.route("/version", get(endpoint::version))
201+
.route("/health", get(async || {
202+
Json(serde_json::json!({
203+
"status": "ok",
204+
}))
205+
}))
206+
.route("/__heartbeat__", get(endpoint::heartbeat))
207+
.route("/__lbheartbeat__", get(endpoint::lbheartbeat));
208+
209+
let app = Router::new()
210+
.merge(authenticated_routes)
211+
.merge(public_routes)
201212
.layer(TraceLayer::new_for_http())
202213
.layer(cors)
203214
.with_state(app_state);

0 commit comments

Comments
 (0)