e4d6ac74d36e42e87ebc0658b40dcbe0b55ba141
[litoprism.git] / src / main.rs
1 use std::io::Cursor;
2 use std::fmt::Display;
3 use std::fmt;
4 use actix_web::{get, web, App, HttpServer, HttpResponse, Responder, ResponseError, Result};
5 use actix_web::body::BoxBody;
6 use actix_web::http::StatusCode;
7 use image::{ImageBuffer, ColorType, RgbImage, write_buffer_with_format};
8 use image::ImageOutputFormat::Png;
9
10 #[derive(Debug)]
11 struct ImageError(image::ImageError);
12
13 impl Display for ImageError {
14 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15 write!(f, "{}", &self.0)
16 }
17 }
18
19 impl ResponseError for ImageError {
20 fn status_code(&self) -> StatusCode {
21 StatusCode::INTERNAL_SERVER_ERROR
22 }
23
24 fn error_response(&self) -> HttpResponse<BoxBody> {
25 HttpResponse::InternalServerError().finish()
26 }
27 }
28
29 fn to_imageresult<T> (result: Result<T, image::ImageError>) -> Result<T, ImageError> {
30 match result {
31 Ok(v) => Ok(v),
32 Err(e) => Err(ImageError(e))
33 }
34 }
35
36 #[get("/hello/{name}")]
37 async fn greet(name: web::Path<String>) -> impl Responder {
38 format!("Hello {name}!")
39 }
40
41 #[get("/gen/{data}")]
42 async fn img_gen(data: web::Path<String>) -> Result<impl Responder> {
43 let mut cursor = Cursor::new(Vec::new());
44 let img: RgbImage = ImageBuffer::new(512, 512);
45 to_imageresult(write_buffer_with_format(&mut cursor, &img, 512, 512, ColorType::Rgb8, Png))?;
46 Ok(cursor.into_inner().customize().insert_header(("content-type", "image/png")))
47 }
48
49 #[actix_web::main] // or #[tokio::main]
50 async fn main() -> std::io::Result<()> {
51 HttpServer::new(|| {
52 App::new().service(greet).service(img_gen)
53 })
54 .bind(("127.0.0.1", 8080))?
55 .run()
56 .await
57 }