Skip to content

Commit

Permalink
users/grpc: move default values to options file
Browse files Browse the repository at this point in the history
  • Loading branch information
while-loop committed Dec 16, 2017
1 parent e0004bd commit 3a2a071
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
4 changes: 0 additions & 4 deletions common/registry/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,3 @@ func (s *stubReg) GetService(serviceName, version string) ([]Service, error) {
func (s *stubReg) Name() string {
return "stub"
}

func key(service Service) string {
return service.Name + "-" + service.Version
}
16 changes: 1 addition & 15 deletions common/service/grpc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import (
"net/http"
"time"

"github.com/google/uuid"
"github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/while-loop/levit/common/log"
levnet "github.com/while-loop/levit/common/net"
"github.com/while-loop/levit/common/registry"
"google.golang.org/grpc"
)
Expand All @@ -31,19 +29,7 @@ func NewGrpcService(options Options) Service {
metrics(rpc, options.MetricsAddr)
}

if options.IP == "" {
options.IP = levnet.GetIP()
}

if options.Port <= 0 {
options.Port = 8080
}

if options.TTL == 0 {
options.TTL = 30 * time.Second
}

options.UUID = uuid.New().String()
options.applyDefaults()

s := &grpcService{rpc,
options,
Expand Down
18 changes: 18 additions & 0 deletions common/service/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package service
import (
"fmt"
"time"

"github.com/google/uuid"
levnet "github.com/while-loop/levit/common/net"
)

type Options struct {
Expand All @@ -15,6 +18,21 @@ type Options struct {
TTL time.Duration
}

func (o *Options) applyDefaults() {
if o.IP == "" {
o.IP = levnet.GetIP()
}

if o.Port <= 0 {
o.Port = 8080
}

if o.TTL == 0 {
o.TTL = 30 * time.Second
}

o.UUID = uuid.New().String()
}
func (o Options) laddr() string {
return fmt.Sprintf("%s:%d", o.IP, o.Port)
}
2 changes: 0 additions & 2 deletions users/cmd/usersd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"flag"
"strconv"
"strings"
"time"

"github.com/while-loop/levit/common/log"
"github.com/while-loop/levit/common/registry"
Expand Down Expand Up @@ -41,7 +40,6 @@ func main() {
ServiceName: version.Name,
ServiceVersion: version.Version,
MetricsAddr: ":8181",
TTL: 5 * time.Second,
IP: parts[0],
Port: int(port),
})
Expand Down

0 comments on commit 3a2a071

Please sign in to comment.