11use crate :: config:: { AdminConfig , get_host_path_from_domain} ;
22use crate :: domain_storage:: DomainStorage ;
33use delay_timer:: prelude:: * ;
4+ use salvo:: oapi:: OpenApi ;
45use salvo:: prelude:: * ;
56use serde:: { Deserialize , Serialize } ;
67use std:: collections:: HashMap ;
@@ -55,7 +56,7 @@ impl AdminServer {
5556 }
5657
5758 fn routes ( & self ) -> Router {
58- Router :: with_hoop ( BearerValidator {
59+ let api_router = Router :: with_hoop ( BearerValidator {
5960 token : format ! ( "Bearer {}" , self . conf. token) ,
6061 } )
6162 . hoop (
@@ -70,7 +71,15 @@ impl AdminServer {
7071 . push ( Router :: with_path ( "file/upload" ) . post ( service:: update_file) )
7172 . push ( Router :: with_path ( "files/metadata" ) . get ( service:: get_files_metadata) )
7273 . push ( Router :: with_path ( "files/delete" ) . post ( service:: remove_domain_version) )
73- . push ( Router :: with_path ( "files/revoke_version" ) . post ( service:: revoke_version) )
74+ . push ( Router :: with_path ( "files/revoke_version" ) . post ( service:: revoke_version) ) ;
75+
76+ let doc = OpenApi :: new ( "SPA Server Admin API" , "1.0.0" )
77+ . merge_router ( & api_router) ;
78+
79+ Router :: new ( )
80+ . push ( doc. into_router ( "/api-doc/openapi.json" ) )
81+ . push ( SwaggerUi :: new ( "/api-doc/openapi.json" ) . into_router ( "swagger-ui" ) )
82+ . push ( api_router)
7483 }
7584
7685 pub async fn run ( & self ) -> anyhow:: Result < ( ) > {
@@ -116,7 +125,6 @@ pub mod service {
116125 GetDomainOption , GetDomainPositionFormat , GetDomainPositionOption ,
117126 UpdateUploadingStatusOption , UploadFileOption ,
118127 } ;
119- use entity:: storage:: DomainInfo ;
120128 use salvo:: prelude:: * ;
121129 use std:: collections:: HashMap ;
122130 use std:: sync:: Arc ;
@@ -129,6 +137,7 @@ pub mod service {
129137 #[ endpoint(
130138 responses(
131139 ( status_code = 200 , description = "Domain info retrieved successfully" , body = Vec <entity:: storage:: DomainInfo >) ,
140+ ( status_code = 400 , description = "Bad request" ) ,
132141 ( status_code = 401 , description = "Unauthorized" )
133142 )
134143 ) ]
@@ -143,7 +152,7 @@ pub mod service {
143152 if let Some ( data) = domain_info. iter ( ) . find ( |x| x. domain == domain) {
144153 res. render ( Json ( & [ data] ) ) ;
145154 } else {
146- let data: Vec < & DomainInfo > = Vec :: new ( ) ;
155+ let data: Vec < & entity :: storage :: DomainInfo > = Vec :: new ( ) ;
147156 res. render ( Json ( data) ) ;
148157 }
149158 return ;
@@ -161,10 +170,12 @@ pub mod service {
161170 /// Changes the current active version for a domain.
162171 #[ endpoint(
163172 responses(
164- ( status_code = 200 , description = "Version updated successfully" ) ,
173+ ( status_code = 200 , description = "Version updated successfully" , body = String ) ,
165174 ( status_code = 404 , description = "Domain not found" ) ,
175+ ( status_code = 400 , description = "Bad request" ) ,
166176 ( status_code = 401 , description = "Unauthorized" )
167- )
177+ ) ,
178+ request_body = DomainWithOptVersionOption
168179 ) ]
169180 pub async fn update_domain_version ( req : & mut Request , res : & mut Response , depot : & mut Depot ) {
170181 let storage = depot. obtain :: < Arc < DomainStorage > > ( ) . unwrap ( ) ;
@@ -194,6 +205,7 @@ pub mod service {
194205 /// Get upload position
195206 ///
196207 /// Returns the file system path for uploading files to a domain.
208+ /// Response can be either JSON (with format=json query param) or plain text path.
197209 #[ endpoint(
198210 responses(
199211 ( status_code = 200 , description = "Upload position retrieved" ) ,
@@ -237,7 +249,8 @@ pub mod service {
237249 ( status_code = 200 , description = "Upload status updated" ) ,
238250 ( status_code = 400 , description = "Bad request" ) ,
239251 ( status_code = 401 , description = "Unauthorized" )
240- )
252+ ) ,
253+ request_body = UpdateUploadingStatusOption
241254 ) ]
242255 pub async fn change_upload_status ( req : & mut Request , res : & mut Response , depot : & mut Depot ) {
243256 let storage = depot. obtain :: < Arc < DomainStorage > > ( ) . unwrap ( ) ;
@@ -263,6 +276,7 @@ pub mod service {
263276 /// Upload file
264277 ///
265278 /// Uploads a single file to a domain version. Uses multipart/form-data.
279+ /// Requires query parameters: domain, version, path.
266280 #[ endpoint(
267281 responses(
268282 ( status_code = 200 , description = "File uploaded successfully" ) ,
@@ -317,7 +331,7 @@ pub mod service {
317331 /// Returns metadata for all files in a specific domain version.
318332 #[ endpoint(
319333 responses(
320- ( status_code = 200 , description = "Files metadata retrieved" ) ,
334+ ( status_code = 200 , description = "Files metadata retrieved" , body = Vec <entity :: storage :: ShortMetaData > ) ,
321335 ( status_code = 400 , description = "Bad request" ) ,
322336 ( status_code = 401 , description = "Unauthorized" )
323337 )
@@ -386,7 +400,8 @@ pub mod service {
386400 ( status_code = 200 , description = "Domain versions removed" ) ,
387401 ( status_code = 400 , description = "Bad request" ) ,
388402 ( status_code = 401 , description = "Unauthorized" )
389- )
403+ ) ,
404+ request_body = DeleteDomainVersionOption
390405 ) ]
391406 pub async fn remove_domain_version ( req : & mut Request , res : & mut Response , depot : & mut Depot ) {
392407 if let Ok ( query) = req. parse_json :: < DeleteDomainVersionOption > ( ) . await {
@@ -407,7 +422,8 @@ pub mod service {
407422 ( status_code = 404 , description = "Domain or version not found" ) ,
408423 ( status_code = 400 , description = "Bad request" ) ,
409424 ( status_code = 401 , description = "Unauthorized" )
410- )
425+ ) ,
426+ request_body = DomainWithVersionOption
411427 ) ]
412428 pub async fn revoke_version ( req : & mut Request , res : & mut Response , depot : & mut Depot ) {
413429 let domain_storage = depot. obtain :: < Arc < DomainStorage > > ( ) . unwrap ( ) ;
0 commit comments