Created
March 25, 2025 06:13
-
-
Save darko-mesaros/b62f68917c99d6076b8f0bd88f8b9dad to your computer and use it in GitHub Desktop.
Rust + Postgres + Redis makefile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Project configuration | |
PROJECT_NAME = rusty_databases | |
# Docker configuration | |
DOCKER = docker | |
POSTGRES_CONTAINER = $(PROJECT_NAME)_postgres | |
REDIS_CONTAINER = $(PROJECT_NAME)_redis | |
# Generate a random password (16 characters) | |
PG_PASSWORD := $(shell openssl rand -base64 12 | tr -d '\n') | |
# Default target - shows help | |
default: | |
@echo "Rusty 🦀 Databases go brrrr:" | |
@echo "---------------------------------" | |
@echo "Available commands:" | |
@echo " make setup - Set up Docker containers (Redis & PostgreSQL)" | |
@echo " make build - Build the Rust application" | |
@echo " make run - Run the Rust application with dependencies" | |
@echo " make stop - Stop all Docker containers" | |
@echo " make clean - Stop containers and clean build artifacts" | |
# Setup Docker containers | |
setup: | |
@echo "Starting PostgreSQL container with random password..." | |
$(DOCKER) run --name $(POSTGRES_CONTAINER) -e POSTGRES_PASSWORD=$(PG_PASSWORD) -p 5432:5432 -d postgres:14 | |
@echo "Starting Redis container..." | |
$(DOCKER) run --name $(REDIS_CONTAINER) -p 6379:6379 -d redis:alpine | |
@echo "Setting environment variables..." | |
@echo "export DATABASE_URL=postgres://postgres:$(PG_PASSWORD)@localhost:5432/postgres" > .env | |
@echo "export REDIS_URL=redis://localhost:6379" >> .env | |
@echo "Setup complete! Docker containers are running." | |
@echo "Environment variables have been written to .env" | |
@echo "Generated PostgreSQL password: $(PG_PASSWORD)" | |
@echo "(Password is also saved in the .env file)" | |
# Build the Rust application | |
build: | |
cargo build | |
# Run with environment variables from .env | |
run: | |
@if [ ! -f .env ]; then \ | |
echo "Environment file not found. Run 'make setup' first."; \ | |
exit 1; \ | |
fi | |
@echo "Loading environment variables and running application..." | |
@source .env && cargo run | |
# Stop Docker containers | |
stop: | |
@echo "Stopping Docker containers..." | |
-$(DOCKER) stop $(POSTGRES_CONTAINER) $(REDIS_CONTAINER) | |
# Clean everything | |
clean: stop | |
@echo "Removing Docker containers..." | |
-$(DOCKER) rm $(POSTGRES_CONTAINER) $(REDIS_CONTAINER) | |
@echo "Cleaning Rust build artifacts..." | |
cargo clean | |
@echo "Clean complete!" | |
.PHONY: default setup build run stop clean |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment