init commit
This commit is contained in:
parent
4de155f33f
commit
af7c7d93d0
4 changed files with 1907 additions and 0 deletions
1846
Cargo.lock
generated
Normal file
1846
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
12
Cargo.toml
Normal file
12
Cargo.toml
Normal file
|
@ -0,0 +1,12 @@
|
|||
[package]
|
||||
name = "ipimg"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
ab_glyph = "0.2.26"
|
||||
axum = "0.7.5"
|
||||
axum-client-ip = "0.6.0"
|
||||
image = { version = "0.25.1", features = ["png"], default-features = false }
|
||||
imageproc = "0.25.0"
|
||||
tokio = { version = "1.37.0", features = ["rt-multi-thread"] }
|
BIN
Roboto-Regular.ttf
Normal file
BIN
Roboto-Regular.ttf
Normal file
Binary file not shown.
49
src/main.rs
Normal file
49
src/main.rs
Normal file
|
@ -0,0 +1,49 @@
|
|||
use std::{io::Cursor, net::SocketAddr};
|
||||
|
||||
use ab_glyph::FontRef;
|
||||
use axum::{extract::State, http::header, response::IntoResponse, routing::get, Router};
|
||||
use axum_client_ip::InsecureClientIp;
|
||||
use image::Rgba;
|
||||
use imageproc::drawing::{self, text_size};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let fontfile = include_bytes!("../Roboto-Regular.ttf");
|
||||
let font = FontRef::try_from_slice(fontfile).unwrap();
|
||||
|
||||
let app = Router::new().route("/ip.png", get(ip_png)).with_state(font);
|
||||
|
||||
let list = tokio::net::TcpListener::bind("0.0.0.0:8080").await.unwrap();
|
||||
axum::serve(
|
||||
list,
|
||||
app.into_make_service_with_connect_info::<SocketAddr>(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
async fn ip_png(ip: InsecureClientIp, State(font): State<FontRef<'static>>) -> impl IntoResponse {
|
||||
let out_buf = tokio::task::spawn_blocking(move || {
|
||||
let text = ip.0.to_string();
|
||||
let (x, _) = text_size(100.0, &font, &text);
|
||||
let mut img = image::DynamicImage::new(x, 100, image::ColorType::Rgba8);
|
||||
|
||||
let textcol = Rgba([127, 127, 127, 255]);
|
||||
drawing::draw_text_mut(&mut img, textcol, 0, 0, 100.0, &font, &text);
|
||||
|
||||
let mut buf = Vec::new();
|
||||
img.write_to(&mut Cursor::new(&mut buf), image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
|
||||
buf
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let headers = [
|
||||
(header::CONTENT_TYPE, "image/png"),
|
||||
(header::CACHE_CONTROL, "private, max-age=60"),
|
||||
];
|
||||
|
||||
(headers, out_buf)
|
||||
}
|
Loading…
Reference in a new issue