-
Notifications
You must be signed in to change notification settings - Fork 22
/
server.go
173 lines (142 loc) · 4.21 KB
/
server.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package revealgo
import (
"embed"
"fmt"
"log"
"net/http"
"path/filepath"
"text/template"
socketio "github.com/googollee/go-socket.io"
"github.com/googollee/go-socket.io/engineio"
"github.com/googollee/go-socket.io/engineio/transport"
"github.com/googollee/go-socket.io/engineio/transport/polling"
"github.com/googollee/go-socket.io/engineio/transport/websocket"
"golang.org/x/crypto/bcrypt"
)
//go:embed assets/revealjs
var revealjs embed.FS
//go:embed assets/socketio
var socketiojs embed.FS
//go:embed assets/multiplex
var multiplex embed.FS
//go:embed assets/templates/slide.html
var slideTemplate string
type Server struct {
port int
}
type ServerParam struct {
Path string
Theme string
OriginalTheme bool
DisableAutoOpen bool
Transition string
Separator string
VerticalSeparator string
Multiplex MultiplexParam
}
type MultiplexParam struct {
IsMaster bool
Secret string
Identifier string
}
type RevealMultiplexData struct {
Secret string `json:"secret,omitempty"`
SocketId string `json:"socketId"`
State map[string]interface{} `json:"state"`
}
func (server *Server) Serve(param ServerParam) {
port := 3000
if server.port > 0 {
port = server.port
}
fmt.Printf("accepting connections at http://*:%d/\n", port)
http.Handle("/", contentHandler(param, http.FileServer(http.Dir("."))))
http.Handle("/revealjs/", assetsHandler("/assets/", http.FileServer(http.FS(revealjs))))
if param.Multiplex.Secret != "" {
socketioServer := setupSocketIO()
go func() {
if err := socketioServer.Serve(); err != nil {
log.Fatalf("socketio listen error: %s\n", err)
}
}()
defer socketioServer.Close()
param.Multiplex.IsMaster = true
http.Handle("/master/", http.StripPrefix("/master", contentHandler(param, http.FileServer(http.Dir(".")))))
http.Handle("/socketio/", assetsHandler("/assets/", http.FileServer(http.FS(socketiojs))))
http.Handle("/multiplex/", assetsHandler("/assets/", http.FileServer(http.FS(multiplex))))
http.Handle("/socket.io/", socketioServer)
fmt.Printf("master slides can be found at http://*:%d/master/\n", port)
}
if !param.DisableAutoOpen {
go openBrowser(port)
}
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil {
log.Fatal("ListenAndServe:", err)
}
}
func setupSocketIO() *socketio.Server {
var allowOriginFunc = func(r *http.Request) bool {
return true
}
server := socketio.NewServer(&engineio.Options{
Transports: []transport.Transport{
&polling.Transport{
CheckOrigin: allowOriginFunc,
},
&websocket.Transport{
CheckOrigin: allowOriginFunc,
},
},
})
server.OnConnect("/", func(s socketio.Conn) error {
s.SetContext("")
log.Println("connected:", s.ID())
return nil
})
server.OnEvent("/", "multiplex-statechanged", func(s socketio.Conn, data RevealMultiplexData) {
if err := bcrypt.CompareHashAndPassword([]byte(data.SocketId), []byte(data.Secret)); err != nil {
return
}
data.Secret = ""
server.BroadcastToNamespace("/", data.SocketId, data)
})
server.OnError("/", func(s socketio.Conn, e error) {
log.Println("Socket error:", e)
})
server.OnDisconnect("/", func(s socketio.Conn, reason string) {
log.Println("closed", reason)
})
return server
}
func contentHandler(params ServerParam, h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if mimeType := detectContentType(r.URL.Path); mimeType != "" {
w.Header().Set("Content-Type", mimeType)
}
if r.URL.Path != "/" {
h.ServeHTTP(w, r)
return
}
tmpl, err := template.New("slide template").Parse(slideTemplate)
if err != nil {
log.Printf("error:%v", err)
http.NotFound(w, r)
return
}
if err := tmpl.Execute(w, params); err != nil {
log.Fatalf("error:%v", err)
}
})
}
func assetsHandler(prefix string, h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if mimeType := detectContentType(r.URL.Path); mimeType != "" {
w.Header().Set("Content-Type", mimeType)
}
if prefix != "" {
r.URL.Path = filepath.Join(prefix, r.URL.Path)
r.URL.RawPath = filepath.Join(prefix, r.URL.RawPath)
}
h.ServeHTTP(w, r)
})
}