-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
152 lines (127 loc) · 3.41 KB
/
main.go
File metadata and controls
152 lines (127 loc) · 3.41 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
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
146
147
148
149
150
151
152
package main
import (
"fmt"
"net/http"
"os"
"path/filepath"
"time"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
var clients = make(map[*websocket.Conn]bool)
func main() {
if len(os.Args) < 2 {
panic("give root of fileServer as commandline arg")
}
directoryPath := os.Args[1]
_, err := os.Stat(directoryPath)
if os.IsNotExist(err) {
fmt.Printf("dir %s not found.\n", directoryPath)
return
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
serveFileWithScript(w, r, directoryPath)
})
http.HandleFunc("/ws", handleWebSocket)
go watchDirectory(directoryPath)
port := 7060
fmt.Printf("Server started at http://localhost:%d\n", port)
err = http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
if err != nil {
fmt.Printf("Error starting server: %s\n", err)
}
}
func serveFileWithScript(w http.ResponseWriter, r *http.Request, directoryPath string) {
filePath := filepath.Join(directoryPath, r.URL.Path)
// Check if the requested file is an HTML file
if filepath.Ext(filePath) == ".html" {
// Serve the HTML file with injected script
http.ServeFile(w, r, filePath)
injectScript(w, filePath)
} else {
// Serve other files as raw data
http.ServeFile(w, r, filePath)
}
}
func injectScript(w http.ResponseWriter, filePath string) {
// Read the HTML file content
content, err := os.ReadFile(filePath)
if err != nil {
http.Error(w, "Could not read file", http.StatusInternalServerError)
return
}
// Inject the script before the closing </body> tag
script := `<script>
const socket = new WebSocket('ws://localhost:7060/ws');
socket.onmessage = function(event) {
if (event.data === 'refresh') {
location.reload();
console.log("trying content with websocket notfication");
}
};
</script>`
// Replace the closing </body> tag with the injected script
contentStr := string(content)
contentStr = contentStr[:len(contentStr)-7] + script + "\n</body>"
// Write the modified content back to the response
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(contentStr))
}
func handleWebSocket(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Println("Error upgrading connection:", err)
return
}
defer conn.Close()
clients[conn] = true
// Keep the connection open
for {
_, _, err := conn.ReadMessage()
if err != nil {
fmt.Println("Error reading message:", err)
break
}
}
delete(clients, conn)
}
func watchDirectory(directoryPath string) {
lastModTime := time.Time{}
for {
files, err := os.ReadDir(directoryPath)
if err != nil {
fmt.Println("Error reading directory:", err)
return
}
var newestModTime time.Time
for _, file := range files {
info, err := file.Info()
if err != nil {
fmt.Println("Error getting file info:", err)
continue
}
if info.ModTime().After(newestModTime) {
newestModTime = info.ModTime()
}
}
if newestModTime.After(lastModTime) {
lastModTime = newestModTime
notifyClients()
}
time.Sleep(1 * time.Second) // Check every second
}
}
func notifyClients() {
for client := range clients {
err := client.WriteMessage(websocket.TextMessage, []byte("refresh"))
if err != nil {
fmt.Println("Error sending message:", err)
client.Close()
delete(clients, client)
}
}
}