/
webstatic.go
49 lines (43 loc) · 1.18 KB
/
webstatic.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
package main
import (
"fmt"
"log"
"net/http"
"os/exec"
"runtime"
"github.com/rakyll/statik/fs"
// initialize binary data in statik dir
_ "github.com/eyetoe/webstatic/statik"
)
// Before buildling, run `statik -src=./data`
// to generate the statik package.
// Note! when updating contents of ./data delete the ./statik dir so it can be recreated
func main() {
statikFS, err := fs.New()
if err != nil {
log.Fatalf(err.Error())
}
// Serve the data from the root
http.Handle("/", http.FileServer(statikFS))
// Serve the data from the ./assets/ here as an example
http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(statikFS)))
if nope := 1; nope != 1 {
openURL("http://localhost:8080/")
}
fmt.Println("listening on TCP:8080")
http.ListenAndServe("0.0.0.0:8080", nil)
}
func openURL(url string) error {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", "http://localhost:4001/").Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("Cannot open URL %s on this platform", url)
}
return err
}