112 lines
3.2 KiB
Go
112 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
var logFile *os.File
|
|
|
|
func main() {
|
|
// Open log file
|
|
var err error
|
|
logFile, err = os.OpenFile("/tmp/clawdnode-debug.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer logFile.Close()
|
|
|
|
// Log to both file and stdout
|
|
log.SetOutput(io.MultiWriter(os.Stdout, logFile))
|
|
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds)
|
|
|
|
http.HandleFunc("/", handleRoot)
|
|
http.HandleFunc("/log", handleLog)
|
|
http.HandleFunc("/notification", handleNotification)
|
|
http.HandleFunc("/call", handleCall)
|
|
http.HandleFunc("/event", handleEvent)
|
|
http.HandleFunc("/health", handleHealth)
|
|
|
|
port := "9876"
|
|
log.Printf("🚀 ClawdNode Debug Server starting on :%s", port)
|
|
log.Printf("📝 Logging to /tmp/clawdnode-debug.log")
|
|
log.Printf("Endpoints: /log, /notification, /call, /event, /health")
|
|
|
|
if err := http.ListenAndServe(":"+port, nil); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func handleRoot(w http.ResponseWriter, r *http.Request) {
|
|
log.Printf("[%s] %s %s from %s", r.Method, r.URL.Path, r.URL.RawQuery, r.RemoteAddr)
|
|
fmt.Fprintf(w, "ClawdNode Debug Server\n\nEndpoints:\n- POST /log\n- POST /notification\n- POST /call\n- POST /event\n- GET /health\n")
|
|
}
|
|
|
|
func handleHealth(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(map[string]interface{}{
|
|
"status": "ok",
|
|
"time": time.Now().UTC().Format(time.RFC3339),
|
|
})
|
|
}
|
|
|
|
func handleLog(w http.ResponseWriter, r *http.Request) {
|
|
body, _ := io.ReadAll(r.Body)
|
|
log.Printf("📋 [LOG] from %s:\n%s", r.RemoteAddr, string(body))
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(map[string]string{"status": "logged"})
|
|
}
|
|
|
|
func handleNotification(w http.ResponseWriter, r *http.Request) {
|
|
body, _ := io.ReadAll(r.Body)
|
|
|
|
var data map[string]interface{}
|
|
json.Unmarshal(body, &data)
|
|
|
|
log.Printf("🔔 [NOTIFICATION] from %s:", r.RemoteAddr)
|
|
log.Printf(" App: %v", data["app"])
|
|
log.Printf(" Title: %v", data["title"])
|
|
log.Printf(" Text: %v", data["text"])
|
|
log.Printf(" Package: %v", data["package"])
|
|
log.Printf(" Raw: %s", string(body))
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(map[string]string{"status": "received"})
|
|
}
|
|
|
|
func handleCall(w http.ResponseWriter, r *http.Request) {
|
|
body, _ := io.ReadAll(r.Body)
|
|
|
|
var data map[string]interface{}
|
|
json.Unmarshal(body, &data)
|
|
|
|
log.Printf("📞 [CALL] from %s:", r.RemoteAddr)
|
|
log.Printf(" Number: %v", data["number"])
|
|
log.Printf(" Contact: %v", data["contact"])
|
|
log.Printf(" State: %v", data["state"])
|
|
log.Printf(" Raw: %s", string(body))
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(map[string]string{"status": "received"})
|
|
}
|
|
|
|
func handleEvent(w http.ResponseWriter, r *http.Request) {
|
|
body, _ := io.ReadAll(r.Body)
|
|
|
|
var data map[string]interface{}
|
|
json.Unmarshal(body, &data)
|
|
|
|
eventType := data["type"]
|
|
log.Printf("⚡ [EVENT:%v] from %s:", eventType, r.RemoteAddr)
|
|
log.Printf(" Raw: %s", string(body))
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(map[string]string{"status": "received"})
|
|
}
|