-
Notifications
You must be signed in to change notification settings - Fork 0
/
sfs.go
88 lines (71 loc) · 1.89 KB
/
sfs.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
package main
import (
"fmt"
"flag"
"net/http"
"log"
"os/exec"
"runtime"
)
type ConfigParams struct {
port *string
folder *string
}
func main() {
params := getConfigParams()
go serveStaticContent(params)
openRootFolderInBrowser(params)
waitUntilKeyPress()
}
func getConfigParams() *ConfigParams {
configParams := &ConfigParams{}
configParams.port = flag.String("port", "8080", "The port in wich to run the static file web server")
configParams.folder = flag.String("folder", "./www", "The name of the folder to be served")
flag.Parse()
return configParams
}
func serveStaticContent(params *ConfigParams) {
http.Handle("/", http.FileServer(http.Dir(*params.folder)))
if err := http.ListenAndServe(":" + *params.port, decorateDefaultHttpHandler()); err != nil {
log.Fatal("ListenAndServe:", err)
}
}
func openRootFolderInBrowser(params *ConfigParams) {
openbrowser("http://localhost:" + *params.port)
}
func waitUntilKeyPress() {
fmt.Println("Press <Enter> to stop serving files...")
var end string
fmt.Scanln(&end)
}
func decorateDefaultHttpHandler() http.Handler {
return noCacheHandler(http.DefaultServeMux)
}
func noCacheHandler(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Header.Del("If-None-Match")
r.Header.Del("If-Range")
r.Header.Del("If-Modified-Since")
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
if r.Proto == "HTTP/1.0" {
w.Header().Set("Pragma", "no-cache")
}
handler.ServeHTTP(w, r)
})
}
func openbrowser(url string) {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
if err != nil {
log.Fatal(err)
}
}