about summary refs log tree commit diff
path: root/src/main.go
blob: 674e38cd52d3eb9f01a2e09bbc9ca557c35a03b5 (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
76
77
78
79
80
81
package main

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

	"git.sr.ht/~hanemile/faila/src/http"
	"git.sr.ht/~hanemile/faila/src/structs"
	"github.com/sirupsen/logrus"
	"github.com/spf13/viper"
)

func main() {
	initConfig()
	initLogging()

	go ExitHandler()

	http.Server()
}

// initialize the config (config.yml)
func initConfig() {
	// parse the config
	viper.SetConfigName("config")
	viper.AddConfigPath(".")
	viper.AddConfigPath("/go/src/app/")
	viper.AutomaticEnv()

	// Fix nested keys don't work with .
	viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))

	// read the config
	err := viper.ReadInConfig()
	if err != nil {
		panic(fmt.Errorf("Fatal error config file: %s", err))
	}

	// unmarshal the config
	var conf structs.Configuration
	err = viper.Unmarshal(&conf)
	if err != nil {
		log.Fatalf("unable to decode into struct, %v", err)
	}
}

// Initialize the loglevel using the value given in the config
func initLogging() {

	switch viper.GetString("verbose.level") {
	case "1":
		logrus.SetLevel(logrus.PanicLevel)
	case "2":
		logrus.SetLevel(logrus.FatalLevel)
	case "3":
		logrus.SetLevel(logrus.ErrorLevel)
	case "4":
		logrus.SetLevel(logrus.WarnLevel)
	case "5":
		logrus.SetLevel(logrus.InfoLevel)
	case "6":
		logrus.SetLevel(logrus.DebugLevel)
	case "7":
		logrus.SetLevel(logrus.TraceLevel)
	}

}

// ExitHandler handles exit signals such as ^C
func ExitHandler() {
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt, syscall.SIGTERM)
	go func() {
		<-c
		os.Exit(1)
	}()
}