-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.go
110 lines (87 loc) · 3.35 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"app/config"
"app/src"
"app/src/StreetViewImage/Infrastructure/Logger"
"app/src/StreetViewImage/Infrastructure/Server"
"fmt"
"github.com/google/uuid"
"github.com/j7mbo/goenvconfig"
"github.com/j7mbo/goij"
"github.com/j7mbo/goij/src/TypeRegistry"
"github.com/sirupsen/logrus"
"io/ioutil"
)
/*
configToShareWithInjector shares any objects there with the injector in shareConfiguration().
Ensure these are pointers as the call to the goenvconfig lib parser requires pointers to update them with env vars.
[...] instead of [] ensures we get a fixed-size array instead of a slice.
*/
var configToShareWithInjector = [...]interface{}{
&config.ElasticSearchConfiguration{},
&config.GrpcServerConfiguration{},
&config.RedisConfiguration{},
&config.StreetViewApiConfiguration{},
}
/* Here we golang! */
func main() {
ij := Goij.NewInjector(TypeRegistry.New(src.GetRegistry(), src.GetConfigRegistry(), src.GetVendorRegistry()), nil)
/* Injector Configuration. */
shareConfiguration(ij)
shareInjector(ij)
configureLogger(ij)
delegateGrpcMapper(ij)
/* Webserver (for GRPC actually). */
ij.Make("app/src/StreetViewImage/Infrastructure/Server.GrpcServer").(Server.GrpcServer).Run()
}
/* shareInjector shares Goij in case a factory needs access to the injector. */
func shareInjector(injector Goij.Injector) {
injector.Delegate("github.com/j7mbo/goij.Injector", func() Goij.Injector {
return injector
})
}
/* shareConfiguration shares env-parsed configuration objects for automatic injection. */
func shareConfiguration(injector Goij.Injector) {
parser := goenvconfig.NewGoEnvParser()
for _, conf := range configToShareWithInjector {
_ = parser.Parse(conf)
injector.Share(conf)
}
}
/* configureLogger attempts the first application logger and shares any required fields. */
func configureLogger(injector Goij.Injector) {
requiredFields := Logger.RequiredLogFields{Env: "Dev", CorrelationId: uuid.New()}
/* The UUID here will be overwritten with one from the request. We can't have a UUID here. */
injector.Share(requiredFields)
elasticLogger := injector.Make(
"app/src/StreetViewImage/Infrastructure/Logger.ElasticSearchLogger",
).(*Logger.ElasticSearchLogger)
injector.Share(elasticLogger)
backupLogFile, err := ioutil.TempFile("/tmp", "palmago_streetview_log_")
if err != nil {
panic(err)
}
fmt.Println("Backup log file located at: " + backupLogFile.Name())
fileLogger, err := Logger.NewFileLogger(*backupLogFile, *logrus.New(), requiredFields)
if err != nil {
panic(err)
}
/*
Atm, we have to do this instead of Share() because Share() is not the first-used choice for the injector
(maybe it should be?)...
*/
injector.Delegate("app/src/StreetViewImage/Infrastructure/Logger.LoggingStrategy", func() *Logger.LoggingStrategy {
return Logger.NewLoggingStrategy(*elasticLogger, *fileLogger)
})
}
/*
delegateGrpcMapper delegates the initialisation of the controller interface to the Server.GerpcErrorMapper - this is
used to avoid cyclic dependencies from app -> controller -> server -> controller. Fuck sake.
*/
func delegateGrpcMapper(injector Goij.Injector) {
injector.Delegate("app/src/StreetViewImage/Presentation/Controller.GrpcErrorMapper", func() Server.GrpcErrorMapper {
return Server.NewGrpcErrorMapper(
injector.Make("app/src/StreetViewImage/Infrastructure/Logger.LoggingStrategy").(*Logger.LoggingStrategy),
)
})
}