Skip to content

Commit

Permalink
fixup: refactor launchpad, clearer structure, cancel timer
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Schrottner <[email protected]>
  • Loading branch information
aepfli committed Feb 20, 2025
1 parent cf38c5a commit 16001f5
Show file tree
Hide file tree
Showing 7 changed files with 390 additions and 374 deletions.
4 changes: 3 additions & 1 deletion flagd/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ FROM golang:1.24 AS builder
WORKDIR /app

# Copy Go modules and dependencies
COPY launchpad/go.mod launchpad/go.sum launchpad/main.go ./
COPY launchpad/go.mod launchpad/go.sum ./
RUN go mod download

COPY launchpad/ ./

# Build the Go binary
RUN go build -o launchpad main.go

Expand Down
75 changes: 75 additions & 0 deletions launchpad/handlers/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package handlers

import (
"encoding/json"
"fmt"
"net/http"
"openfeature.com/flagd-testbed/launchpad/pkg"
"strconv"
)

// Response struct to standardize API responses
type Response struct {
Status string `json:"status"`
Message string `json:"message"`
}

// StartFlagdHandler starts the `flagd` process
func StartFlagdHandler(w http.ResponseWriter, r *http.Request) {
config := r.URL.Query().Get("config")

if err := flagd.StartFlagd(config); err != nil {
respondWithJSON(w, http.StatusInternalServerError, "error", fmt.Sprintf("Failed to start flagd: %v", err))
return
}
respondWithJSON(w, http.StatusOK, "success", "flagd started successfully")
}

// RestartHandler stops and starts `flagd`
func RestartHandler(w http.ResponseWriter, r *http.Request) {
secondsStr := r.URL.Query().Get("seconds")
if secondsStr == "" {
secondsStr = "5"
}

seconds, err := strconv.Atoi(secondsStr)
if err != nil || seconds < 0 {
respondWithJSON(w, http.StatusBadRequest, "error", "'seconds' must be a non-negative integer")
return
}

flagd.RestartFlagd(seconds)
respondWithJSON(w, http.StatusOK, "success", fmt.Sprintf("flagd will restart in %d seconds", seconds))
}

// StopFlagdHandler stops `flagd`
func StopFlagdHandler(w http.ResponseWriter, r *http.Request) {
if err := flagd.StopFlagd(); err != nil {
respondWithJSON(w, http.StatusInternalServerError, "error", fmt.Sprintf("Failed to stop flagd: %v", err))
return
}
respondWithJSON(w, http.StatusOK, "success", "flagd stopped successfully")
}

// ChangeHandler triggers JSON file merging and notifies `flagd`
func ChangeHandler(w http.ResponseWriter, r *http.Request) {
if err := flagd.CombineJSONFiles(flagd.InputDir); err != nil {
respondWithJSON(w, http.StatusInternalServerError, "error", fmt.Sprintf("Failed to update JSON files: %v", err))
return
}

respondWithJSON(w, http.StatusOK, "success", "JSON files updated successfully")
}

// Utility function to send JSON responses
func respondWithJSON(w http.ResponseWriter, statusCode int, status, message string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)

response := Response{
Status: status,
Message: message,
}

json.NewEncoder(w).Encode(response)
}
Loading

0 comments on commit 16001f5

Please sign in to comment.