2 releases
Uses new Rust 2024
| 0.2.1 | Dec 14, 2025 |
|---|---|
| 0.2.0 | Jul 26, 2025 |
#617 in Filesystem
92KB
966 lines
random-image-server
A simple http server that returns a random image from a pre-configured directory.
The server exposes the following endpoints:
GET /health: Returns a 200 OK response to indicate the server is running.GET /random: Returns a random image from the configured sources.GET /sequential: Returns the next image in sequence from the configured sources.
Features
- Random image serving: Returns a random image from among the configured sources.
- Sequential image serving: Enumerates images sequentially from the configured sources.
- In-memory caching: Caches images at startup for fast access.
- File system caching: Caches images on disk for reduced memory usage.
- if cached images are modified externally, the server will detect this and invalidate the entry in the cache.
- TODO: instead, the server should reload the image from the source and update the cache.
- if cached images are modified externally, the server will detect this and invalidate the entry in the cache.
- Can serve png, jpg, and webp images, as well as animated gifs.
- Supports both local file paths and URLs as image sources.
- Configurable via a
config.tomlfile. - Graceful shutdown on termination signals.
- Logging, with configurable log levels.
Configuration
The server can be configured using a config.toml file. The configuration file should be placed in the same directory as the binary.
The configuration file should have the following structure:
[server]
port = 8080 # The port the server will listen on
host = "0.0.0.0" # The host the server will bind to
log_level = "info" # The log level for the server, can be "error", "warn", "info", "debug", or "trace"
sources = [
"/path/to/image.jpg",
"/path/to/another/image.png",
"/path/to/image/directory",
"http://example.com/images"
]
[cache]
# Configuration for the cache backend
backend = "file_system" # The type of cache backend to use, can be "in_memory" or "file_system"
You can also override the configuration using environment variables. The environment variables should be prefixed with RANDOM_IMAGE_SERVER_, and the keys should be in uppercase with underscores instead of dots. For example, to set the port, you can use the environment variable RANDOM_IMAGE_SERVER_PORT.
Installation
follow instructions in the Release page for the latest release, which involves curling a script and piping it to sh, or install from crates.io:
cargo install random-image-server
Usage
As a Docker Container
I don't know how to publish the docker image to a registry without having to pay for it, so you will have to build the image yourself. So, you'll need to clone the repository and build the image yourself.
You can build and run the server as a docker container using the provided Dockerfile and docker-compose file.
- Make sure to update the
config.tomlfile with your desired configuration, and update thecompose.ymlfile to mount the configuration file and any image directories you want to serve. - Build the docker image with
docker compose build: - Run the docker container with
docker compose up:
This will start the server and expose it on port 8080. You can access the server at http://localhost:8080.
As a Systemd Service
After downloading the binary:
- put the binary in
/usr/local/bin/random-image-serveror create a symlink to it in/usr/local/bin/ - download the
random-image-server.servicefile from the repo and place it in/etc/systemd/system/ - place your
config.tomlfile in/etc/random-image-server/config.toml(or create a symlink), and edit it to your liking. - run the following commands to enable and start the service:
sudo systemctl enable random-image-server.service
sudo systemctl start random-image-server.service
You can check the status of the service with:
sudo systemctl status random-image-server.service
and view the logs with:
sudo journalctl -u random-image-server.service
Dependencies
~18–40MB
~552K SLoC