Skip to content

Commit

Permalink
Merge pull request #11 from dirodriguezm/functional-options
Browse files Browse the repository at this point in the history
refactor: use functional options pattern for config
  • Loading branch information
dirodriguezm authored Aug 6, 2024
2 parents cf964e4 + 1fa9234 commit 4bc81fc
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 40 deletions.
13 changes: 13 additions & 0 deletions tapservicego/cmd/tapservice/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"ataps/internal/tapsync"
"fmt"
)

func main() {
config := tapsync.NewConfig() // by default config uses env variables
r := tapsync.NewTapSyncService(config)
r.Router.Run(fmt.Sprintf(":%d", config.Port))
r.DB.Close()
}
13 changes: 9 additions & 4 deletions tapservicego/dagger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (m *Tapservicego) BuildEnv(ctx context.Context, source *Directory) *Contain
WithExec([]string{"go", "mod", "download"}).
WithExec([]string{"go", "mod", "verify"}).
WithDirectory("/usr/src/app", source).
WithExec([]string{"go", "build", "-o", "/usr/local/bin/ataps", "."})
WithExec([]string{"go", "build", "-o", "/usr/local/bin/ataps", "cmd/tapservice/main.go"})
}

// Tests the go package
Expand All @@ -53,13 +53,13 @@ func (m *Tapservicego) Test(ctx context.Context, source *Directory) (string, err
}

// Build the tap service
func (m *Tapservicego) Build(ctx context.Context, source *Directory) *Container {
func (m *Tapservicego) Build(ctx context.Context, source *Directory, port int) *Container {
return dag.Container().
From("golang:1.22.3-bookworm").
WithExec([]string{"apt-get", "update"}).
WithExec([]string{"apt-get", "install", "libcfitsio-dev", "--yes"}).
WithFile("/bin/ataps", m.BuildEnv(ctx, source).File("/usr/local/bin/ataps")).
WithExposedPort(8080).
WithExposedPort(port).
WithEntrypoint([]string{"ataps"})
}

Expand All @@ -71,8 +71,13 @@ func (m *Tapservicego) Run(
username string,
password string,
dbname *string,
port *int,
) *Container {
container := m.Build(ctx, source)
portOverride := 8080
if port == nil {
port = &portOverride
}
container := m.Build(ctx, source, *port)
if dbname == nil {
dbname = &username
}
Expand Down
File renamed without changes.
4 changes: 1 addition & 3 deletions tapservicego/internal/tapsync/alerce_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (suite *AlerceTestSuite) SetupSuite() {
} else {
suite.T().Fatal("Unknown environment")
}
suite.Service = NewTapSyncService()
suite.Service = NewTapSyncService(NewConfig(WithDatabaseURL(suite.connUrl)))
}

func (suite *AlerceTestSuite) TearDownSuite() {
Expand Down Expand Up @@ -71,7 +71,6 @@ func (suite *AlerceTestSuite) InitializeLocalDB() {
suite.T().Fatal(err)
}
suite.connUrl = connStr
os.Setenv("DATABASE_URL", connStr)
db, err := GetDB(connStr)
if err != nil {
suite.T().Log("Could not connect")
Expand All @@ -98,7 +97,6 @@ func (suite *AlerceTestSuite) InitializeDaggerDB() {
// connect to alerce database
connUrl = connUrl + " dbname=alerce"
suite.connUrl = connUrl
os.Setenv("DATABASE_URL", connUrl)
db, err = GetDB(connUrl)
if err != nil {
suite.T().Log("Could not connect")
Expand Down
30 changes: 24 additions & 6 deletions tapservicego/internal/tapsync/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,32 @@ import "os"
// Config is the configuration for the application
type Config struct {
DatabaseURL string
Port int
}

func GetConfig() *Config {
databaseUrl, ok := os.LookupEnv("DATABASE_URL")
if !ok {
panic("DATABASE_URL not set")
type ConfigOption func(*Config)

func NewConfig(opts ...ConfigOption) *Config {
defaultDatabaseUrl := os.Getenv("DATABASE_URL")
defaultPort := 8080
config := &Config{
DatabaseURL: defaultDatabaseUrl,
Port: defaultPort,
}
for _, opt := range opts {
opt(config)
}
return &Config{
DatabaseURL: databaseUrl,
return config
}

func WithDatabaseURL(databaseUrl string) ConfigOption {
return func(c *Config) {
c.DatabaseURL = databaseUrl
}
}

func WithPort(port int) ConfigOption {
return func(c *Config) {
c.Port = port
}
}
3 changes: 1 addition & 2 deletions tapservicego/internal/tapsync/tapsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ type TapSyncService struct {
config *Config
}

func NewTapSyncService() *TapSyncService {
config := GetConfig()
func NewTapSyncService(config *Config) *TapSyncService {
db, err := GetDB(config.DatabaseURL)
if err != nil {
panic(err)
Expand Down
16 changes: 4 additions & 12 deletions tapservicego/internal/tapsync/tapsync_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
type TapSyncTestSuite struct {
suite.Suite
DB *sql.DB
connUrl string
ConnUrl string
PsqlContainer *postgres.PostgresContainer
ctx context.Context
Service *TapSyncService
Expand All @@ -28,7 +28,7 @@ func (suite *TapSyncTestSuite) SetupSuite() {
} else {
suite.T().Fatal("Unknown environment")
}
suite.Service = NewTapSyncService()
suite.Service = NewTapSyncService(NewConfig(WithDatabaseURL(suite.ConnUrl)))
}

func (suite *TapSyncTestSuite) TearDownSuite() {
Expand All @@ -38,12 +38,6 @@ func (suite *TapSyncTestSuite) TearDownSuite() {
suite.DB.Close()
}

func (suite *TapSyncTestSuite) SetupTest() {
}

func (suite *TapSyncTestSuite) TearDownTest() {
}

func TestTapSyncTestSuite(t *testing.T) {
suite.Run(t, new(TapSyncTestSuite))
}
Expand All @@ -61,14 +55,13 @@ func (suite *TapSyncTestSuite) InitializeLocalDB() {
if err != nil {
suite.T().Fatal(err)
}
suite.connUrl = connStr
os.Setenv("DATABASE_URL", connStr)
db, err := GetDB(connStr)
if err != nil {
suite.T().Log("Could not connect")
suite.T().Fatal(err)
}
suite.DB = db
suite.ConnUrl = connStr
}

func (suite *TapSyncTestSuite) InitializeDaggerDB() {
Expand All @@ -88,12 +81,11 @@ func (suite *TapSyncTestSuite) InitializeDaggerDB() {
db.Close()
// connect to tapsync database
connUrl = connUrl + " dbname=tapsync"
suite.connUrl = connUrl
os.Setenv("DATABASE_URL", connUrl)
db, err = GetDB(connUrl)
if err != nil {
suite.T().Log("Could not connect")
suite.T().Fatal(err)
}
suite.DB = db
suite.ConnUrl = connUrl
}
11 changes: 0 additions & 11 deletions tapservicego/main.go

This file was deleted.

2 changes: 0 additions & 2 deletions tapservicego/test.http

This file was deleted.

0 comments on commit 4bc81fc

Please sign in to comment.