Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
81 lines (74 loc) · 1.76 KB
/
Copy pathmain.go
File metadata and controls
81 lines (74 loc) · 1.76 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
package main
import (
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"time"
"github.com/gobuffalo/packr"
"github.com/pkg/errors"
"github.com/zserge/webview"
)
var webviewSettings = webview.Settings{
Title: "Desktop by Machine Box",
Width: 650,
Height: 350,
Resizable: true,
ExternalInvokeCallback: handleClientInvoke,
}
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "%v", err)
os.Exit(2)
}
}
// handleClientInvoke handles client-side calls to
// window.external.invoke.
func handleClientInvoke(w webview.WebView, data string) {
webview.Debug("TODO: handleClientInvoke: ", data)
}
func run() error {
const endpoint = "0.0.0.0:0"
debug := os.Getenv("MB_DESKTOP_DEBUG") == "true"
box := packr.NewBox("./www")
http.HandleFunc("/time", handleTime)
http.Handle("/", http.FileServer(box))
ln, err := net.Listen("tcp", endpoint)
if err != nil {
return errors.Wrap(err, "net.Listen")
}
go func() {
if err := http.Serve(ln, nil); err != nil {
fmt.Fprintf(os.Stderr, "%v", err)
os.Exit(2)
}
}()
addr := "http://" + ln.Addr().String()
log.Println("serving via", addr)
client := http.Client{Timeout: 100 * time.Millisecond}
for {
time.Sleep(100 * time.Millisecond)
resp, err := client.Get(addr)
if err != nil {
continue
}
resp.Body.Close()
if resp.StatusCode == http.StatusOK {
break
}
}
webviewSettings.URL = addr
webviewSettings.Debug = debug
win := webview.New(webviewSettings)
win.Run()
return nil
}
// handleTime handles the /time endpoint.
// It is used to demonstrate an API call from app.js.
func handleTime(w http.ResponseWriter, r *http.Request) {
if _, err := io.WriteString(w, time.Now().Format(time.RFC822)); err != nil {
log.Println(err)
}
}