@@ -6,6 +6,53 @@ use axum::{Json, extract::State};
66use serde:: { Deserialize , Serialize } ;
77use 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 ) ]
1057pub 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 ) ]
2876pub 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
4190pub 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 ) ]
56174pub struct KissTranslationRequest {
57175 text : String ,
0 commit comments