package adminpanel import ( "encoding/json" "fmt" "net/http" "strings" "writebot/internal/helpers" "writebot/internal/writeserver" ) var ( writePort int controlPort int ) func Init(wp, cp int) { writePort = wp controlPort = cp } const adminHTML = ` WriteBot

WriteBot

Ctrl+D bookmark this page
Loading...
-
` func CreateControlServer() *http.Server { html := strings.ReplaceAll(adminHTML, "WRITE_PORT", fmt.Sprintf("%d", writePort)) mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { path := r.URL.Path w.Header().Set("Access-Control-Allow-Origin", "*") switch path { case "/": w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Write([]byte(html)) case "/status": w.Header().Set("Content-Type", "application/json") s := writeserver.GetWriteStatus() s["ip"] = helpers.GetLanIP("WLAN") s["port"] = writePort json.NewEncoder(w).Encode(s) case "/start": if r.Method == "POST" { w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(writeserver.StartWriteServer()) } else { http.NotFound(w, r) } case "/stop": if r.Method == "POST" { w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(map[string]bool{"ok": writeserver.StopWriteServer()}) } else { http.NotFound(w, r) } case "/ip": w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(map[string]interface{}{ "ip": helpers.GetLanIP("WLAN"), "port": writePort, }) case "/health": w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(map[string]bool{"ok": true}) default: w.WriteHeader(http.StatusNotFound) w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(map[string]string{"error": "not found"}) } }) addr := fmt.Sprintf("127.0.0.1:%d", controlPort) srv := &http.Server{ Addr: addr, Handler: mux, } fmt.Printf("[control] http://%s\n", addr) return srv }