forked from stephendltg/go-webview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
124 lines (106 loc) · 2.68 KB
/
main.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
package main
import (
"flag"
"fmt"
"net"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/webview/webview"
)
// Vars cli
var (
title = flag.String("title", "broz", "Webview title")
debug = flag.Bool("debug", false, "Mode debug")
height = flag.Int("height", 600, "Webview height")
width = flag.Int("width", 800, "Webview width")
url = flag.String("url", "", "Navigate to this url")
dir = flag.String("dir", ".", "path to serve")
)
// Init
func init() {
// Parse cli
flag.Parse()
}
// Get port available
func getport() (int, net.Listener) {
listener, err := net.Listen("tcp", ":0")
if err != nil {
panic(err)
}
port := listener.Addr().(*net.TCPAddr).Port
return port, listener
}
// Handle http server
func handle(fs http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Referrer-Policy", "0")
// Cache
w.Header().Add("Surrogate-Control", "no-store")
w.Header().Add("Cache-Control", "no-store, no-cache, must-revalidate, proxy-revalidate")
w.Header().Add("Pragma", "no-cache")
w.Header().Add("Expires", "0")
// Security
w.Header().Add("X-Content-Type-Options", "nosniff")
w.Header().Add("X-DNS-Prefetch-Control", "off")
w.Header().Add("X-Frame-Options", "DENY")
w.Header().Add("Strict-Transport-Security", "5184000")
w.Header().Add("X-Download-Options", "noopen")
w.Header().Add("X-XSS-Protection", "1; mode=block")
fs.ServeHTTP(w, r)
}
}
// static server
func fileServer(listener net.Listener, path string) {
http.Handle("/", handle(http.FileServer(http.Dir(path))))
panic(http.Serve(listener, nil))
}
// Absolu path exec
func abspath() string {
exe, err := os.Executable()
if err != nil {
panic(err)
}
exe, err = filepath.EvalSymlinks(exe)
if err != nil {
panic(err)
}
dir := filepath.Dir(exe)
return dir
}
// Run webview
func runWebview(url string, title string, width int, height int, debug bool) {
w := webview.New(debug)
defer w.Destroy()
w.SetTitle(title)
w.SetSize(width, height, webview.HintNone)
w.Navigate(url)
// Func close webview for js
w.Bind("close", func() {
os.Exit(0)
})
w.Run()
}
func main() {
port, listener := getport()
var path string
if strings.HasPrefix(*dir, "./") || strings.HasPrefix(*dir, "../") || strings.HasPrefix(*dir, ".\\") || strings.HasPrefix(*dir, "..\\") {
path = filepath.Join(abspath(), *dir)
} else {
path = *dir
}
if *url == "" {
go fileServer(listener, path)
url := fmt.Sprintf("http://localhost:%d/", port)
runWebview(url, *title, *width, *height, *debug)
} else {
var uri string
if !strings.Contains(*url, "://") {
uri = "http://" + *url
} else {
uri = *url
}
runWebview(uri, *title, *width, *height, *debug)
}
}