-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
170 lines (146 loc) · 5.92 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2022 Board of Trustees of the University of Illinois.
//
// 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 (
"content/core"
"content/core/model"
"content/driven/awsstorage"
cacheadapter "content/driven/cache"
corebb "content/driven/core"
storage "content/driven/storage"
"content/driven/twitter"
driver "content/driver/web"
"log"
"os"
"strconv"
"strings"
"github.com/golang-jwt/jwt"
"github.com/rokwire/core-auth-library-go/v2/authservice"
"github.com/rokwire/core-auth-library-go/v2/sigauth"
"github.com/rokwire/logging-library-go/v2/logs"
)
var (
// Version : version of this executable
Version string
// Build : build date of this executable
Build string
)
func main() {
if len(Version) == 0 {
Version = "dev"
}
serviceID := "content"
loggerOpts := logs.LoggerOpts{SuppressRequests: logs.NewStandardHealthCheckHTTPRequestProperties(serviceID + "/version")}
logger := logs.NewLogger(serviceID, &loggerOpts)
port := getEnvKey("CONTENT_PORT", true)
//common
coreBBHost := getEnvKey("CONTENT_CORE_BB_HOST", true)
contentServiceURL := getEnvKey("CONTENT_SERVICE_URL", true)
authService := authservice.AuthService{
ServiceID: serviceID,
ServiceHost: contentServiceURL,
FirstParty: true,
AuthBaseURL: coreBBHost,
}
serviceRegLoader, err := authservice.NewRemoteServiceRegLoader(&authService, []string{"auth"})
if err != nil {
log.Fatalf("Error initializing remote service registration loader: %v", err)
}
serviceRegManager, err := authservice.NewServiceRegManager(&authService, serviceRegLoader)
if err != nil {
log.Fatalf("Error initializing service registration manager: %v", err)
}
//end common
//mongoDB adapter
mongoDBAuth := getEnvKey("CONTENT_MONGO_AUTH", true)
mongoDBName := getEnvKey("CONTENT_MONGO_DATABASE", true)
mongoTimeout := getEnvKey("CONTENT_MONGO_TIMEOUT", false)
storageAdapter := storage.NewStorageAdapter(mongoDBAuth, mongoDBName, mongoTimeout, logger)
err = storageAdapter.Start()
if err != nil {
log.Fatal("Cannot start the mongoDB adapter - " + err.Error())
}
// S3 Adapter
s3Bucket := getEnvKey("CONTENT_S3_BUCKET", true)
s3ProfileImagesBucket := getEnvKey("CONTENT_S3_PROFILE_IMAGES_BUCKET", true)
s3UsersAudiosBucket := getEnvKey("CONTENT_S3_USERS_AUDIOS_BUCKET", true)
s3Region := getEnvKey("CONTENT_S3_REGION", true)
awsAccessKeyID := getEnvKey("CONTENT_AWS_ACCESS_KEY_ID", true)
awsSecretAccessKey := getEnvKey("CONTENT_AWS_SECRET_ACCESS_KEY", true)
awsConfig := &model.AWSConfig{S3Bucket: s3Bucket,
S3ProfileImagesBucket: s3ProfileImagesBucket,
S3UsersAudiosBucket: s3UsersAudiosBucket,
S3Region: s3Region, AWSAccessKeyID: awsAccessKeyID, AWSSecretAccessKey: awsSecretAccessKey}
presignExpirationMinutesVal := getEnvKey("CONTENT_S3_REQUEST_PRESIGN_EXPIRATION_MINUTES", false)
presignExpirationMinutes, err := strconv.Atoi(presignExpirationMinutesVal)
if err != nil {
logger.Warnf("error parsing S3 request presign expiration minutes: %s - applying default", err.Error())
}
awsAdapter := awsstorage.NewAWSStorageAdapter(awsConfig, presignExpirationMinutes)
defaultCacheExpirationSeconds := getEnvKey("CONTENT_DEFAULT_CACHE_EXPIRATION_SECONDS", false)
cacheAdapter := cacheadapter.NewCacheAdapter(defaultCacheExpirationSeconds)
twitterFeedURL := getEnvKey("CONTENT_TWITTER_FEED_URL", true)
twitterAccessToken := getEnvKey("CONTENT_TWITTER_ACCESS_TOKEN", true)
twitterAdapter := twitter.NewTwitterAdapter(twitterFeedURL, twitterAccessToken)
mtAppID := getEnvKey("CONTENT_MULTI_TENANCY_APP_ID", true)
mtOrgID := getEnvKey("CONTENT_MULTI_TENANCY_ORG_ID", true)
//core adapter
serviceAccountID := getEnvKey("CONTENT_SERVICE_ACCOUNT_ID", false)
privKeyRaw := getEnvKey("CONTENT_PRIV_KEY", true)
privKeyRaw = strings.ReplaceAll(privKeyRaw, "\\n", "\n")
privKey, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(privKeyRaw))
if err != nil {
log.Fatalf("Error parsing priv key: %v", err)
}
signatureAuth, err := sigauth.NewSignatureAuth(privKey, serviceRegManager, false)
if err != nil {
log.Fatalf("Error initializing signature auth: %v", err)
}
serviceAccountLoader, err := authservice.NewRemoteServiceAccountLoader(&authService, serviceAccountID, signatureAuth)
if err != nil {
log.Fatalf("Error initializing remote service account loader: %v", err)
}
serviceAccountManager, err := authservice.NewServiceAccountManager(&authService, serviceAccountLoader)
if err != nil {
log.Fatalf("Error initializing service account manager: %v", err)
}
coreAdapter := corebb.NewCoreAdapter(coreBBHost, serviceAccountManager)
// application
application := core.NewApplication(Version, Build, storageAdapter, awsAdapter, twitterAdapter, cacheAdapter, mtAppID, mtOrgID, serviceID, coreAdapter, logger)
application.Start()
// web adapter
webAdapter := driver.NewWebAdapter(contentServiceURL, port, application, serviceRegManager, logger)
webAdapter.Start()
}
func getEnvKeyAsList(key string, required bool) []string {
stringValue := getEnvKey(key, required)
// it is comma separated format
stringListValue := strings.Split(stringValue, ",")
if len(stringListValue) == 0 && required {
log.Fatalf("missing or empty env var: %s", key)
}
return stringListValue
}
func getEnvKey(key string, required bool) string {
// get from the environment
value, exist := os.LookupEnv(key)
if !exist {
if required {
log.Fatal("No provided environment variable for " + key)
} else {
log.Printf("No provided environment variable for " + key)
}
}
return value
}