/
main.go
98 lines (74 loc) · 2.47 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
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"os"
"os/signal"
"gitlab.com/piLights/dioder"
"gitlab.com/piLights/dioder-rpc/configuration"
"gitlab.com/piLights/dioder-rpc/logging"
"gitlab.com/piLights/dioder-rpc/piLightsVersion"
"gitlab.com/piLights/dioder-rpc/rpc"
"github.com/urfave/cli"
)
func main() {
// Report crashes
//defer captureCrash()
application := cli.NewApp()
application.Name = "Dioder-Server"
application.Version = piLightsVersion.Version
application.Flags = applicationFlags
application.Action = func(c *cli.Context) error {
logging.NewLoggingService()
go logging.Service()
//Check, if we should update
if c.Bool("update") {
startUpdate()
return nil
}
if c.String("writeConfiguration") != "" {
fmt.Println(configuration.DioderConfiguration)
error := configuration.DioderConfiguration.WriteConfigurationToFile(c.String("writeConfiguration"))
return error
}
if c.String("configurationFile") != "" {
config, err := configuration.NewConfiguration(c.String("configurationFile"))
if err != nil {
return err
}
configuration.DioderConfiguration = config
}
//Set the pins
if configuration.DioderConfiguration.Debug {
logging.LogChan <- fmt.Sprintf("Configuring the Pins to: Red: %s, Green: %s, Blue: %s", configuration.DioderConfiguration.Pins.Red, configuration.DioderConfiguration.Pins.Green, configuration.DioderConfiguration.Pins.Blue)
}
if !configuration.DioderConfiguration.NoAutoconfiguration {
go startAutoConfigurationServer()
}
configuration.DioderConfiguration.DioderInstance = dioder.New(dioder.Pins{Red: configuration.DioderConfiguration.Pins.Red, Green: configuration.DioderConfiguration.Pins.Green, Blue: configuration.DioderConfiguration.Pins.Blue}, configuration.DioderConfiguration.PiBlaster)
//Handle CTRL + C
osSignalChan := make(chan os.Signal, 1)
signal.Notify(osSignalChan, os.Interrupt)
go func() {
<-osSignalChan
// Close channels
close(osSignalChan)
close(logging.LogChan)
close(logging.FatalChan)
// Release all Pins for further use
configuration.DioderConfiguration.DioderInstance.Release()
os.Exit(0)
}()
rpc.StartServer()
defer configuration.DioderConfiguration.DioderInstance.Release()
return nil
}
application.Run(os.Args)
}
//hashPassword hashes the defined password with SHA256
func hashPassword(password string) string {
hasher := sha256.New()
hasher.Write([]byte(password))
return hex.EncodeToString(hasher.Sum(nil))
}