-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve.go
More file actions
109 lines (91 loc) · 2.45 KB
/
serve.go
File metadata and controls
109 lines (91 loc) · 2.45 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
/*
A minimalistic demonstration of:
* serving static content, embedded into your compiled Go binary
* serving a JSON based API call (and querying it from your page via fetch())
* serving websocket-based contents (and querying it from your page via WebSocket())
(C) Robert Kisteleki
Licensed under MIT license
*/
package main
import (
"embed"
"encoding/json"
"fmt"
"io/fs"
"log"
"net/http"
"time"
"github.com/gorilla/websocket"
)
//go:embed assets
var embeddedFS embed.FS
// we send these objects as JSON
type apiDataStruct struct {
IntField int `json:"i"`
StringField string `json:"s"`
}
func main() {
// "assets" is where static stuff goes to, but it's served with HTTP under /
serverRoot, err := fs.Sub(embeddedFS, "assets")
if err != nil {
log.Fatal(err)
}
// define the paths we serve: static, API, WS
http.Handle("/", http.FileServer(http.FS(serverRoot)))
http.HandleFunc("/api/json/", sampleApiResponse)
http.Handle("/api/ws/", sampleWsHandle{upgrader: websocket.Upgrader{}})
// start serving
log.Fatal(http.ListenAndServe(":8080", nil))
}
func apiData(i int, s string) ([]byte, error) {
ret := apiDataStruct{i, s}
return json.Marshal(ret)
}
// a trivial JSON response
func sampleApiResponse(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
response, err := apiData(0, "X")
if err != nil {
panic(err)
}
fmt.Fprint(w, string(response))
}
// WebSocket stuff
type sampleWsHandle struct {
upgrader websocket.Upgrader
}
// here's where the actual WebSocket handler code is
func (wsh sampleWsHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
conn, err := wsh.upgrader.Upgrade(w, r, nil)
if err != nil {
log.Printf("Error %s when upgrading connection to websocket", err)
return
}
defer func() {
log.Println("Closing WebSocket")
conn.Close()
}()
for {
// uncomment if you want to read a message first
/*
_, message, err := conn.ReadMessage()
if err != nil {
log.Printf("Error %s when reading message from client", err)
return
}
log.Printf("Received: %s", string(message))
*/
// generate some output
for i := 1; i <= 10; i++ {
// send one data object
data, _ := apiData(i, "abcdefghij"[0:i])
err = conn.WriteMessage(websocket.TextMessage, []byte(data))
if err != nil {
log.Printf("Error sending message: %v", err)
return
}
// simulate waiting for some new data to be generated
time.Sleep(1 * time.Second)
}
}
}