-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
146 lines (124 loc) · 3.71 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/nokka/d2-armory-api/internal/character"
"github.com/nokka/d2-armory-api/internal/httpserver"
"github.com/nokka/d2-armory-api/internal/mgo"
"github.com/nokka/d2-armory-api/internal/parsing"
"github.com/nokka/d2-armory-api/internal/statistics"
"github.com/nokka/d2-armory-api/pkg/env"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
func main() {
var (
httpAddress = env.String("HTTP_ADDRESS", ":80")
mongoDBHost = env.String("MONGO_HOST", "mongodb:27017")
databaseName = env.String("MONGO_DB", "armory")
mongoUsername = env.String("MONGO_USERNAME", "")
mongoPassword = env.String("MONGO_PASSWORD", "")
d2sPath = env.String("D2S_PATH", "")
cacheDuration = env.String("CACHE_DURATION", "3m")
statisticsUser = env.String("STATISTICS_USER", "")
statisticsPassword = env.String("STATISTICS_PASSWORD", "")
corsEnabled = env.String("CORS_ENABLED", "false")
logRequests = env.String("LOG_REQUESTS", "false")
)
if d2sPath == "" {
log.Println("d2s path missing")
os.Exit(0)
}
if statisticsUser == "" {
log.Println("statistics credentials user is missing")
os.Exit(0)
}
if statisticsPassword == "" {
log.Println("statistics credentials password is missing")
os.Exit(0)
}
cd, err := time.ParseDuration(cacheDuration)
if err != nil {
log.Printf("failed to parse cache duration, %s", err)
os.Exit(0)
}
cors, err := strconv.ParseBool(corsEnabled)
if err != nil {
log.Printf("failed to parse cors enabled, %s", err)
os.Exit(0)
}
logging, err := strconv.ParseBool(logRequests)
if err != nil {
log.Printf("failed to parse log requests, %s", err)
os.Exit(0)
}
clientOptions := options.Client().ApplyURI("mongodb://" + mongoDBHost)
// If a username is supplied, auth with it.
if mongoUsername != "" {
clientOptions.SetAuth(options.Credential{
AuthSource: databaseName,
Username: mongoUsername,
Password: mongoPassword,
})
}
// Context used for mongo operations, to time them out and cancel their context.
mgoCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err != nil {
log.Println("failed to create context with timeout for mongodb connection", err)
os.Exit(0)
}
client, err := mongo.Connect(mgoCtx, clientOptions)
if err != nil {
log.Println("failed to connect to mongodb", err)
os.Exit(0)
}
err = client.Ping(mgoCtx, readpref.Primary())
if err != nil {
log.Println("failed to ping mongodb", err)
os.Exit(0)
}
log.Println("connected to mongodb")
// Repositories.
characterRepository := mgo.NewCharacterRepository(databaseName, client)
statisticsRepository := mgo.NewStatisticsRepository(databaseName, client)
// Business logic services.
parser := parsing.NewParser(d2sPath)
characterService := character.NewService(parser, characterRepository, cd)
statisticsService := statistics.NewService(statisticsRepository)
// Channel to receive errors on.
errorChannel := make(chan error)
// Credentials for posting statistics map.
credentials := map[string]string{
statisticsUser: statisticsPassword,
}
// HTTP server.
go func() {
httpServer := httpserver.NewServer(
httpAddress,
characterService,
statisticsService,
credentials,
cors,
logging,
)
errorChannel <- httpServer.Open()
}()
// Capture interupts.
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
errorChannel <- fmt.Errorf("got signal %s", <-c)
}()
// Listen for errors indefinitely.
if err := <-errorChannel; err != nil {
os.Exit(1)
}
}