-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
77 lines (68 loc) · 1.81 KB
/
Copy pathmain.go
File metadata and controls
77 lines (68 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"os"
"github.com/atotto/clipboard"
qrterminal "github.com/mdp/qrterminal/v3"
)
type Payload struct {
Text string `json:"text"`
}
func main() {
port := 8000
ip := getLocalIP()
if ip == "" {
log.Fatal("❌ Could not determine local IP address")
}
address := fmt.Sprintf("http://%s:%d", ip, port)
fmt.Println("📡 Server available at:", address)
fmt.Println("📱 Scan this QR code to connect:")
// ✅ Fixed Config
cfg := qrterminal.Config{
Level: qrterminal.L,
Writer: os.Stdout,
}
qrterminal.GenerateWithConfig(address, cfg)
// Start HTTP server
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodPost:
var payload Payload
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
http.Error(w, "❌ Invalid JSON", http.StatusBadRequest)
return
}
if err := clipboard.WriteAll(payload.Text); err != nil {
http.Error(w, "❌ Failed to write to clipboard", http.StatusInternalServerError)
return
}
log.Println("📋 Clipboard updated:", payload.Text)
fmt.Fprintln(w, "✅ Clipboard updated")
case http.MethodGet:
fmt.Fprintln(w, "🖥️ Clipboard server is running.\nSend a POST with JSON: { \"text\": \"...\" }")
default:
http.Error(w, "❌ Method not allowed", http.StatusMethodNotAllowed)
}
})
log.Printf("🚀 Serving on %s\n", address)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
}
// getLocalIP returns the first non-loopback IPv4 address
func getLocalIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, addr := range addrs {
if ipNet, ok := addr.(*net.IPNet); ok &&
!ipNet.IP.IsLoopback() &&
ipNet.IP.To4() != nil {
return ipNet.IP.String()
}
}
return ""
}