|
| 1 | +# Service Wait |
| 2 | + |
| 3 | +A lightweight CLI tool that waits for HTTP, MongoDB, or PostgreSQL endpoints to become available before proceeding. Useful as an init container or entrypoint wrapper in containerised deployments. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **HTTP** – probe one or multiple HTTP endpoints |
| 8 | +- **PostgreSQL** – wait for a Postgres database to accept connections |
| 9 | +- **MongoDB** – wait for a Mongo instance to respond to pings |
| 10 | +- Configurable timeout and polling interval |
| 11 | +- Configuration via CLI flags or environment variables |
| 12 | + |
| 13 | +## Install |
| 14 | + |
| 15 | +### Prerequisites |
| 16 | + |
| 17 | +- [mise](https://mise.jdx.dev) |
| 18 | + |
| 19 | +```bash |
| 20 | +mise trust |
| 21 | +mise install |
| 22 | +``` |
| 23 | + |
| 24 | +### Build from source |
| 25 | + |
| 26 | +```bash |
| 27 | +make build # outputs bin/service-wait |
| 28 | +make install # installs to ~/.local/bin |
| 29 | +``` |
| 30 | + |
| 31 | +### Docker |
| 32 | + |
| 33 | +```bash |
| 34 | +docker build -t service-wait . |
| 35 | +``` |
| 36 | + |
| 37 | +### Use in a Docker image (multi-stage) |
| 38 | + |
| 39 | +Copy the binary into your own image using a multi-stage build: |
| 40 | + |
| 41 | +```dockerfile |
| 42 | +FROM ghcr.io/mafigit/service-wait:v0.0.1@sha256:f76661dd68c5bfef49712400d8cd64d46e4f8ab932f644d628b4cee0294b1e28 AS service-wait |
| 43 | + |
| 44 | +FROM alpine:3 |
| 45 | + |
| 46 | +COPY --from=service-wait /ko-app/service-wait /usr/local/bin/service-wait |
| 47 | + |
| 48 | +ENTRYPOINT ["service-wait"] |
| 49 | +``` |
| 50 | + |
| 51 | +## Usage |
| 52 | + |
| 53 | +```bash |
| 54 | +service-wait [flags] |
| 55 | +``` |
| 56 | + |
| 57 | +### Flags |
| 58 | + |
| 59 | +| Flag | Short | EnvVar | Default | Description | |
| 60 | +|------|-------|--------|---------|-------------| |
| 61 | +| `--url` | `-u` | `SERVICE_WAIT_URL` | | HTTP endpoint to probe | |
| 62 | +| `--urls` | `-U` | `SERVICE_WAIT_URLS` | | Multiple HTTP endpoints (comma-separated) | |
| 63 | +| `--timeout` | `-t` | `SERVICE_WAIT_TIMEOUT` | `30s` | Max time to wait | |
| 64 | +| `--interval` | `-i` | `SERVICE_WAIT_INTERVAL` | `30s` | Polling interval | |
| 65 | +| `--psql-host` | `-H` | `SERVICE_WAIT_PSQL_HOST` / `PGHOST` | | PostgreSQL host | |
| 66 | +| `--psql-port` | `-p` | `SERVICE_WAIT_PSQL_PORT` / `PGPORT` | `5432` | PostgreSQL port | |
| 67 | +| `--psql-user` | | `SERVICE_WAIT_PSQL_USER` / `PGUSER` | | PostgreSQL user | |
| 68 | +| `--psql-password` | `-P` | `SERVICE_WAIT_PSQL_PASSWORD` / `PGPASSWORD` | | PostgreSQL password | |
| 69 | +| `--psql-database` | `-d` | `SERVICE_WAIT_PSQL_DATABASE` / `PGDATABASE` | | PostgreSQL database | |
| 70 | +| `--psql-sslmode` | `-s` | `SERVICE_WAIT_PSQL_SSLMODE` / `PGSSLMODE` | `disable` | PostgreSQL SSL mode | |
| 71 | +| `--psql-dsn` | | `SERVICE_WAIT_PSQL_DSN` | | PostgreSQL DSN (overrides individual flags) | |
| 72 | +| `--mongo-host` | | `SERVICE_WAIT_MONGO_HOST` | | MongoDB host | |
| 73 | +| `--mongo-port` | | `SERVICE_WAIT_MONGO_PORT` | `27017` | MongoDB port | |
| 74 | +| `--mongo-user` | | `SERVICE_WAIT_MONGO_USER` | | MongoDB user | |
| 75 | +| `--mongo-password` | | `SERVICE_WAIT_MONGO_PASSWORD` | | MongoDB password | |
| 76 | +| `--mongo-database` | | `SERVICE_WAIT_MONGO_DATABASE` | | MongoDB database | |
| 77 | +| `--mongo-auth-source` | | `SERVICE_WAIT_MONGO_AUTH_SOURCE` | | MongoDB auth source | |
| 78 | +| `--verbose` | `-V` | `SERVICE_WAIT_DEBUG` | `false` | Enable debug logging | |
| 79 | + |
| 80 | +### Examples |
| 81 | + |
| 82 | +Wait for an HTTP endpoint: |
| 83 | + |
| 84 | +```bash |
| 85 | +service-wait --url http://localhost:8080/health --timeout 60s --interval 2s |
| 86 | +``` |
| 87 | + |
| 88 | +Wait for PostgreSQL: |
| 89 | + |
| 90 | +```bash |
| 91 | +service-wait --psql-host localhost --psql-user postgres --psql-password secret --psql-database mydb |
| 92 | +``` |
| 93 | + |
| 94 | +Wait for MongoDB: |
| 95 | + |
| 96 | +```bash |
| 97 | +service-wait --mongo-host localhost --mongo-user admin --mongo-password secret --mongo-database mydb |
| 98 | +``` |
| 99 | + |
| 100 | +Using environment variables: |
| 101 | + |
| 102 | +```bash |
| 103 | +export SERVICE_WAIT_URL=http://localhost:8080/health |
| 104 | +export SERVICE_WAIT_TIMEOUT=60s |
| 105 | +service-wait |
| 106 | +``` |
| 107 | + |
| 108 | +## Development |
| 109 | + |
| 110 | +```bash |
| 111 | +make build # compile |
| 112 | +make test # run tests (requires Docker for testcontainers) |
| 113 | +make fmt # format code |
| 114 | +make vet # run go vet |
| 115 | +make tidy # tidy go modules |
| 116 | +``` |
| 117 | + |
| 118 | +## License |
| 119 | + |
| 120 | +[MIT](LICENSE) |
0 commit comments