-
Notifications
You must be signed in to change notification settings - Fork 3
/
web.go
55 lines (45 loc) · 1.05 KB
/
web.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
package main
import (
"fmt"
"net/http"
"os/exec"
"runtime"
)
// WebServer represents a simple web server
type WebServer struct {
static http.Handler
}
// NewWebServer creates a new WebServer instance
func NewWebServer(dir string) (*WebServer, error) {
ws := &WebServer{}
s := http.FileServer(http.Dir(dir))
ws.static = s
return ws, nil
}
// Bind starts the web server listening on the given address / port
func (ws *WebServer) Bind(bind string) {
http.ListenAndServe(bind, ws.static)
}
// Run starts th web server listening on port 8080
func (ws *WebServer) Run() {
ws.Bind(":8080")
}
func (ws *WebServer) OpenBrowser() {
var command string
var args []string
switch runtime.GOOS {
case "windows":
command = "cmd"
args = []string{"/c", "start"}
case "darwin":
command = "open"
default:
command = "xdg-open"
}
args = append(args, "http://127.0.0.1:8080")
err := exec.Command(command, args...).Start()
if err != nil {
fmt.Println("Error launching your web browser...")
fmt.Println("Please manually go to http://127.0.0.1:8080")
}
}