Skip to content

Commit

Permalink
chore: Add health check to Dice service in Docker Compose
Browse files Browse the repository at this point in the history
  • Loading branch information
vinitparekh17 committed Oct 17, 2024
1 parent 0076351 commit 5cecd74
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 28 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ $ curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/ins
```

Steps to clone and run:
```
```sh
$ git clone https://github.com/dicedb/playground-mono
$ cd playground-mono
$ go run main.go
```

### Using Docker

```sh
docker compose up -d
```
46 changes: 34 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ import (

// Config holds the application configuration
type Config struct {
DiceDBAddr string
ServerPort string
RequestLimitPerMin int64 // Field for the request limit
RequestWindowSec float64 // Field for the time window in float64
AllowedOrigins []string // Field for the allowed origins
IsTestEnv bool
DiceDB struct {
Addr string // Field for the Dice address
Username string // Field for the username
Password string // Field for the password
}
Server struct {
Port string // Field for the server port
IsTestEnv bool
RequestLimitPerMin int64 // Field for the request limit
RequestWindowSec float64 // Field for the time window in float64
AllowedOrigins []string // Field for the allowed origins
}
}

// LoadConfig loads the application configuration from environment variables or defaults
Expand All @@ -27,12 +33,28 @@ func LoadConfig() *Config {
}

return &Config{
DiceDBAddr: getEnv("DICEDB_ADDR", "localhost:7379"), // Default DiceDB address
ServerPort: getEnv("SERVER_PORT", ":8080"), // Default server port
RequestLimitPerMin: getEnvInt("REQUEST_LIMIT_PER_MIN", 1000), // Default request limit
RequestWindowSec: getEnvFloat64("REQUEST_WINDOW_SEC", 60), // Default request window in float64
AllowedOrigins: getEnvArray("ALLOWED_ORIGINS", []string{"http://localhost:3000"}), // Default allowed origins
IsTestEnv: getEnvBool("IS_TEST_ENVIRONMENT", false), // Default test env
DiceDB: struct {
Addr string
Username string
Password string
}{
Addr: getEnv("DICEDB_ADDR", "localhost:7379"), // Default Dice address
Username: getEnv("DICEDB_USERNAME", "dice"), // Default username
Password: getEnv("DICEDB_PASSWORD", ""), // Default password
},
Server: struct {
Port string
IsTestEnv bool
RequestLimitPerMin int64
RequestWindowSec float64
AllowedOrigins []string
}{
Port: getEnv("SERVER_PORT", ":8080"),
IsTestEnv: getEnvBool("IS_TEST_ENVIRONMENT", false), // Default server port
RequestLimitPerMin: getEnvInt("REQUEST_LIMIT_PER_MIN", 1000), // Default request limit
RequestWindowSec: getEnvFloat64("REQUEST_WINDOW_SEC", 60), // Default request window in float64
AllowedOrigins: getEnvArray("ALLOWED_ORIGINS", []string{"http://localhost:3000"}), // Default allowed origins
},
}
}

Expand Down
11 changes: 8 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ services:
image: dicedb/dicedb:latest
ports:
- "7379"
healthcheck:
test: ["CMD", "PING"]
interval: 10s
timeout: 3s
retries: 3
networks:
- dice-network

Expand All @@ -14,9 +19,9 @@ services:
depends_on:
- dicedb
environment:
- DICE_ADDR=dicedb:7379
- DICE_USERNAME=${DICE_USERNAME}
- DICE_PASSWORD=${DICE_PASSWORD}
- DICEDB_ADDR=dicedb:7379
- DICEDB_USERNAME=${DICE_USERNAME}
- DICEDB_PASSWORD=${DICE_PASSWORD}
networks:
- dice-network

Expand Down
4 changes: 3 additions & 1 deletion internal/db/dicedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ func (db *DiceDB) CloseDiceDB() {

func InitDiceClient(configValue *config.Config) (*DiceDB, error) {
diceClient := dicedb.NewClient(&dicedb.Options{
Addr: configValue.DiceDBAddr,
Addr: configValue.DiceDB.Addr,
Username: configValue.DiceDB.Username,
Password: configValue.DiceDB.Password,
DialTimeout: 10 * time.Second,
MaxRetries: 10,
})
Expand Down
2 changes: 1 addition & 1 deletion internal/middleware/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
// Updated enableCors function to return a boolean indicating if OPTIONS was handled
func handleCors(w http.ResponseWriter, r *http.Request) bool {
configValue := config.LoadConfig()
allAllowedOrigins := configValue.AllowedOrigins
allAllowedOrigins := configValue.Server.AllowedOrigins
origin := r.Header.Get("Origin")
allowed := false

Expand Down
12 changes: 6 additions & 6 deletions internal/tests/integration/ratelimiter_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (

func TestRateLimiterWithinLimit(t *testing.T) {
configValue := config.LoadConfig()
limit := configValue.RequestLimitPerMin
window := configValue.RequestWindowSec
limit := configValue.Server.RequestLimitPerMin
window := configValue.Server.RequestWindowSec

w, r, rateLimiter := util.SetupRateLimiter(limit, window)

Expand All @@ -25,8 +25,8 @@ func TestRateLimiterWithinLimit(t *testing.T) {

func TestRateLimiterExceedsLimit(t *testing.T) {
configValue := config.LoadConfig()
limit := configValue.RequestLimitPerMin
window := configValue.RequestWindowSec
limit := configValue.Server.RequestLimitPerMin
window := configValue.Server.RequestWindowSec

w, r, rateLimiter := util.SetupRateLimiter(limit, window)

Expand All @@ -43,8 +43,8 @@ func TestRateLimiterExceedsLimit(t *testing.T) {

func TestRateLimitHeadersSet(t *testing.T) {
configValue := config.LoadConfig()
limit := configValue.RequestLimitPerMin
window := configValue.RequestWindowSec
limit := configValue.Server.RequestLimitPerMin
window := configValue.Server.RequestWindowSec

w, r, rateLimiter := util.SetupRateLimiter(limit, window)

Expand Down
4 changes: 2 additions & 2 deletions internal/tests/stress/ratelimiter_stress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (

func TestRateLimiterUnderStress(t *testing.T) {
configValue := config.LoadConfig()
limit := configValue.RequestLimitPerMin
window := configValue.RequestWindowSec
limit := configValue.Server.RequestLimitPerMin
window := configValue.Server.RequestWindowSec

_, r, rateLimiter := util.SetupRateLimiter(limit, window)

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func main() {

// Create mux and register routes
mux := http.NewServeMux()
httpServer := server.NewHTTPServer(":8080", mux, diceClient, configValue.RequestLimitPerMin, configValue.RequestWindowSec)
httpServer := server.NewHTTPServer(":8080", mux, diceClient, configValue.Server.RequestLimitPerMin, configValue.Server.RequestWindowSec)
mux.HandleFunc("/health", httpServer.HealthCheck)
mux.HandleFunc("/shell/exec/{cmd}", httpServer.CliHandler)
mux.HandleFunc("/search", httpServer.SearchHandler)
Expand Down
2 changes: 1 addition & 1 deletion util/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func ParseHTTPRequest(r *http.Request) (*cmds.CommandRequest, error) {

configValue := config.LoadConfig()
// Check if the command is blocklisted
if err := BlockListedCommand(command); err != nil && !configValue.IsTestEnv {
if err := BlockListedCommand(command); err != nil && !configValue.Server.IsTestEnv {
return nil, err
}

Expand Down

0 comments on commit 5cecd74

Please sign in to comment.