mirror of
				https://github.com/ivanch/tcc.git
				synced 2025-10-31 17:37:36 +00:00 
			
		
		
		
	add actix
This commit is contained in:
		
							
								
								
									
										18
									
								
								ActixAPI/Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										18
									
								
								ActixAPI/Cargo.lock
									
									
									
										generated
									
									
									
								
							| @@ -9,6 +9,7 @@ dependencies = [ | |||||||
|  "actix-files", |  "actix-files", | ||||||
|  "actix-web", |  "actix-web", | ||||||
|  "magick_rust", |  "magick_rust", | ||||||
|  |  "qstring", | ||||||
| ] | ] | ||||||
|  |  | ||||||
| [[package]] | [[package]] | ||||||
| @@ -299,9 +300,9 @@ checksum = "9ba43ea6f343b788c8764558649e08df62f86c6ef251fdaeb1ffd010a9ae50a2" | |||||||
|  |  | ||||||
| [[package]] | [[package]] | ||||||
| name = "bindgen" | name = "bindgen" | ||||||
| version = "0.66.1" | version = "0.68.1" | ||||||
| source = "registry+https://github.com/rust-lang/crates.io-index" | source = "registry+https://github.com/rust-lang/crates.io-index" | ||||||
| checksum = "f2b84e06fc203107bfbad243f4aba2af864eb7db3b1cf46ea0a023b0b433d2a7" | checksum = "726e4313eb6ec35d2730258ad4e15b547ee75d6afaa1361a922e78e59b7d8078" | ||||||
| dependencies = [ | dependencies = [ | ||||||
|  "bitflags 2.4.0", |  "bitflags 2.4.0", | ||||||
|  "cexpr", |  "cexpr", | ||||||
| @@ -785,9 +786,9 @@ checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" | |||||||
|  |  | ||||||
| [[package]] | [[package]] | ||||||
| name = "magick_rust" | name = "magick_rust" | ||||||
| version = "0.19.0" | version = "0.19.1" | ||||||
| source = "registry+https://github.com/rust-lang/crates.io-index" | source = "registry+https://github.com/rust-lang/crates.io-index" | ||||||
| checksum = "ce666df6ace809001c625ad383f4727657c7c7881df43b3b71486e8e9f07d017" | checksum = "c912b69250bcd5d024852a1a75c567d3b5d881871a55b741018741632a921bf8" | ||||||
| dependencies = [ | dependencies = [ | ||||||
|  "bindgen", |  "bindgen", | ||||||
|  "libc", |  "libc", | ||||||
| @@ -952,6 +953,15 @@ dependencies = [ | |||||||
|  "unicode-ident", |  "unicode-ident", | ||||||
| ] | ] | ||||||
|  |  | ||||||
|  | [[package]] | ||||||
|  | name = "qstring" | ||||||
|  | version = "0.7.2" | ||||||
|  | source = "registry+https://github.com/rust-lang/crates.io-index" | ||||||
|  | checksum = "d464fae65fff2680baf48019211ce37aaec0c78e9264c84a3e484717f965104e" | ||||||
|  | dependencies = [ | ||||||
|  |  "percent-encoding", | ||||||
|  | ] | ||||||
|  |  | ||||||
| [[package]] | [[package]] | ||||||
| name = "quote" | name = "quote" | ||||||
| version = "1.0.33" | version = "1.0.33" | ||||||
|   | |||||||
| @@ -8,4 +8,5 @@ edition = "2021" | |||||||
| [dependencies] | [dependencies] | ||||||
| actix-web = "4" | actix-web = "4" | ||||||
| actix-files = "0.6.2" | actix-files = "0.6.2" | ||||||
| magick_rust = "0.19.0" | magick_rust = "0.19.1" | ||||||
|  | qstring = "0.7.2" | ||||||
|   | |||||||
| @@ -16,10 +16,19 @@ RUN cargo build --release | |||||||
|  |  | ||||||
| FROM debian:bullseye-slim | FROM debian:bullseye-slim | ||||||
|  |  | ||||||
|  | ENV MAGICK_VERSION 7.1 | ||||||
|  |  | ||||||
|  | RUN curl https://imagemagick.org/archive/ImageMagick.tar.gz | tar xz \ | ||||||
|  |     && cd ImageMagick-${MAGICK_VERSION}* \ | ||||||
|  |     && ./configure --with-magick-plus-plus=no --with-perl=no \ | ||||||
|  |     && make \ | ||||||
|  |     && make install \ | ||||||
|  |     && cd .. \ | ||||||
|  |     && rm -r ImageMagick-${MAGICK_VERSION}* | ||||||
|  |  | ||||||
| WORKDIR /app | WORKDIR /app | ||||||
|  |  | ||||||
| COPY --from=build-env /app/target/release . | COPY --from=build-env /app/target/release . | ||||||
|  |  | ||||||
| COPY --from=build-env /app/static ./static | COPY --from=build-env /app/static ./static | ||||||
|  |  | ||||||
| ENTRYPOINT ["./ActixAPI"] | ENTRYPOINT ["./ActixAPI"] | ||||||
|   | |||||||
| @@ -1,22 +1,13 @@ | |||||||
|  | use qstring::QString; | ||||||
| use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder, HttpRequest, Result}; | use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder, HttpRequest, Result}; | ||||||
| use actix_files::NamedFile; | use actix_files::NamedFile; | ||||||
| use magick_rust::{MagickWand, ToMagick}; | use magick_rust::MagickWand; | ||||||
| use magick_rust::{self, FilterType}; |  | ||||||
|  |  | ||||||
| #[get("/status/ok")] | #[get("/status/ok")] | ||||||
| async fn hello() -> impl Responder { | async fn hello() -> impl Responder { | ||||||
|     HttpResponse::Ok().body("{\"status\": 200}") |     HttpResponse::Ok().body("{\"status\": 200}") | ||||||
| } | } | ||||||
|  |  | ||||||
| #[post("/echo")] |  | ||||||
| async fn echo(req_body: String) -> impl Responder { |  | ||||||
|     HttpResponse::Ok().body(req_body) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| async fn manual_hello() -> impl Responder { |  | ||||||
|     HttpResponse::Ok().body("Hey there!") |  | ||||||
| } |  | ||||||
|  |  | ||||||
| async fn static_serve(req: HttpRequest) -> Result<NamedFile> { | async fn static_serve(req: HttpRequest) -> Result<NamedFile> { | ||||||
|     let path: &str = req.path(); |     let path: &str = req.path(); | ||||||
|     let real_path = &path[1..]; |     let real_path = &path[1..]; | ||||||
| @@ -24,19 +15,39 @@ async fn static_serve(req: HttpRequest) -> Result<NamedFile> { | |||||||
|     Ok(NamedFile::open(real_path)?) |     Ok(NamedFile::open(real_path)?) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | #[get("/image/load-small-image")] | ||||||
|  | async fn load_small_image() -> Result<NamedFile> { | ||||||
|  |     let real_path = "static/small-image.png"; | ||||||
|  |  | ||||||
|  |     Ok(NamedFile::open(real_path)?) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | #[get("/image/load-big-image")] | ||||||
|  | async fn load_big_image() -> Result<NamedFile> { | ||||||
|  |     let real_path = "static/big-image.png"; | ||||||
|  |  | ||||||
|  |     Ok(NamedFile::open(real_path)?) | ||||||
|  | } | ||||||
|  |  | ||||||
| #[post("/image/blur")] | #[post("/image/blur")] | ||||||
| async fn blur_image(image_data: web::Bytes) -> Result<HttpResponse> { | async fn blur_image(image_data: web::Bytes, req: HttpRequest) -> Result<HttpResponse> { | ||||||
|     // Load the image from the incoming bytes |     // Load the image from the incoming bytes | ||||||
|     let image = ToMagick::<magick_rust::MagickWand>::to_magick(image_data).unwrap(); |     let mut wand = MagickWand::new(); | ||||||
|  |     wand.read_image_blob(&image_data).unwrap(); | ||||||
|  |  | ||||||
|     // Apply a blur filter to the image |     let query_str = req.query_string(); | ||||||
|     let blurred_image = image.blur(FilterType::Gaussian, 0.0).unwrap(); |     let qs = QString::from(query_str); | ||||||
|  |     let radius = qs.get("radius").unwrap_or("5").parse::<f64>().unwrap_or(5.0); | ||||||
|  |  | ||||||
|     // Convert the blurred image back to bytes |     // Blur the image | ||||||
|     let blurred_image_bytes = blurred_image.to_bytes().unwrap(); |     wand.blur_image(radius, radius).unwrap(); | ||||||
|  |  | ||||||
|  |     // Convert the image back to bytes | ||||||
|  |     let blurred_image_bytes = wand.write_image_blob("png").unwrap(); | ||||||
|  |  | ||||||
|  |     // Return the blurred image bytes | ||||||
|     Ok(HttpResponse::Ok() |     Ok(HttpResponse::Ok() | ||||||
|         .content_type("image/jpeg") |         .content_type("image/png") | ||||||
|         .body(blurred_image_bytes)) |         .body(blurred_image_bytes)) | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -48,11 +59,11 @@ async fn main() -> std::io::Result<()> { | |||||||
|         App::new() |         App::new() | ||||||
|             .route("/static/{filename:.*}", web::get().to(static_serve)) |             .route("/static/{filename:.*}", web::get().to(static_serve)) | ||||||
|             .service(hello) |             .service(hello) | ||||||
|             .service(echo) |             .service(load_small_image) | ||||||
|  |             .service(load_big_image) | ||||||
|             .service(blur_image) |             .service(blur_image) | ||||||
|             .route("/hey", web::get().to(manual_hello)) |  | ||||||
|     }) |     }) | ||||||
|     .bind(("0.0.0.0", 9090))? |     .bind(("0.0.0.0", 5000))? | ||||||
|     .run() |     .run() | ||||||
|     .await |     .await | ||||||
| } | } | ||||||
| @@ -31,7 +31,7 @@ services: | |||||||
|     build: ./ActixAPI |     build: ./ActixAPI | ||||||
|     restart: always |     restart: always | ||||||
|     ports: |     ports: | ||||||
|       - "9083:9090" |       - "9083:5000" | ||||||
|     deploy: |     deploy: | ||||||
|       resources: |       resources: | ||||||
|         limits: |         limits: | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user