-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
145 lines (118 loc) · 3.55 KB
/
main.go
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"flag"
"fmt"
"net/http"
"os"
"sync"
"github.com/gorilla/websocket"
"beginbot.com/GoBeginGPT/pkg/file_watchers"
"beginbot.com/GoBeginGPT/pkg/gpt_response_parser"
"beginbot.com/GoBeginGPT/pkg/skybox"
)
var clients = make(map[*websocket.Conn]bool)
var broadcast = make(chan string)
var mutex = &sync.Mutex{}
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { return true },
}
func showAndTell(broadcast chan string) {
done := make(chan bool)
go file_watchers.Look4MoveRequests(broadcast)
go file_watchers.Look4VoiceRequests(broadcast)
go file_watchers.Look4GptRequests(broadcast)
go file_watchers.Look4RemixRequests(broadcast)
go file_watchers.Look4SkyboxRequests(broadcast)
go file_watchers.Look4PreviousRequests(broadcast)
// This is not currently working
// go file_watchers.Look4DuetRequests(broadcast)
go handleBroadcast()
fmt.Println("Server is listening on :8080")
http.HandleFunc("/ws", websocketHandler)
http.Handle("/", http.FileServer(http.Dir("static")))
err := http.ListenAndServe(":8080", nil)
if err != nil {
fmt.Println("Error starting server:", err)
}
<-done
}
func websocketHandler(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Println("Error upgrading to WebSocket:", err)
return
}
mutex.Lock()
clients[conn] = true
mutex.Unlock()
}
func handleBroadcast() {
for {
content := <-broadcast
mutex.Lock()
for client := range clients {
err := client.WriteMessage(websocket.TextMessage, []byte(content))
if err != nil {
fmt.Println("Error broadcasting message:", err)
client.Close()
delete(clients, client)
for {
fmt.Println("Attempting to reconnect to WebSocket...")
url := "ws://localhost:8080"
conn, _, err := websocket.DefaultDialer.Dial(url, nil)
if err != nil {
fmt.Println("Error dialing WebSocket:", err)
} else {
mutex.Lock()
clients[conn] = true
mutex.Unlock()
fmt.Println("Successfully Connected to WebSocket.")
err = conn.WriteMessage(websocket.TextMessage, []byte(content))
if err != nil {
fmt.Println("Error resending message:", err)
}
break
}
}
}
}
mutex.Unlock()
}
}
// TODO: Ponder naming all this better
func main() {
webserver := flag.Bool("webserver", false, "Whether to run a Seal Webserver")
duet := flag.Bool("duet", false, "Whether to run Duet code")
skybox_styles := flag.Bool("styles", false, "Whether to query for all Skybox Styles")
prompt_file := flag.String("prompt_file", "prompt.txt", "The file that contains the prompt")
remix := flag.Bool("remix", false, "Whether to remix the Skybox Styles")
var remixID int
flag.IntVar(&remixID, "remix_id", 0, "The skybox ID of skybox you want to remix")
var prompt string
flag.StringVar(&prompt, "prompt", "", "The prompt you want to generate")
var styleId int
flag.IntVar(&styleId, "style", 20, "The Skybox Style ID")
flag.Parse()
fmt.Printf("INFO: %s | ID: %d\n", prompt, remixID)
if *webserver {
showAndTell(broadcast)
} else if *remix {
if prompt == "" || remixID == 0 {
fmt.Printf("Need to pass in prompt: %s | ID: %d\n", prompt, remixID)
return
}
skybox.Remix(remixID, styleId, prompt)
} else if *skybox_styles {
skybox.RequestAllStyles()
} else if *duet {
gpt_response_parser.SplitDuet(broadcast, "duet.txt")
} else {
b, err := os.ReadFile(*prompt_file)
if err != nil {
panic(fmt.Sprintf("%+v", err))
}
prompt := string(b)
fmt.Printf("Prompt: %+v", prompt)
skybox.GenerateSkybox(prompt)
}
}