-
Notifications
You must be signed in to change notification settings - Fork 0
/
env-service.go
111 lines (87 loc) · 3.65 KB
/
env-service.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
// Package configuration implements configuration service required by the user service
package configuration
import (
"os"
"strconv"
"strings"
commonErrors "github.com/micro-business/go-core/system/errors"
)
type envConfigurationService struct {
}
// NewEnvConfigurationService creates new instance of the EnvConfigurationService, setting up all dependencies and returns the instance
// Returns the new service or error if something goes wrong
func NewEnvConfigurationService() (ConfigurationContract, error) {
return &envConfigurationService{}, nil
}
// GetGrpcHost retrieves the gRPC host name
// Returns the gRPC host name or error if something goes wrong
func (service *envConfigurationService) GetGrpcHost() (string, error) {
return os.Getenv("GRPC_HOST"), nil
}
// GetGrpcPort retrieves the gRPC port number
// Returns the gRPC port number or error if something goes wrong
func (service *envConfigurationService) GetGrpcPort() (int, error) {
portNumberString := os.Getenv("GRPC_PORT")
if strings.Trim(portNumberString, " ") == "" {
return 0, commonErrors.NewUnknownError("GRPC_PORT is required")
}
portNumber, err := strconv.Atoi(portNumberString)
if err != nil {
return 0, commonErrors.NewUnknownErrorWithError("failed to convert GRPC_PORT to integer", err)
}
return portNumber, nil
}
// GetHttpHost retrieves the HTTP host name
// Returns the HTTP host name or error if something goes wrong
func (service *envConfigurationService) GetHttpHost() (string, error) {
return os.Getenv("HTTP_HOST"), nil
}
// GetHttpPort retrieves the HTTP port number
// Returns the HTTP port number or error if something goes wrong
func (service *envConfigurationService) GetHttpPort() (int, error) {
portNumberString := os.Getenv("HTTP_PORT")
if strings.Trim(portNumberString, " ") == "" {
return 0, commonErrors.NewUnknownError("HTTP_PORT is required")
}
portNumber, err := strconv.Atoi(portNumberString)
if err != nil {
return 0, commonErrors.NewUnknownErrorWithError("failed to convert HTTP_PORT to integer", err)
}
return portNumber, nil
}
// GetDatabaseConnectionString retrieves the database connection string
// Returns the database connection string or error if something goes wrong
func (service *envConfigurationService) GetDatabaseConnectionString() (string, error) {
connectionString := os.Getenv("DATABASE_CONNECTION_STRING")
if strings.Trim(connectionString, " ") == "" {
return "", commonErrors.NewUnknownError("DATABASE_CONNECTION_STRING is required")
}
return connectionString, nil
}
// GetDatabaseName retrieves the database name
// Returns the database name or error if something goes wrong
func (service *envConfigurationService) GetDatabaseName() (string, error) {
databaseName := os.Getenv("USER_DATABASE_NAME")
if strings.Trim(databaseName, " ") == "" {
return "", commonErrors.NewUnknownError("USER_DATABASE_NAME is required")
}
return databaseName, nil
}
// GetDatabaseCollectionName retrieves the database collection name
// Returns the database collection name or error if something goes wrong
func (service *envConfigurationService) GetDatabaseCollectionName() (string, error) {
databaseCollectionName := os.Getenv("USER_DATABASE_COLLECTION_NAME")
if strings.Trim(databaseCollectionName, " ") == "" {
return "", commonErrors.NewUnknownError("USER_DATABASE_COLLECTION_NAME is required")
}
return databaseCollectionName, nil
}
// GetJwksURL retrieves the JWKS URL
// Returns the JWKS URL or error if something goes wrong
func (service *envConfigurationService) GetJwksURL() (string, error) {
jwksURL := os.Getenv("JWKS_URL")
if strings.Trim(jwksURL, " ") == "" {
return "", commonErrors.NewUnknownError("JWKS_URL is required")
}
return jwksURL, nil
}