-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
82 lines (68 loc) · 1.66 KB
/
app.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
package main
import (
"database/sql"
"errors"
"fmt"
"github.com/kneu-messenger-pigeon/events"
"github.com/kneu-messenger-pigeon/fileStorage"
_ "github.com/nakagami/firebirdsql"
"github.com/segmentio/kafka-go"
"io"
"os"
)
const ExitCodeMainError = 1
const ExitCodeLoopIsBroken = 2
const ExitCodeTooManyErrorInLoop = 3
func runApp(out io.Writer) error {
envFilename := ""
if _, err := os.Stat(".env"); err == nil {
envFilename = ".env"
}
config, err := loadConfig(envFilename)
if err != nil {
return errors.New("Failed to load config: " + err.Error())
}
eventbus := MetaEventbus{
out: out,
writer: &kafka.Writer{
Addr: kafka.TCP(config.kafkaHost),
Topic: events.MetaEventsTopic,
Balancer: &kafka.LeastBytes{},
},
}
storage := fileStorage.Storage{
File: config.storageFile,
}
secondaryDekanatDb, err := sql.Open(config.dekanatDbDriverName, config.secondaryDekanatDbDSN)
if err != nil {
return errors.New("Wrong connection configuration for secondary Dekanat DB: " + err.Error())
}
defer func() {
eventbus.writer.Close()
secondaryDekanatDb.Close()
}()
_, err = storage.Get()
if err != nil {
return errors.New(fmt.Sprintf(
"Failed to load storage file %s - %s \n", config.storageFile, err,
))
}
return runMainLoop(config, out, func() error {
return checkDekanatDb(secondaryDekanatDb, &storage, &eventbus)
})
}
func handleExitError(errStream io.Writer, err error) int {
if err != nil {
fmt.Fprintln(errStream, err)
}
if errors.Is(err, TooManyError) {
return ExitCodeTooManyErrorInLoop
}
if errors.Is(err, BreakLoopError) {
return ExitCodeLoopIsBroken
}
if err != nil {
return ExitCodeMainError
}
return 0
}