This guide covers various deployment strategies for the Rust URL Shortener.
git clone <repository-url>
cd rust-url-shortener
cargo install diesel_cli --no-default-features --features sqlite
cp .env.example .env
diesel migration run
cargo run
The server will be available at http://localhost:8080
docker build -t rust-url-shortener .
docker run -d \
-p 8080:8080 \
-e DATABASE_URL=rust_url_shortener.db \
-e BASE_URL=http://localhost:8080 \
--name url-shortener \
rust-url-shortener
docker logs -f url-shortener
docker-compose up -d
docker-compose logs -f
docker-compose down
To persist the SQLite database:
docker run -d \
-p 8080:8080 \
-v $(pwd)/data:/data \
-e DATABASE_URL=/data/rust_url_shortener.db \
-e BASE_URL=http://localhost:8080 \
--name url-shortener \
rust-url-shortener
pip install awsebcli
eb init -p docker rust-url-shortener
eb create production
eb deploy
aws ecr create-repository --repository-name rust-url-shortener
docker tag rust-url-shortener:latest <account-id>.dkr.ecr.<region>.amazonaws.com/rust-url-shortener:latest
docker push <account-id>.dkr.ecr.<region>.amazonaws.com/rust-url-shortener:latest
gcloud builds submit --tag gcr.io/<project-id>/rust-url-shortener
gcloud run deploy rust-url-shortener \
--image gcr.io/<project-id>/rust-url-shortener \
--platform managed \
--region us-central1 \
--allow-unauthenticated \
--set-env-vars DATABASE_URL=rust_url_shortener.db,BASE_URL=https://your-service-url.run.app
az acr create --resource-group myResourceGroup --name myregistry --sku Basic
az acr build --registry myregistry --image rust-url-shortener:latest .
az container create \
--resource-group myResourceGroup \
--name rust-url-shortener \
--image myregistry.azurecr.io/rust-url-shortener:latest \
--dns-name-label rust-url-shortener \
--ports 8080 \
--environment-variables DATABASE_URL=rust_url_shortener.db BASE_URL=http://your-domain.com
app.yaml:
```yaml
name: rust-url-shortener
services:
Procfile:
web: ./target/release/rust-url-shortener
heroku create rust-url-shortener
heroku buildpacks:set emk/rust
git push heroku main
Set these environment variables for production:
DATABASE_URL=<production-database-url>
BASE_URL=https://yourdomain.com
RUST_LOG=info
HOST=0.0.0.0
PORT=8080
For production, consider migrating to PostgreSQL:
Cargo.toml:
diesel = { version = "2.0.4", features = ["postgres", "r2d2", "chrono"] }
DATABASE_URL=postgres://user:password@localhost/rust_url_shortener
diesel migration run
Option 1: Reverse Proxy (Recommended)
Use Nginx or Caddy as a reverse proxy:
server {
listen 80;
server_name yourdomain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name yourdomain.com;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
location / {
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Option 2: Cloud Provider SSL
Most cloud providers offer built-in SSL/TLS termination.
RUST_LOG=info for productiontracing/health endpointMAX_POOL_SIZE=20
MIN_POOL_SIZE=5
# Cron job for daily backups
0 2 * * * sqlite3 /path/to/database.db ".backup '/path/to/backup/db-$(date +\%Y\%m\%d).db'"
# Automated pg_dump
0 2 * * * pg_dump rust_url_shortener > /backups/db-$(date +\%Y\%m\%d).sql
# Change PORT in .env or kill existing process
lsof -ti:8080 | xargs kill
Consider adding a health check endpoint:
async fn health_check() -> impl Responder {
HttpResponse::Ok().json(json!({
"status": "healthy",
"timestamp": chrono::Utc::now()
}))
}
For issues and questions: