Skip to content

Commit

Permalink
fix: docker file and enahanced config structure
Browse files Browse the repository at this point in the history
  • Loading branch information
vinitparekh17 committed Oct 2, 2024
1 parent 088f894 commit aad46c1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 36 deletions.
25 changes: 5 additions & 20 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"
CMD ["./playground-mono"]
40 changes: 28 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/db/dicedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit aad46c1

Please sign in to comment.