forked from sdgdsffdsfff/cat.go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfs.go
77 lines (68 loc) · 1.62 KB
/
confs.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
package cat
import (
"fmt"
"io/ioutil"
"net"
"net/http"
. "os"
"strconv"
"strings"
)
//Configs about cat is required to be correct.
//The client is able to complement some of the configs,
//but is incapable of validation.
//Fortunately invalid configs causes failure of cat's init,
//thus users can be immediately aware of config fault.
var (
PROD string = "http://cat.ctripcorp.com"
FAT string = "http://cat.fws.qa.nt.ctripcorp.com"
UAT string = "http://cat.uat.qa.nt.ctripcorp.com"
CAT_HOST string = FAT
CAT_SERVERS []string
DOMAIN string = "900407"
HOSTNAME string = ""
IP string = ""
TEMPFILE string = ".cat"
)
func cat_config_init() {
var err error
var resp *http.Response
var metaServer string
metaServer = fmt.Sprintf(CAT_HOST+"/cat/s/router?domain=%s", DOMAIN)
resp, err = http.Get(metaServer)
if err != nil {
panic("Fail to access the cat meta server.")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
CAT_SERVERS = strings.Split(string(body), ";")
if DOMAIN == "" {
panic("DOMAIN is required, set your appid to it.")
}
if HOSTNAME == "" {
HOSTNAME, err = Hostname()
if err != nil {
panic("Fail to auto-get HOSTNAME, try config it manually.")
}
}
if IP == "" {
IP = cat_get_ip()
}
}
func cat_get_ip() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, addr := range addrs {
add := strings.Split(addr.String(), "/")[0]
if add == "127.0.0.1" || add == "::1" {
continue
}
first := strings.Split(add, ".")[0]
if _, err := strconv.Atoi(first); err == nil {
return add
}
}
return ""
}