-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
56 lines (47 loc) · 1.01 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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"strconv"
)
var (
ConfigSource string
WebPort int
WebPath string
)
func init() {
flag.StringVar(&ConfigSource, "c", "default", "config source default or env.")
flag.IntVar(&WebPort, "port", 5002, "web port.")
flag.StringVar(&WebPath, "path", "./dist", "web path.")
flag.Parse()
if ConfigSource == "env" {
WebPort = getEnvInt("WEB_PORT", WebPort)
WebPath = getEnvString("WEB_PATH", WebPath)
}
}
func main() {
fmt.Printf("File Server Port:%d Path:%s", WebPort, WebPath)
http.Handle("/", http.FileServer(http.Dir(WebPath)))
err := http.ListenAndServe(fmt.Sprintf(":%d", WebPort), nil)
if err != nil {
fmt.Printf(err.Error())
}
}
func getEnvString(name string, value string) string {
ret := os.Getenv(name)
if ret == "" {
return value
} else {
return ret
}
}
func getEnvInt(name string, value int) int {
env := os.Getenv(name)
if ret, err := strconv.Atoi(env); env == "" || err != nil {
return value
} else {
return ret
}
}