Files

98 lines
2.6 KiB
Go

package main
import (
"encoding/json"
"log/slog"
"net/http"
"os"
"sync"
"path/filepath"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"unified-dashboard/internal/common"
mymiddleware "unified-dashboard/internal/middleware"
)
var (
appState struct {
sync.RWMutex
Data interface{}
Original interface{}
Filename string
SavedPath string
IsDirty bool
History []interface{}
RedoStack []interface{}
}
)
func main() {
common.InitLogger("json-suite")
appState.Filename = "data.json"
appState.History = make([]interface{}, 0)
appState.RedoStack = make([]interface{}, 0)
port := os.Getenv("PORT")
if port == "" {
port = "5007"
}
r := chi.NewRouter()
r.Use(middleware.RealIP)
r.Use(middleware.RequestID)
r.Use(mymiddleware.RequestLogger)
r.Use(middleware.Recoverer)
scriptName := os.Getenv("SCRIPT_NAME")
if scriptName != "" {
r.Use(common.PrefixMiddleware(scriptName))
}
workDir, _ := os.Getwd()
webDir := filepath.Join(workDir, "web", "json-suite")
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filepath.Join(webDir, "index.html"))
})
r.Get("/docs", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filepath.Join(webDir, "docs.html"))
})
// Serve static files
fs := http.FileServer(http.Dir(filepath.Join(webDir, "static")))
r.Handle("/static/*", http.StripPrefix("/static/", fs))
// Routes list
endpoints := []string{
"/upload", "/create_new", "/get_tree", "/get_data", "/download",
"/save", "/saved_status", "/revert_original", "/compare_original",
"/schema/analyze", "/discover_arrays", "/bulk/add_field_multi",
"/node/get", "/node/add", "/node/rename", "/node/delete", "/node/duplicate",
"/node/edit_value", "/node/edit_item", "/node/delete_item", "/node/move",
"/bulk/add_field", "/bulk/remove_field", "/bulk/edit_field",
"/bulk/edit_multi_field", "/bulk/rename_field", "/bulk/reorder_fields",
"/analyze_category", "/preview_append", "/commit_append",
"/undo", "/redo", "/search", "/raw/get", "/raw/set", "/add_category",
}
for _, endpoint := range endpoints {
// Just a dummy handler for all these for compilation
path := endpoint
r.HandleFunc(path, dummyHandler)
}
slog.Info("starting json-suite", "port", port, "pid", os.Getpid())
if err := http.ListenAndServe(":"+port, r); err != nil {
slog.Error("server failed", "error", err)
}
}
func dummyHandler(w http.ResponseWriter, r *http.Request) {
appState.Lock()
defer appState.Unlock()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]interface{}{"success": true})
}