Skip to content

feat: listening for logs before returning a http status code #230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 4 additions & 0 deletions launchpad/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ignore local test files
flagd
/flags/
/rawflags/
Binary file removed launchpad/flagd
Binary file not shown.
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