-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
59 lines (52 loc) · 1.23 KB
/
server.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
package main
import(
"fmt"
"net/http"
"io/ioutil"
"gopkg.in/yaml.v2"
"gopkg.in/redis.v2"
)
type ServerConfig struct {
Port string
Redis string
Servers map[string][]string
}
var(
server_config = ServerConfig{}
redis_client *redis.Client
)
func LoadConfig() {
buf, err := ioutil.ReadFile("config.yml")
if err != nil {
fmt.Println("error: %v", err)
}
err = yaml.Unmarshal(buf, &server_config)
if err != nil {
fmt.Println("error: %v", err)
}
}
func ConnectRedis() {
redis_client = redis.NewTCPClient(&redis.Options{
Addr: server_config.Redis,
Password: "", // no password set
DB: 0, // use default DB
})
}
func SetServers() {
for server_name, server_params := range server_config.Servers {
redis_client.Set("servers:" + server_name + ":ip", server_params[0])
redis_client.Set("servers:" + server_name + ":ssh_user", server_params[1])
fmt.Println("Node " + server_name + " published in redis store")
}
}
func main() {
LoadConfig()
ConnectRedis()
SetServers()
http.HandleFunc("/status", status)
http.HandleFunc("/api", api_handler)
http.HandleFunc("/ps", ps)
http.HandleFunc("/metrics", metrics)
fmt.Println("Server running on port", server_config.Port)
http.ListenAndServe(":" + server_config.Port, nil)
}