Initial version
Project structure, api, discovery service, docker.
This commit is contained in:
42
internal/application/application.go
Normal file
42
internal/application/application.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"git.derfenix.pro/fenix/astontest/pkg/discovery"
|
||||
)
|
||||
|
||||
type Application struct {
|
||||
discoverySet discovery.DiscoverySet
|
||||
log *zap.Logger
|
||||
}
|
||||
|
||||
func NewApplication(cfg *Config, log *zap.Logger) (*Application, error) {
|
||||
discoveryOpts := []discovery.Option{discovery.WithBroadcastInterval(cfg.BroadcastInterval)}
|
||||
|
||||
if cfg.Debug {
|
||||
discoveryOpts = append(discoveryOpts, discovery.WithDebug())
|
||||
}
|
||||
|
||||
set, err := discovery.NewDiscoverySet(log.Named("discovery"), cfg.DiscoveryPort, discoveryOpts...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("new discovery set: %w", err)
|
||||
|
||||
}
|
||||
|
||||
return &Application{
|
||||
discoverySet: set,
|
||||
log: log,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a *Application) Start(ctx context.Context, wg *sync.WaitGroup) {
|
||||
wg.Add(len(a.discoverySet))
|
||||
for _, discover := range a.discoverySet {
|
||||
go discover.Start(ctx, wg)
|
||||
}
|
||||
}
|
||||
25
internal/application/config.go
Normal file
25
internal/application/config.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/sethvargo/go-envconfig"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Debug bool `env:"DEBUG"`
|
||||
DiscoveryPort uint16 `env:"DISCOVERY_PORT,default=4321"`
|
||||
BroadcastInterval time.Duration `env:"BROADCAST_INTERVAL,default=5s"`
|
||||
}
|
||||
|
||||
func NewConfig(ctx context.Context) (Config, error) {
|
||||
cfg := Config{}
|
||||
|
||||
if err := envconfig.Process(ctx, &cfg); err != nil {
|
||||
return Config{}, fmt.Errorf("process envs: %w", err)
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
Reference in New Issue
Block a user