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

import (
	"fmt"
	"log"
	"net/http"

	"github.com/gliderlabs/ssh"
	"github.com/gorilla/mux"
)

var (
	metricsNumPasswords int
	metricsCityNum      map[string]int
	cities              map[string]location
)

func main() {
	// create a map mapping a city to an amount of hits
	metricsCityNum = make(map[string]int)

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

	// parse flags building a config struct
	config := parseFlags()

	// start the ssh server
	log.Println("Starting SSH listener")
	go func() {
		sshPortString := fmt.Sprintf(":%d", config.sshPort)
		listenErr := ssh.ListenAndServe(sshPortString, nil, ssh.PasswordAuth(handlePass))
		if listenErr != nil {
			log.Fatalln(listenErr.Error())
		}
	}()

	// start the http server logging the metrics
	log.Println("Starting HTTP metrics listener")

	r := mux.NewRouter()
	r.HandleFunc("/", indexHandler)
	r.HandleFunc("/metrics", metricsHandler)
	r.HandleFunc("/locations", locationHandlerEndpoint)

	// start the http server exposing the metrics and the locations
	httpPortString := fmt.Sprintf(":%d", config.httpPort)
	listenErr := http.ListenAndServe(httpPortString, r)

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