2026-10-26 11:17:13 +05:30
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"log/slog"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
CollectionsFile = "data/bulk-api-tester/collections.json"
|
|
|
|
|
SpacesFile = "data/bulk-api-tester/spaces.json"
|
|
|
|
|
LastRunFile = "data/bulk-api-tester/last_run.json"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func writeJSON(w http.ResponseWriter, status int, data any) {
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
w.WriteHeader(status)
|
|
|
|
|
json.NewEncoder(w).Encode(data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getCollectionsHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if _, err := os.Stat(CollectionsFile); err == nil {
|
|
|
|
|
var data any
|
|
|
|
|
if err := readJSON(CollectionsFile, &data); err == nil {
|
|
|
|
|
writeJSON(w, http.StatusOK, data)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Default
|
|
|
|
|
writeJSON(w, http.StatusOK, []map[string]any{
|
|
|
|
|
{"id": "default", "name": "My Requests", "requests": []any{}},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func saveCollectionsHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var data []any
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
|
|
|
|
writeJSON(w, http.StatusBadRequest, map[string]any{"success": false, "error": "Invalid data format, expected list"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := atomicWriteJSON(CollectionsFile, data); err != nil {
|
|
|
|
|
slog.Error("failed to save collections", "error", err)
|
|
|
|
|
writeJSON(w, http.StatusInternalServerError, map[string]any{"success": false, "error": err.Error()})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
slog.Info("saved collections", "count", len(data))
|
|
|
|
|
writeJSON(w, http.StatusOK, map[string]bool{"success": true})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getSpacesHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var spacesData []map[string]any
|
|
|
|
|
|
|
|
|
|
// Try loading spaces
|
|
|
|
|
if err := readJSON(SpacesFile, &spacesData); err != nil {
|
|
|
|
|
// Legacy migration logic
|
|
|
|
|
var legacyCollections []any
|
|
|
|
|
if err := readJSON(CollectionsFile, &legacyCollections); err == nil && len(legacyCollections) > 0 {
|
|
|
|
|
spacesData = []map[string]any{
|
|
|
|
|
{
|
|
|
|
|
"id": "vaishno",
|
|
|
|
|
"name": "Vaishno",
|
|
|
|
|
"collections": legacyCollections,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"id": "aman",
|
|
|
|
|
"name": "Aman",
|
|
|
|
|
"collections": legacyCollections,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Fallback
|
|
|
|
|
defaultCollections := []map[string]any{{"id": "default", "name": "My Requests", "requests": []any{}}}
|
|
|
|
|
spacesData = []map[string]any{
|
|
|
|
|
{"id": "vaishno", "name": "Vaishno", "collections": defaultCollections},
|
|
|
|
|
{"id": "aman", "name": "Aman", "collections": defaultCollections},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Save initialized spaces
|
|
|
|
|
atomicWriteJSON(SpacesFile, spacesData)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeJSON(w, http.StatusOK, spacesData)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func saveSpacesHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var data []any
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
|
|
|
|
writeJSON(w, http.StatusBadRequest, map[string]any{"success": false, "error": "Invalid data format, expected list"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := atomicWriteJSON(SpacesFile, data); err != nil {
|
|
|
|
|
slog.Error("failed to save spaces", "error", err)
|
|
|
|
|
writeJSON(w, http.StatusInternalServerError, map[string]any{"success": false, "error": err.Error()})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
slog.Info("saved spaces", "count", len(data))
|
|
|
|
|
writeJSON(w, http.StatusOK, map[string]bool{"success": true})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getLastRunHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
spaceID := r.URL.Query().Get("spaceId")
|
|
|
|
|
if spaceID == "" {
|
|
|
|
|
spaceID = "default"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var data map[string]any
|
|
|
|
|
if err := readJSON(LastRunFile, &data); err == nil {
|
|
|
|
|
if spaceData, ok := data[spaceID]; ok {
|
|
|
|
|
writeJSON(w, http.StatusOK, spaceData)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
writeJSON(w, http.StatusOK, map[string]any{})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func saveLastRunHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var payload map[string]any
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
|
|
|
|
slog.Error("invalid saveLastRun payload", "error", err)
|
|
|
|
|
writeJSON(w, http.StatusBadRequest, map[string]any{"error": err.Error()})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
spaceID, _ := payload["spaceId"].(string)
|
|
|
|
|
if spaceID == "" {
|
|
|
|
|
spaceID = "default"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var data map[string]any
|
|
|
|
|
if err := readJSON(LastRunFile, &data); err != nil {
|
|
|
|
|
data = make(map[string]any)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The payload is the entire state data to save for this space
|
|
|
|
|
data[spaceID] = payload
|
|
|
|
|
|
|
|
|
|
if err := atomicWriteJSON(LastRunFile, data); err != nil {
|
|
|
|
|
slog.Error("failed to save last run data", "space_id", spaceID, "error", err)
|
|
|
|
|
writeJSON(w, http.StatusInternalServerError, map[string]any{"error": err.Error()})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
slog.Info("saved last run state", "space_id", spaceID)
|
|
|
|
|
writeJSON(w, http.StatusOK, map[string]bool{"success": true})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getDocsDataHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
paths := []string{
|
2026-10-26 17:57:34 +05:30
|
|
|
"data/bulk-api-tester/docs_apis.json",
|
2026-10-26 11:17:13 +05:30
|
|
|
"web/bulk-api-tester/static/data/docs_apis.json",
|
|
|
|
|
"web/docs/data.json",
|
|
|
|
|
"scratch/faithful_docs_data.json",
|
|
|
|
|
}
|
|
|
|
|
for _, p := range paths {
|
|
|
|
|
if b, err := os.ReadFile(p); err == nil {
|
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
w.Write(b)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
writeJSON(w, http.StatusNotFound, map[string]string{"error": "docs data not found"})
|
|
|
|
|
}
|
|
|
|
|
|