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

import (
	"fmt"
	"log"
	"os"
	"os/signal"
	"syscall"

	"github.com/gliderlabs/ssh"
)

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()

	// if a state exists, load it
	err := loadState(config)
	if err != nil {
		log.Printf("Could not load the state: %s", err)
	}

	// start the ssh server
	log.Printf("Starting SSH listener on port %d", config.sshPort)
	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 exit handler handing SIGTERM syscalls
	// The handler writes the state (the amount of hits processed until the
	// syscall) to the statefile
	exitHandler(config)

	// start the http server exposing the metrics
	// this is blocking
	setupHTTPServer(config)
}

func exitHandler(config config) {

	// relay incoming signals to the signalChannel
	signalChannel := make(chan os.Signal, 2)
	signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM)

	// wait until a signal is given, then write the state to a file, THEN exit
	go func() {
		<-signalChannel
		fmt.Println("Ending process... Saving the state")

		// write the state to a file
		err := WriteStateToFile(config)
		if err != nil {
			log.Printf("Error writing the stateFile: %s", err)
		}

		os.Exit(0)
	}()
}