feat(server):提供静态netfilter文件的下载。
This commit is contained in:
		
							
								
								
									
										47
									
								
								license_server/src/controllers/downloads.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								license_server/src/controllers/downloads.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,47 @@ | ||||
| use std::path::PathBuf; | ||||
|  | ||||
| use axum::{ | ||||
|     body::Body, | ||||
|     http::{header, StatusCode}, | ||||
|     response::{IntoResponse, Response}, | ||||
|     routing, Router, | ||||
| }; | ||||
| use tokio::fs::File; | ||||
| use tokio_util::io::ReaderStream; | ||||
|  | ||||
| pub struct DownloadHandler { | ||||
|     routes: Router, | ||||
| } | ||||
|  | ||||
| impl Into<Router> for DownloadHandler { | ||||
|     fn into(self) -> Router { | ||||
|         self.routes | ||||
|     } | ||||
| } | ||||
|  | ||||
| impl DownloadHandler { | ||||
|     pub fn init() -> Self { | ||||
|         let routes = Router::new().route("/package", routing::get(download_netfiler)); | ||||
|  | ||||
|         Self { routes } | ||||
|     } | ||||
| } | ||||
|  | ||||
| async fn download_netfiler() -> Result<impl IntoResponse, StatusCode> { | ||||
|     let netfilter_file_path = PathBuf::from("./netfilter.zip"); | ||||
|     if !netfilter_file_path.exists() { | ||||
|         return Err(StatusCode::NOT_FOUND); | ||||
|     } | ||||
|     let file = File::open(netfilter_file_path).await.unwrap(); | ||||
|     let stream = ReaderStream::new(file); | ||||
|     let body = Body::from_stream(stream); | ||||
|     let response = Response::new(body); | ||||
|     Ok(( | ||||
|         StatusCode::OK, | ||||
|         [( | ||||
|             header::CONTENT_DISPOSITION, | ||||
|             "attachment; filename=netfilter.zip", | ||||
|         )], | ||||
|         response, | ||||
|     )) | ||||
| } | ||||
| @@ -1,3 +1,4 @@ | ||||
| mod downloads; | ||||
| mod license; | ||||
| mod products; | ||||
|  | ||||
| @@ -8,6 +9,7 @@ pub fn controllers() -> Box<dyn Iterator<Item = Box<Router>>> { | ||||
|     let controllers: Vec<Box<Router>> = vec![ | ||||
|         Box::new(products::ProductsController::init().into()), | ||||
|         Box::new(license::LicenseController::init().into()), | ||||
|         Box::new(downloads::DownloadHandler::init().into()), | ||||
|     ]; | ||||
|  | ||||
|     Box::from(controllers.into_iter()) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user