This repository has been archived by the owner on Apr 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
86 lines (78 loc) · 2.99 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
// Copyright 2021 AI Redefined Inc. <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"net"
"github.com/spf13/viper"
"github.com/cogment/cogment-trial-datastore/backend"
"github.com/cogment/cogment-trial-datastore/backend/boltBackend"
"github.com/cogment/cogment-trial-datastore/backend/memoryBackend"
"github.com/cogment/cogment-trial-datastore/grpcservers"
"github.com/cogment/cogment-trial-datastore/version"
log "github.com/sirupsen/logrus"
)
func main() {
viper.AutomaticEnv()
viper.SetDefault("PORT", 9000)
viper.SetDefault("GRPC_REFLECTION", false)
viper.SetDefault("LOG_LEVEL", "info")
viper.SetDefault("MEMORY_STORAGE_MAX_SAMPLE_SIZE", memoryBackend.DefaultMaxSampleSize)
viper.SetDefault("FILE_STORAGE_PATH", nil)
viper.SetEnvPrefix("COGMENT_TRIAL_DATASTORE")
logLevel, err := log.ParseLevel(viper.GetString("LOG_LEVEL"))
if err != nil {
expectedLevels := make([]string, 0)
for _, level := range log.AllLevels {
expectedLevels = append(expectedLevels, level.String())
}
log.Fatalf("invalid log level specified %q expecting one of %v", viper.GetString("LOG_LEVEL"), expectedLevels)
}
log.Infof("setting up log level to %q", logLevel.String())
log.SetLevel(logLevel)
var backend backend.Backend
if viper.IsSet("FILE_STORAGE_PATH") {
storageFilePath := viper.GetString("FILE_STORAGE_PATH")
log.Infof("using a file storage backend in %q", storageFilePath)
backend, err = boltBackend.CreateBoltBackend(storageFilePath)
if err != nil {
log.Fatalf("unable to create the bolt file backend: %v", err)
}
} else {
log.Info("using an in-memory storage")
backend, err = memoryBackend.CreateMemoryBackend(viper.GetUint32("MEMORY_STORAGE_MAX_SAMPLE_SIZE"))
if err != nil {
log.Fatalf("unable to create the memory backend: %v", err)
}
}
port := viper.GetInt("PORT")
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
log.Fatalf("unable to listen to tcp port %d: %v", port, err)
}
server := grpcservers.CreateGrpcServer(viper.GetBool("GRPC_REFLECTION"))
err = grpcservers.RegisterTrialDatastoreServer(server, backend)
if err != nil {
log.Fatalf("%v", err)
}
err = grpcservers.RegisterDatalogServer(server, backend)
if err != nil {
log.Fatalf("%v", err)
}
log.WithField("port", port).WithField("version", version.Version).Info("Cogment Trial Datastore service starts...\n")
err = server.Serve(listener)
if err != nil {
log.Fatalf("unexpected error while serving grpc services: %v", err)
}
}