Skip to content

Commit 985f9c8

Browse files
t-perssonfredjn
andauthored
Clean up configurations to utilize composition instead (#89)
* Clean up configurations to utilize composition instead Co-authored-by: Fredrik Fristedt <[email protected]> --------- Co-authored-by: Fredrik Fristedt <[email protected]>
1 parent 36688ef commit 985f9c8

File tree

30 files changed

+250
-875
lines changed

30 files changed

+250
-875
lines changed

cmd/executionspace/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"runtime/debug"
2424
"syscall"
2525

26-
config "github.com/eiffel-community/etos-api/internal/configs/executionspace"
26+
"github.com/eiffel-community/etos-api/internal/config"
2727
"github.com/eiffel-community/etos-api/internal/database/etcd"
2828
"github.com/eiffel-community/etos-api/internal/executionspace/provider"
2929
"github.com/eiffel-community/etos-api/internal/logging"
@@ -39,7 +39,7 @@ import (
3939

4040
// main sets up logging and starts up the webservice.
4141
func main() {
42-
cfg := config.Get()
42+
cfg := config.NewExecutionSpaceConfig()
4343
ctx := context.Background()
4444

4545
var hooks []logrus.Hook
@@ -120,7 +120,7 @@ func fileLogging(cfg config.Config) logrus.Hook {
120120

121121
// remoteLogging starts a new rabbitmq publisher if the rabbitmq parameters are set
122122
// Warning: Must call publisher.Close() on the publisher returned from this function
123-
func remoteLogging(cfg config.Config) *rabbitmq.Publisher {
123+
func remoteLogging(cfg config.ExecutionSpaceConfig) *rabbitmq.Publisher {
124124
if cfg.RabbitMQHookURL() != "" {
125125
if cfg.RabbitMQHookExchangeName() == "" {
126126
panic("-rabbitmq_hook_exchange (env:ETOS_RABBITMQ_EXCHANGE) must be set when using -rabbitmq_hook_url (env:ETOS_RABBITMQ_URL)")

cmd/iut/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"syscall"
2525
"time"
2626

27-
config "github.com/eiffel-community/etos-api/internal/configs/iut"
27+
"github.com/eiffel-community/etos-api/internal/config"
2828
"github.com/eiffel-community/etos-api/internal/database/etcd"
2929
"github.com/eiffel-community/etos-api/internal/logging"
3030
server "github.com/eiffel-community/etos-api/internal/server"
@@ -37,7 +37,7 @@ import (
3737

3838
// main sets up logging and starts up the webserver.
3939
func main() {
40-
cfg := config.Get()
40+
cfg := config.NewIUTConfig()
4141
ctx := context.Background()
4242

4343
var hooks []logrus.Hook

cmd/logarea/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"syscall"
2525
"time"
2626

27-
config "github.com/eiffel-community/etos-api/internal/configs/logarea"
27+
"github.com/eiffel-community/etos-api/internal/config"
2828
"github.com/eiffel-community/etos-api/internal/logging"
2929
"github.com/eiffel-community/etos-api/internal/server"
3030
"github.com/eiffel-community/etos-api/pkg/application"
@@ -36,7 +36,7 @@ import (
3636

3737
// main sets up logging and starts up the logarea webservice.
3838
func main() {
39-
cfg := config.Get()
39+
cfg := config.NewLogAreaConfig()
4040
ctx := context.Background()
4141

4242
var hooks []logrus.Hook

cmd/sse/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"syscall"
2525
"time"
2626

27-
config "github.com/eiffel-community/etos-api/internal/configs/sse"
27+
"github.com/eiffel-community/etos-api/internal/config"
2828
"github.com/eiffel-community/etos-api/internal/logging"
2929
"github.com/eiffel-community/etos-api/internal/server"
3030
"github.com/eiffel-community/etos-api/pkg/application"
@@ -37,7 +37,7 @@ import (
3737

3838
// main sets up logging and starts up the sse webserver.
3939
func main() {
40-
cfg := config.Get()
40+
cfg := config.NewSSEConfig()
4141
ctx := context.Background()
4242

4343
var hooks []logrus.Hook

internal/configs/base/config.go internal/config/base.go

+12-14
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ type Config interface {
3232
DatabaseURI() string
3333
}
3434

35-
// cfg implements the Config interface.
36-
type cfg struct {
35+
// baseCfg implements the Config interface.
36+
type baseCfg struct {
3737
serviceHost string
3838
servicePort string
3939
stripPrefix string
@@ -44,9 +44,9 @@ type cfg struct {
4444
databasePort string
4545
}
4646

47-
// Get creates a config interface based on input parameters or environment variables.
48-
func Get() Config {
49-
var conf cfg
47+
// load the command line vars for a base configuration.
48+
func load() Config {
49+
var conf baseCfg
5050

5151
flag.StringVar(&conf.serviceHost, "address", EnvOrDefault("SERVICE_HOST", "127.0.0.1"), "Address to serve API on")
5252
flag.StringVar(&conf.servicePort, "port", EnvOrDefault("SERVICE_PORT", "8080"), "Port to serve API on")
@@ -56,43 +56,41 @@ func Get() Config {
5656
flag.StringVar(&conf.etosNamespace, "etosnamespace", ReadNamespaceOrEnv("ETOS_NAMESPACE"), "Path, including filename, for the log files to create.")
5757
flag.StringVar(&conf.databaseHost, "databasehost", EnvOrDefault("ETOS_ETCD_HOST", "etcd-client"), "Host to the database.")
5858
flag.StringVar(&conf.databasePort, "databaseport", EnvOrDefault("ETOS_ETCD_PORT", "2379"), "Port to the database.")
59-
60-
flag.Parse()
6159
return &conf
6260
}
6361

6462
// ServiceHost returns the host of the service.
65-
func (c *cfg) ServiceHost() string {
63+
func (c *baseCfg) ServiceHost() string {
6664
return c.serviceHost
6765
}
6866

6967
// ServicePort returns the port of the service.
70-
func (c *cfg) ServicePort() string {
68+
func (c *baseCfg) ServicePort() string {
7169
return c.servicePort
7270
}
7371

7472
// StripPrefix returns the prefix to strip. Empty string if no prefix.
75-
func (c *cfg) StripPrefix() string {
73+
func (c *baseCfg) StripPrefix() string {
7674
return c.stripPrefix
7775
}
7876

7977
// LogLevel returns the log level.
80-
func (c *cfg) LogLevel() string {
78+
func (c *baseCfg) LogLevel() string {
8179
return c.logLevel
8280
}
8381

8482
// LogFilePath returns the path to where log files should be stored, including filename.
85-
func (c *cfg) LogFilePath() string {
83+
func (c *baseCfg) LogFilePath() string {
8684
return c.logFilePath
8785
}
8886

8987
// ETOSNamespace returns the ETOS namespace.
90-
func (c *cfg) ETOSNamespace() string {
88+
func (c *baseCfg) ETOSNamespace() string {
9189
return c.etosNamespace
9290
}
9391

9492
// DatabaseURI returns the URI to the ETOS database.
95-
func (c *cfg) DatabaseURI() string {
93+
func (c *baseCfg) DatabaseURI() string {
9694
return fmt.Sprintf("%s:%s", c.databaseHost, c.databasePort)
9795
}
9896

internal/configs/sse/config_test.go internal/config/base_test.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package config
1717

1818
import (
19-
"os"
2019
"testing"
2120

2221
"github.com/stretchr/testify/assert"
@@ -29,12 +28,12 @@ func TestGet(t *testing.T) {
2928
logLevel := "DEBUG"
3029
logFilePath := "path/to/a/file"
3130

32-
os.Setenv("SERVICE_HOST", serverHost)
33-
os.Setenv("SERVICE_PORT", port)
34-
os.Setenv("LOGLEVEL", logLevel)
35-
os.Setenv("LOG_FILE_PATH", logFilePath)
31+
t.Setenv("SERVICE_HOST", serverHost)
32+
t.Setenv("SERVICE_PORT", port)
33+
t.Setenv("LOGLEVEL", logLevel)
34+
t.Setenv("LOG_FILE_PATH", logFilePath)
3635

37-
conf, ok := Get().(*cfg)
36+
conf, ok := load().(*baseCfg)
3837
assert.Truef(t, ok, "cfg returned from get is not a config interface")
3938
assert.Equal(t, port, conf.servicePort)
4039
assert.Equal(t, serverHost, conf.serviceHost)
@@ -46,15 +45,15 @@ type getter func() string
4645

4746
// Test that the getters in the Cfg struct return the values from the struct.
4847
func TestGetters(t *testing.T) {
49-
conf := &cfg{
48+
conf := &baseCfg{
5049
serviceHost: "127.0.0.1",
5150
servicePort: "8080",
5251
logLevel: "TRACE",
5352
logFilePath: "a/file/path.json",
5453
}
5554
tests := []struct {
5655
name string
57-
cfg *cfg
56+
cfg *baseCfg
5857
function getter
5958
value string
6059
}{

internal/config/executionspace.go

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright Axis Communications AB.
2+
//
3+
// For a full list of individual contributors, please see the commit history.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
package config
17+
18+
import (
19+
"flag"
20+
"os"
21+
"time"
22+
23+
"github.com/sirupsen/logrus"
24+
)
25+
26+
type ExecutionSpaceConfig interface {
27+
Config
28+
Hostname() string
29+
Timeout() time.Duration
30+
ExecutionSpaceWaitTimeout() time.Duration
31+
RabbitMQHookURL() string
32+
RabbitMQHookExchangeName() string
33+
EiffelGoerURL() string
34+
}
35+
36+
// executionSpaceCfg implements the ExecutionSpaceConfig interface.
37+
type executionSpaceCfg struct {
38+
Config
39+
stripPrefix string
40+
hostname string
41+
timeout time.Duration
42+
executionSpaceWaitTimeout time.Duration
43+
rabbitmqHookURL string
44+
rabbitmqHookExchange string
45+
eiffelGoerURL string
46+
}
47+
48+
// NewExecutionSpaceConfig creates an executio nspace config interface based on input parameters or environment variables.
49+
func NewExecutionSpaceConfig() ExecutionSpaceConfig {
50+
var conf executionSpaceCfg
51+
52+
defaultTimeout, err := time.ParseDuration(EnvOrDefault("REQUEST_TIMEOUT", "1m"))
53+
if err != nil {
54+
logrus.Panic(err)
55+
}
56+
57+
executionSpaceWaitTimeout, err := time.ParseDuration(EnvOrDefault("EXECUTION_SPACE_WAIT_TIMEOUT", "1h"))
58+
if err != nil {
59+
logrus.Panic(err)
60+
}
61+
62+
flag.StringVar(&conf.hostname, "hostname", EnvOrDefault("PROVIDER_HOSTNAME", "http://localhost"), "Host to supply to ESR for starting executors")
63+
flag.DurationVar(&conf.timeout, "timeout", defaultTimeout, "Maximum timeout for requests to Execution space provider Service.")
64+
flag.DurationVar(&conf.executionSpaceWaitTimeout, "executionSpaceWaitTimeout", executionSpaceWaitTimeout, "Timeout duration to wait when trying to checkout execution space(s)")
65+
flag.StringVar(&conf.rabbitmqHookURL, "rabbitmq_hook_url", os.Getenv("ETOS_RABBITMQ_URL"), "URL to the ETOS rabbitmq for logs")
66+
flag.StringVar(&conf.rabbitmqHookExchange, "rabbitmq_hook_exchange", os.Getenv("ETOS_RABBITMQ_EXCHANGE"), "Exchange to use for the ETOS rabbitmq for logs")
67+
flag.StringVar(&conf.eiffelGoerURL, "event_repository_host", os.Getenv("EIFFEL_GOER_URL"), "Event repository URL used for Eiffel event lookup")
68+
base := load()
69+
flag.Parse()
70+
conf.Config = base
71+
72+
return &conf
73+
}
74+
75+
// Hostname returns the hostname to use for executors.
76+
func (c *executionSpaceCfg) Hostname() string {
77+
return c.hostname
78+
}
79+
80+
// Timeout returns the request timeout for Execution space provider Service API.
81+
func (c *executionSpaceCfg) Timeout() time.Duration {
82+
return c.timeout
83+
}
84+
85+
// ExecutionSpaceWaitTimeout returns the timeout for checking out execution spaces.
86+
func (c *executionSpaceCfg) ExecutionSpaceWaitTimeout() time.Duration {
87+
return c.executionSpaceWaitTimeout
88+
}
89+
90+
// RabbitMQHookURL returns the rabbitmq url for ETOS logs
91+
func (c *executionSpaceCfg) RabbitMQHookURL() string {
92+
return c.rabbitmqHookURL
93+
}
94+
95+
// EiffelGoerURL returns the Eiffel event repository used for event lookups
96+
func (c *executionSpaceCfg) EiffelGoerURL() string {
97+
return c.eiffelGoerURL
98+
}
99+
100+
// RabbitMQHookExchangeName returns the rabbitmq exchange name used for ETOS logs
101+
func (c *executionSpaceCfg) RabbitMQHookExchangeName() string {
102+
return c.rabbitmqHookExchange
103+
}

internal/config/iut.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright Axis Communications AB.
2+
//
3+
// For a full list of individual contributors, please see the commit history.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
package config
17+
18+
import "flag"
19+
20+
type IUTConfig interface {
21+
Config
22+
}
23+
24+
// NewIUTConfig creates an iut config interface based on input parameters or environment variables.
25+
func NewIUTConfig() IUTConfig {
26+
cfg := load()
27+
flag.Parse()
28+
return cfg
29+
}

internal/config/logarea.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright Axis Communications AB.
2+
//
3+
// For a full list of individual contributors, please see the commit history.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
package config
17+
18+
import "flag"
19+
20+
type LogAreaConfig interface {
21+
Config
22+
}
23+
24+
// NewLogAreaConfig creates a log area config interface based on input parameters or environment variables.
25+
func NewLogAreaConfig() LogAreaConfig {
26+
cfg := load()
27+
flag.Parse()
28+
return cfg
29+
}

internal/config/sse.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright Axis Communications AB.
2+
//
3+
// For a full list of individual contributors, please see the commit history.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
package config
17+
18+
import "flag"
19+
20+
type SSEConfig interface {
21+
Config
22+
}
23+
24+
// NewSSEConfig creates a sse config interface based on input parameters or environment variables.
25+
func NewSSEConfig() SSEConfig {
26+
cfg := load()
27+
flag.Parse()
28+
return cfg
29+
}

0 commit comments

Comments
 (0)