-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmain.go
63 lines (56 loc) · 1.13 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"runtime/debug"
)
var (
dir string
port int
version bool
open bool
)
func init() {
flag.IntVar(&port, "p", 8100, "port to serve on")
flag.StringVar(&dir, "path", "./", "the directory of static file to host")
flag.BoolVar(&version, "version", false, "print program version")
flag.BoolVar(&open, "open", false, "open with default browser")
flag.Parse()
if version {
info, ok := debug.ReadBuildInfo()
if ok {
println(info.Main.Version, info.Main.Sum)
}
os.Exit(0)
}
if dir == "./" {
d, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
dir = d
}
}
func main() {
if open {
go func() {
if err := Open(fmt.Sprintf("http://%s:%d", IP(), port)); err != nil {
log.Printf("can not open default browser %s", err)
}
}()
}
http.Handle("/", FileServer(Dir(abs(dir))))
log.Printf("Serving %s on HTTP port: %d\n", abs(dir), port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
}
func abs(dir string) string {
if filepath.IsAbs(dir) {
return dir
}
pwd, _ := os.Getwd()
return filepath.Join(pwd, dir)
}