-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
57 lines (54 loc) · 1.21 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
package main
import (
"io/ioutil"
"log"
"net/http"
"os"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello world 2\n \r"))
hostname := os.Getenv("HOSTNAME")
w.Write([]byte("HOSTNAME:" + hostname + "\r \n"))
cookies, err := r.Cookie("env")
if err != nil {
// emm 没设置cookies 那就 读dev conf吧
config, err := ioutil.ReadFile("./configs/dev.json") // just pass the file name
if err != nil {
w.Write([]byte("\n \r"))
w.Write([]byte(err.Error()))
}
w.Write(config)
return
}
cookiesEnv := cookies.Value
config, err := loadConfig(cookiesEnv)
if err != nil {
w.Write([]byte("\n \r"))
w.Write([]byte(err.Error()))
return
}
w.Write([]byte("\n \r"))
w.Write(config)
})
log.Println("run at :8801")
http.ListenAndServe(":8801", nil)
}
func loadConfig(cookiesEnv string) ([]byte, error) {
var path string
switch cookiesEnv {
case "test":
path = "./configs/test.json"
case "prod":
path = "./configs/prod.json"
case "stage":
path = "./configs/stage.json"
default:
path = "./configs/dev.json"
}
b, err := ioutil.ReadFile(path) // just pass the file name
if err != nil {
return nil, err
}
return b, nil
}