-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
87 lines (73 loc) · 1.91 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
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
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"time"
"github.com/garyburd/redigo/redis"
"github.com/oschwald/geoip2-golang"
"gopkg.in/olivere/elastic.v3"
)
func setupRedis(config string) redis.Conn {
redisClient, err := redis.Dial("tcp", config)
if err != nil {
panic(err)
}
return redisClient
}
func setupElasticsearch(root string, host string) *elastic.Client {
elasticsearchClient, err := elastic.NewClient(
elastic.SetURL("http://"+host),
elastic.SetHealthcheckTimeoutStartup(30*time.Second),
)
if err != nil {
panic(err)
}
exists, err := elasticsearchClient.IndexExists("requestbin").Do()
if err != nil {
panic(err)
}
if !exists {
file, err := ioutil.ReadFile(root + "/requestbin.index.config")
if err != nil {
panic(err)
}
fmt.Println(string(file))
_, err = elasticsearchClient.CreateIndex("requestbin").
BodyString(string(file)).
Do()
if err != nil {
panic(err)
}
}
return elasticsearchClient
}
func main() {
fmt.Println("starting")
root := os.Getenv("ROOT")
redisClient := setupRedis(os.Getenv("REDIS"))
defer redisClient.Close()
db, err := geoip2.Open(root + "/GeoLite2-City.mmdb")
if err != nil {
panic(err)
}
defer db.Close()
elasticsearchClient := setupElasticsearch(root, os.Getenv("ELASTICSEARCH"))
elasticsearchWriter := ElasticsearchRequestWriter{client: elasticsearchClient, GeoIPDB: db}
httpPort, err := strconv.Atoi(os.Getenv("PORT"))
if err != nil {
panic(fmt.Sprintf("Invalid port %s", os.Getenv("PORT")))
}
redisWriter := RedisHttpRequestWriter{client: redisClient}
writers := []HttpRequestWriter{
redisWriter,
elasticsearchWriter,
}
startLoggingHttpServer(httpPort, root+"/static/", writers)
startAdminHttpServer(httpPort+1, root+"/static/", redisClient)
startKibanaProxy(httpPort+2, os.Getenv("KIBANA"), root+"/passwd")
tcpPort := os.Getenv("TCP_PORT")
startTCPServer(tcpPort, elasticsearchWriter)
fmt.Println("started")
}