Skip to content

Commit

Permalink
feat: auth support with dockerfile (#15)
Browse files Browse the repository at this point in the history
Co-authored-by: pshubham <[email protected]>
  • Loading branch information
vinitparekh17 and lucifercr07 authored Oct 18, 2024
1 parent 1f6392a commit 62d5d90
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 30 deletions.
8 changes: 8 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
DICEDB_ADDR=localhost:7379
DICEDB_USERNAME=dice
DICEDB_PASSWORD=
SERVER_PORT=:8080
IS_TEST_ENVIRONMENT=false
REQUEST_LIMIT_PER_MIN=1000
REQUEST_WINDOW_SEC=60
ALLOWED_ORIGINS=http://localhost:3000
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Stage 1: Build the Go application
FROM golang:1.23 AS go-builder
WORKDIR /app
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
WORKDIR /app
COPY --from=go-builder /app/playground-mono .
EXPOSE 8080
CMD ["./playground-mono"]
42 changes: 40 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,47 @@ $ sudo su
$ curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b /bin v1.60.1
```

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

### Running the Project Using Docker

#### 1. Clone the repository:

```bash
git clone https://github.com/dicedb/playground-mono
```

#### 2. Navigate to the project directory:

```bash
cd playground-mono
```

#### 3. Copy the sample environment file:

```bash
cp .env.sample .env
```
This creates the `.env` file, which stores your environment variables. Make sure to update any necessary values inside this file before running the server.

#### 4. Start the application using Docker Compose:

```bash
docker compose up -d
```
This command will pull any necessary Docker images, build the containers, and run them in detached mode (`-d`).

#### 5. Verify the server is running:

Open your browser and go to:

```bash
http://localhost:8080/health
```
This endpoint should return a status indicating that the server is up and running.
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
31 changes: 31 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
services:
dicedb:
image: dicedb/dicedb:latest
ports:
- "7379:7379"
healthcheck:
test: ["CMD", "PING"]
interval: 10s
timeout: 3s
retries: 3
networks:
- dice-network

backend:
build:
context: .
ports:
- "8080:8080"
depends_on:
- dicedb
environment:
- DICEDB_ADDR=localhost:7379
- DICEDB_USERNAME=${DICEDB_USERNAME}
- DICEDB_PASSWORD=${DICEDB_PASSWORD}
networks:
- dice-network

networks:
dice-network:
driver: bridge

4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/dicedb/dicedb-go v0.0.0-20241011194507-ad62a2dfc08e h1:H53752EPCePdIz7ceoYNIfiHWpiSrKE1rVKXnSyV8KI=
github.com/dicedb/dicedb-go v0.0.0-20241011194507-ad62a2dfc08e/go.mod h1:luVXhxSZ0TkZvA3dCqDPQ0WaLx5AZ29+/5gDyWq1Xi0=
github.com/dicedb/dicedb-go v0.0.0-20241015181607-d31c1df12107 h1:sL5dXtCsogSMP/afS2K2vVMMYFqJy02EezeRXpZnGy0=
github.com/dicedb/dicedb-go v0.0.0-20241015181607-d31c1df12107/go.mod h1:iaOsphlvjJ87VL/5d32ZgeQYxhYS51k7/bvFKro7lWk=
github.com/dicedb/go-dice v0.0.0-20240820180649-d97f15fca831 h1:Cqyj9WCtoobN6++bFbDSe27q94SPwJD9Z0wmu+SDRuk=
github.com/dicedb/go-dice v0.0.0-20240820180649-d97f15fca831/go.mod h1:8+VZrr14c2LW8fW4tWZ8Bv3P2lfvlg+PpsSn5cWWuiQ=
github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk=
github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
github.com/docker/docker v27.1.1+incompatible h1:hO/M4MtV36kzKldqnA37IWhebRA+LnqqcqDja6kVaKY=
Expand Down
4 changes: 3 additions & 1 deletion internal/db/dicedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,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,
EnablePrettyResponse: true,
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
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"server/config"
"server/internal/db"
"server/internal/server"

_ "github.com/joho/godotenv/autoload"
)

func main() {
Expand All @@ -20,7 +22,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 62d5d90

Please sign in to comment.