-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
364 lines (315 loc) · 7.14 KB
/
options.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package ui
import (
"bytes"
"fmt"
"io/fs"
"log"
"net"
"net/http"
"os"
"strings"
"sync"
"time"
)
type Option func(*uiConfig) error
type uiConfig struct {
Mode string
Quiet bool
BlurOnClose bool
HistoryMode bool
OpenURL func(string) error
Root fs.FS
AppX int
AppY int
AppWidth int
AppHeight int
// AppChromeArgs []string
// AppChromeBinary string
OnlineAddr string
OnlineListener net.Listener
OnlinePrefix string
OnlineAuth func(http.HandlerFunc) http.HandlerFunc
OnlineCertFile string
OnlineKeyFile string
OnlineAttach HTTPServer
OnlineAttachTLS bool
LocalMapURL func(net.Listener) string
LocalExitDelay *time.Duration
}
func defaultUIConfig() *uiConfig {
return &uiConfig{
Root: defaultRoot,
BlurOnClose: false,
AppX: 200,
AppY: 200,
AppWidth: 1024,
AppHeight: 768,
}
}
//
// General Options
//
// Mode can be override by a 'MODE=xxx' environment variable.
// Default value is page.
func Mode(mod string) Option {
return func(c *uiConfig) error {
switch mod {
case "", modApp, modPage, modOnline, modChrome:
c.Mode = mod
default:
return fmt.Errorf("invalid run mode: %v", mod)
}
return nil
}
}
func Quiet() Option {
return func(c *uiConfig) error {
c.Quiet = true
return nil
}
}
func BlurOnClose(blur bool) Option {
return func(c *uiConfig) error {
c.BlurOnClose = blur
return nil
}
}
// OpenURL is a callback to enable custom frontend.
// If not set, a browser will be opened.
func OpenURL(fn func(string) error) Option {
return func(c *uiConfig) error {
c.OpenURL = fn
return nil
}
}
//
// FileSystem Options
//
func HistoryMode(enable bool) Option {
return func(c *uiConfig) error {
c.HistoryMode = enable
return nil
}
}
func Root(root fs.FS) Option {
return func(c *uiConfig) error {
c.Root = root
return nil
}
}
func RootHtml(html string) Option {
return func(c *uiConfig) error {
c.Root = htmlRoot(html)
return nil
}
}
func RootFiles(files map[string]string) Option {
return func(c *uiConfig) error {
c.Root = NewSimpleRoot(files)
return nil
}
}
//
// App Mode Options
//
func AppWindow(x, y, width, height int) Option {
return func(c *uiConfig) error {
c.AppX, c.AppY = x, y
c.AppWidth, c.AppHeight = width, height
return nil
}
}
// func AppFullScreen() Option {
// return AppChromeArgs("--window-position=0,0", "--window-size=5000,5000", "--start-fullscreen")
// }
// func AppChromeArgs(args ...string) Option {
// return func(c *uiConfig) error {
// c.AppChromeArgs = append(c.AppChromeArgs, args...)
// return nil
// }
// }
// func AppChromeBinary(path string) Option {
// return func(c *uiConfig) error {
// c.AppChromeBinary = path
// return nil
// }
// }
//
// Page Mode Options
//
//
// Online Mode Options
//
func OnlinePort(port int) Option {
return func(c *uiConfig) error {
c.OnlineAddr = fmt.Sprintf(":%d", port)
return nil
}
}
func OnlineAddr(addr string) Option {
return func(c *uiConfig) error {
c.OnlineAddr = addr
return nil
}
}
func OnlineListener(listener net.Listener) Option {
return func(c *uiConfig) error {
c.OnlineListener = listener
return nil
}
}
func OnlinePrefix(prefix string) Option {
return func(c *uiConfig) error {
c.OnlinePrefix = prefix
return nil
}
}
func OnlineAuth(auth func(http.HandlerFunc) http.HandlerFunc) Option {
return func(c *uiConfig) error {
c.OnlineAuth = auth
return nil
}
}
func OnlineTLS(certFile, keyFile string) Option {
return func(c *uiConfig) error {
if certFile == "" || keyFile == "" {
return fmt.Errorf("cert/key file is empty")
}
c.OnlineCertFile = certFile
c.OnlineKeyFile = keyFile
return nil
}
}
func OnlineAttach(existingServer HTTPServer, tls bool) Option {
return func(c *uiConfig) error {
c.OnlineAttach = existingServer
c.OnlineAttachTLS = tls
return nil
}
}
//
// Local Options
//
func LocalMapURL(mapURL func(net.Listener) string) Option {
return func(c *uiConfig) error {
c.LocalMapURL = mapURL
return nil
}
}
// LocalExitDelay contorl auto exit behaviour of a local server.
// By default, when all clients where lost, a local server will wait for a new client and exit if timeout.
// A local server will exit immediately after any client lost when duration is 0,
// and never exit when duration is less than 0.
func LocalExitDelay(d time.Duration) Option {
return func(c *uiConfig) error {
c.LocalExitDelay = &d
return nil
}
}
//
// Simple Http FileSystem
//
const defaultRoot = htmlRoot(`<!DOCTYPE html>
<html>
<head>
<title>Universal User Interface</title>
</head>
<body>
<h1>Hello, Go and TypeScript!</h1>
<script src="/gots.js?name=window"></script>
</body>
</html>
`)
type htmlRoot string
func (r htmlRoot) Open(name string) (fs.File, error) {
return NewHtmlRoot(string(r)).Open(name)
}
type simpleRoot struct {
files map[string]string
listCache []*stringFile
once sync.Once
}
func NewHtmlRoot(html string) *simpleRoot {
files := map[string]string{"index.html": html}
return NewSimpleRoot(files)
}
func NewSimpleRoot(files map[string]string) *simpleRoot {
return &simpleRoot{
files: files,
once: sync.Once{},
}
}
func (r *simpleRoot) Open(name string) (fs.File, error) {
r.once.Do(func() {
cache := make([]*stringFile, 0, len(r.files))
badNames := []string{}
for name, text := range r.files {
if index := strings.LastIndex(name, "/"); index != -1 && index != 0 {
log.Printf("name should not contain '/': %s", name)
}
absName := name
if name == "" || name[0] != '/' {
absName = "/" + name
badNames = append(badNames, name)
}
r.files[absName] = text
cache = append(cache, &stringFile{name: absName, text: text})
}
for _, name := range badNames {
delete(r.files, name)
}
r.listCache = cache
})
switch name {
case "/":
return &stringFile{name: name, children: r.listCache}, nil
default:
text, ok := r.files[name]
if !ok {
return nil, os.ErrNotExist
}
return &stringFile{name: name, text: text}, nil
}
}
type stringFile struct {
name string
text string
children []*stringFile
buf *bytes.Buffer
}
func (s *stringFile) Read(p []byte) (n int, err error) {
if s.buf == nil {
s.buf = bytes.NewBufferString(s.text)
}
return s.buf.Read(p)
}
func (s *stringFile) Close() error {
return nil
}
func (s *stringFile) Seek(offset int64, whence int) (int64, error) {
return 0, http.ErrNotSupported
}
func (s *stringFile) Readdir(count int) ([]os.FileInfo, error) {
if len(s.children) == 0 {
return nil, nil
}
limit := len(s.children)
if count > 0 && limit > count {
limit = count
}
ret := make([]os.FileInfo, limit)
for i := 0; i < limit; i++ {
ret[i] = s.children[i]
}
return ret, nil
}
func (s *stringFile) Stat() (os.FileInfo, error) {
return s, nil
}
// os.FileInfo members
func (s *stringFile) Name() string { return s.name }
func (s *stringFile) Size() int64 { return int64(len(s.text)) }
func (s *stringFile) Mode() os.FileMode { return 0644 }
func (s *stringFile) ModTime() time.Time { return time.Time{} }
func (s *stringFile) IsDir() bool { return s.children != nil }
func (s *stringFile) Sys() interface{} { return nil }