Skip to content

Commit

Permalink
feat(launchpad): adding restart to launchpad (#199)
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Schrottner <simon.schrottner@dynatrace.com>
  • Loading branch information
aepfli authored Jan 20, 2025
1 parent f0f4844 commit 87bd6d3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
11 changes: 10 additions & 1 deletion launchpad/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,16 @@ Launchpad is a lightweight HTTP server built in Go that controls a `flagd` binar
- **Example:**
```bash
curl -X POST http://localhost:8080/change
```

### 3. `/restart`
- **Method:** `POST`
- **Description:** restarts the running `flagd` binary.
- **Query Parameters:**
- `seconds` (optional): Time between stop and start. Defaults to `"5"`.

- **Example:**
```bash
curl -X POST http://localhost:8080/restart?seconds=5 ```

## Configuration Files

Expand Down
Binary file added launchpad/flagd
Binary file not shown.
37 changes: 36 additions & 1 deletion launchpad/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"os/exec"
"os/signal"
"strconv"
"sync"
"syscall"
"time"
Expand Down Expand Up @@ -92,6 +93,39 @@ type FlagConfig struct {
} `json:"flags"`
}

func restartHandler(w http.ResponseWriter, r *http.Request) {
// Parse the "seconds" query parameter
secondsStr := r.URL.Query().Get("seconds")
if secondsStr == "" {
secondsStr = "5"
}

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

fmt.Println("flagd will be stopped for restart\n")
// Stop flagd
if err := stopFlagd(); err != nil {
http.Error(w, fmt.Sprintf("Failed to stop flagd: %v", err), http.StatusInternalServerError)
return
}

fmt.Fprintf(w, "flagd will restart in %d seconds...\n", seconds)

// Restart flagd after the specified delay
go func(delay int) {
time.Sleep(time.Duration(delay) * time.Second)
if err := startFlagd(currentConfig); err != nil {
fmt.Printf("Failed to restart flagd: %v\n", err)
} else {
fmt.Println("flagd restarted successfully.")
}
}(seconds)
}

var mu sync.Mutex // Mutex to ensure thread-safe file operations

func changeHandler(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -157,11 +191,12 @@ func main() {

// Define your HTTP handlers
http.HandleFunc("/start", startFlagdHandler)
http.HandleFunc("/restart", restartHandler)
http.HandleFunc("/stop", stopFlagdHandler)
http.HandleFunc("/change", changeHandler)

// Create the server
server := &http.Server{Addr: ":8010"}
server := &http.Server{Addr: ":8080"}

// We put the signal handler into a goroutine
go func() {
Expand Down

0 comments on commit 87bd6d3

Please sign in to comment.