about summary refs log tree commit diff
path: root/main.go
blob: ee3938e01555accf5afbcbe77c41c239ffadc199 (plain)
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
package main

import (
	"encoding/json"
	"fmt"
	"github.com/gliderlabs/ssh"
	//"github.com/gorilla/mux"
	"io/ioutil"
	"log"
	"net/http"
	"strings"
)

var (
	metrics_num_passwords int
	metrics_city_num      map[string]int
	cities                map[string]location
)

type geoipresult struct {
	Query       string  `json:"query"`
	Status      string  `json:"status"`
	Country     string  `json:"country"`
	CountryCode string  `json:"countryCode"`
	Region      string  `json:"region"`
	RegionName  string  `json:"regionName"`
	City        string  `json:"city"`
	Zip         string  `json:"zip"`
	Lat         float64 `json:"lat"`
	Lon         float64 `json:"lon"`
	Timezone    string  `json:"timezone"`
	Isp         string  `json:"isp"`
	Org         string  `json:"org"`
	As          string  `json:"as"`
}

type location struct {
	key       string  `json:"key"`
	latitude  float64 `json:"latitude"`
	longitude float64 `json:"longitude"`
	name      string  `json:"name"`
}

func main() {

	// create a map mapping a city to an amount of hits
	metrics_city_num = make(map[string]int)

	// create a cities map mapping a city to a location
	cities = make(map[string]location)

	// start the ssh server
	log.Println("Starting SSH listener")
	go func() {
		listenErr := ssh.ListenAndServe(":2222", nil, ssh.PasswordAuth(handlePass))
		if listenErr != nil {
			log.Fatalln(listenErr.Error())
		}
	}()

	// start the http server logging the metrics
	log.Println("Starting HTTP metrics listener")
	http.HandleFunc("/", indexHandler)
	http.HandleFunc("/metrics", metricsHandler)
	http.HandleFunc("/locations", locationHandlerEndpoint)

	// start the http server exposing the metrics and the locations
	listenErr := http.ListenAndServe(":8084", nil)

	// handle potential errors
	if listenErr != nil {
		log.Fatalln(listenErr.Error())
	}
}