diff --git a/Dockerfile b/Dockerfile index 74c2bdd..9e4a735 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,30 +1,15 @@ # Stage 1: Build the Go application FROM golang:1.23 AS go-builder - -# Set the working directory for Go WORKDIR /app - -# Install git to clone repositories -RUN apt-get update && apt-get install -y git - -# Clone the Go repository (playground-mono) -COPY . /app/playground-mono -WORKDIR /app/playground-mono - -# Download Go module dependencies -RUN go mod download +COPY go.mod go.sum ./ +RUN go mod download -x +COPY . . RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o playground-mono . # Stage 2: Build the final container with the Go binary FROM alpine:latest RUN apk --no-cache add ca-certificates - -# Set the working directory for the final container WORKDIR /app - -# Copy Go binary from the Go builder stage -COPY --from=go-builder /app/playground-mono/playground-mono /app/playground-mono - +COPY --from=go-builder /app/playground-mono . EXPOSE 8080 - -CMD sh -c "./playground-mono" \ No newline at end of file +CMD ["./playground-mono"] \ No newline at end of file diff --git a/config/config.go b/config/config.go index cf5caef..edded73 100644 --- a/config/config.go +++ b/config/config.go @@ -7,24 +7,40 @@ import ( // Config holds the application configuration type Config struct { - DiceAddr string // Field for the Dice address - Username string // Field for the username - Password string // Field for the password - ServerPort string // Field for the server port - RequestLimit int // Field for the request limit - RequestWindow int // Field for the time window in seconds + 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 + RequestLimit int // Field for the request limit + RequestWindow int // Field for the time window in seconds + } } // LoadConfig loads the application configuration from environment variables or defaults func LoadConfig() *Config { return &Config{ // if host and port is combined then it is called - DiceAddr: getEnv("DICE_ADDR", "localhost:7379"), // Default Dice address - Username: getEnv("DICE_USERNAME", "dice"), // Default username - Password: getEnv("DICE_PASSWORD", ""), // Default password is empty - ServerPort: getEnv("SERVER_PORT", ":8080"), // Default server port - RequestLimit: getEnvInt("REQUEST_LIMIT", 1000), // Default request limit - RequestWindow: getEnvInt("REQUEST_WINDOW", 60), // Default request window in seconds + DiceDB: struct { + Addr string + Username string + Password string + }{ + Addr: getEnv("DICE_ADDR", "localhost:7379"), // Default Dice address + Username: getEnv("DICE_USERNAME", "dice"), // Default username + Password: getEnv("DICE_PASSWORD", ""), // Default password + }, + Server: struct { + Port string + RequestLimit int + RequestWindow int + }{ + Port: getEnv("SERVER_PORT", ":8080"), // Default server port + RequestLimit: getEnvInt("REQUEST_LIMIT", 1000), // Default request limit + RequestWindow: getEnvInt("REQUEST_WINDOW", 60), // Default request window in seconds + }, } } diff --git a/internal/db/dicedb.go b/internal/db/dicedb.go index 52e842b..17cf480 100644 --- a/internal/db/dicedb.go +++ b/internal/db/dicedb.go @@ -37,9 +37,9 @@ func (db *DiceDB) CloseDiceDB() { func InitDiceClient(configValue *config.Config) (*DiceDB, error) { diceClient := dice.NewClient(&dice.Options{ - Addr: configValue.DiceAddr, - Username: configValue.Username, - Password: configValue.Password, + Addr: configValue.DiceDB.Addr, + Username: configValue.DiceDB.Username, + Password: configValue.DiceDB.Password, DialTimeout: 10 * time.Second, MaxRetries: 10, }) diff --git a/main.go b/main.go index 09a082f..e6dacc5 100644 --- a/main.go +++ b/main.go @@ -18,7 +18,7 @@ func main() { // Create mux and register routes mux := http.NewServeMux() - httpServer := server.NewHTTPServer(":8080", mux, diceClient, configValue.RequestLimit, configValue.RequestWindow) + httpServer := server.NewHTTPServer(":8080", mux, diceClient, configValue.Server.RequestLimit, configValue.Server.RequestWindow) mux.HandleFunc("/health", httpServer.HealthCheck) mux.HandleFunc("/cli/{cmd}", httpServer.CliHandler) mux.HandleFunc("/search", httpServer.SearchHandler)