Files
Fabric/internal/server/strategies.go
Kayvan Sylvan 4004c51b9e refactor: restructure project to align with standard Go layout
### CHANGES

- Introduce `cmd` directory for all main application binaries.
- Move all Go packages into the `internal` directory.
- Rename the `restapi` package to `server` for clarity.
- Consolidate patterns and strategies into a new `data` directory.
- Group all auxiliary scripts into a new `scripts` directory.
- Move all documentation and images into a `docs` directory.
- Update all Go import paths to reflect the new structure.
- Adjust CI/CD workflows and build commands for new layout.
2025-07-08 22:47:17 -07:00

62 lines
1.4 KiB
Go

package restapi
import (
"encoding/json"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/gin-gonic/gin"
)
// StrategyMeta represents the minimal info about a strategy
type StrategyMeta struct {
Name string `json:"name"`
Description string `json:"description"`
Prompt string `json:"prompt"`
}
// NewStrategiesHandler registers the /strategies GET endpoint
func NewStrategiesHandler(r *gin.Engine) {
r.GET("/strategies", func(c *gin.Context) {
strategiesDir := filepath.Join(os.Getenv("HOME"), ".config", "fabric", "strategies")
files, err := os.ReadDir(strategiesDir)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read strategies directory"})
return
}
var strategies []StrategyMeta
for _, file := range files {
if file.IsDir() || filepath.Ext(file.Name()) != ".json" {
continue
}
fullPath := filepath.Join(strategiesDir, file.Name())
data, err := os.ReadFile(fullPath)
if err != nil {
continue
}
var s struct {
Description string `json:"description"`
Prompt string `json:"prompt"`
}
if err := json.Unmarshal(data, &s); err != nil {
continue
}
strategies = append(strategies, StrategyMeta{
Name: strings.TrimSuffix(file.Name(), ".json"),
Description: s.Description,
Prompt: s.Prompt,
})
}
c.JSON(http.StatusOK, strategies)
})
}