Skip to content

Commit

Permalink
Refactored config to be uniform with possibilty for local overrides.
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Jan 24, 2024
1 parent 44b6d0d commit 9cd5687
Show file tree
Hide file tree
Showing 16 changed files with 161 additions and 117 deletions.
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ endif

default: clean tidy $(NAME)

all: clean tidy install

build: $(NAME)

init: go.mod tidy
Expand All @@ -29,6 +31,8 @@ $(NAME): tidy

clean:
rm -f $(NAME)
make -C cmd/home clean
make -C cmd/relay clean

console:
docker-compose up -d
Expand All @@ -41,7 +45,7 @@ down:
docker-compose down

home:
make -C cmd/home install
make -C cmd/home

relay:
make -C cmd/relay go-ma-relay
Expand All @@ -52,9 +56,11 @@ image:
--build-arg "BUILD_IMAGE=$(BUILD_IMAGE)" \
.

install: relay home
install: relay home $(NAME)
sudo make -C cmd/home install
sudo make -C cmd/relay install
sudo install -m755 $(NAME) $(DESTDIR)$(PREFIX)/bin/$(NAME)


lint:
find -name "*.yaml" -exec yamllint -c .yamllintrc {} \;
Expand Down
2 changes: 2 additions & 0 deletions cmd/home/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.log
go-ma-*
2 changes: 1 addition & 1 deletion cmd/home/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ $(NAME):
go build -o $(NAME) .

install: $(NAME)
sudo mv $(NAME) $(PREFIX)/bin
sudo install -m755 $(NAME) $(DESTDIR)$(PREFIX)/bin/$(NAME)

clean:
rm -f $(NAME)
27 changes: 27 additions & 0 deletions cmd/home/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

const (
defaultHttpSocket string = "0.0.0.0:5003"
)

func init() {

// Flags - user configurations

pflag.String("http_socket", defaultHttpSocket, "Address for webserver to listen on")
viper.BindPFlag("http.socket", pflag.Lookup("socket"))

// pflag.Int("port", defaultListenPort, "Port for service to listen on")
// v.BindPFlag("port", pflag.Lookup("port"))

pflag.Parse()
}

func getHttpSocket() string {
return viper.GetString("http.socket")
}
24 changes: 7 additions & 17 deletions cmd/home/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,23 @@ package main

import (
"context"
"flag"
"fmt"
"net/http"
"os"

"github.com/spf13/pflag"

"github.com/bahner/go-ma-actor/actor"
"github.com/bahner/go-ma-actor/config"
"github.com/bahner/go-ma-actor/p2p"

log "github.com/sirupsen/logrus"
)

// var (
// ctx context.Context
// err error

// a *actor.Actor
// p *p2p.P2P
// n host.Host

// envelopes <-chan *msg.Envelope
// )

func main() {

flag.Parse()
config.Init()
pflag.Parse()
config.Init("home.yaml")

ctx := context.Background()

Expand All @@ -53,14 +43,14 @@ func main() {
fmt.Printf("I am : %s\n", a.Entity.DID.String())
fmt.Printf("My public key is: %s\n", n.ID().String())

go discoveryHandler(p)
discoveryHandler(p)
go handleEvents(ctx, a)

// This is defined in web.go. It makes it possible to add extra parameters to the handler.
h := &WebHandlerData{n, a}
http.HandleFunc("/", h.WebHandler)
fmt.Println("Listening on port 5003...")
err = http.ListenAndServe("0.0.0.0:5003", nil)
log.Infof("Listening on %s\n", getHttpSocket())
err = http.ListenAndServe(getHttpSocket(), nil)
if err != nil {
log.Fatal(err)
}
Expand Down
29 changes: 2 additions & 27 deletions cmd/relay/.gitignore
Original file line number Diff line number Diff line change
@@ -1,27 +1,2 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
arm64-*

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work
go-*
.env
go-ma-relay*
.vscode
.env
*.log
go-ma*
2 changes: 1 addition & 1 deletion cmd/relay/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ release: image push
docker push $(IMAGE):$(VERSION)

install:
install -Dm755 $(NAME) $(DESTDIR)$(PREFIX)/bin/$(NAME)
install -m755 $(NAME) $(DESTDIR)$(PREFIX)/bin/$(NAME)

.PHONY: default init tidy install clean distclean
46 changes: 11 additions & 35 deletions cmd/relay/config.go
Original file line number Diff line number Diff line change
@@ -1,52 +1,28 @@
package main

import (
"flag"

"go.deanishe.net/env"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

const (
defaultListenPort string = "4001" // 0 = random
defaultHttpAddr string = "0.0.0.0"
defaultHttpPort string = "4000"
)

var (
httpSocket string

httpAddr string = env.Get("GO_MA_RELAY_HTTP_ADDR", defaultHttpAddr)
httpPort string = env.Get("GO_MA_RELAY_HTTP_PORT", defaultHttpPort)
listenPort string = env.Get("GO_MA_RELAY_LISTEN_PORT", defaultListenPort)
// defaultListenPort int = 4001 // 0 = random
defaultHttpSocket string = "0.0.0.0:4000"
)

func init() {

// Flags - user configurations

flag.StringVar(&httpAddr, "httpAddr", httpAddr, "Address to listen on")
flag.StringVar(&httpPort, "httpPort", httpPort, "Listen port for webserver")

flag.StringVar(&listenPort, "listenPort", listenPort, "Port to listen on for peers")

flag.Parse()
pflag.String("http_socket", defaultHttpSocket, "Address for webserver to listen on")
viper.BindPFlag("http.socket", pflag.Lookup("socket"))

// Assemble vars for http server
httpSocket = httpAddr + ":" + httpPort
}

func GetListenPort() string {
return listenPort
}

func GetHttpSocket() string {
return httpSocket
}
// pflag.Int("port", defaultListenPort, "Port for service to listen on")
// v.BindPFlag("port", pflag.Lookup("port"))

func GetHttpAddr() string {
return httpAddr
pflag.Parse()
}

func GetHttpPort() string {
return httpPort
func getHttpSocket() string {
return viper.GetString("http.socket")
}
5 changes: 3 additions & 2 deletions cmd/relay/constants.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

const (
NAME = "go-ma-relay"
VERSION = "1.0.6"
NAME = "go-ma-relay"
VERSION = "1.0.6"
ENV_PREFIX = "GO_MA_RELAY"
)
16 changes: 15 additions & 1 deletion cmd/relay/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/multiformats/go-multiaddr v0.12.1
github.com/sirupsen/logrus v1.9.3
github.com/spf13/pflag v1.0.5
go.deanishe.net/env v0.5.1
github.com/spf13/viper v1.18.2
)

require (
Expand All @@ -28,6 +28,7 @@ require (
github.com/facebookgo/atomicfile v0.0.0-20151019160806-2de1f203e7d5 // indirect
github.com/flynn/noise v1.0.0 // indirect
github.com/francoispqt/gojay v1.2.13 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fxamacker/cbor/v2 v2.5.0 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
Expand All @@ -42,6 +43,7 @@ require (
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/golang-lru v1.0.2 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/huin/goupnp v1.3.0 // indirect
github.com/ipfs/bbloom v0.0.4 // indirect
github.com/ipfs/boxo v0.16.0 // indirect
Expand Down Expand Up @@ -83,6 +85,7 @@ require (
github.com/libp2p/go-reuseport v0.4.0 // indirect
github.com/libp2p/go-yamux/v4 v4.0.1 // indirect
github.com/libp2p/zeroconf/v2 v2.2.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd // indirect
github.com/matoous/go-nanoid/v2 v2.0.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand All @@ -92,6 +95,7 @@ require (
github.com/mikioh/tcpopt v0.0.0-20190314235656-172688c1accc // indirect
github.com/minio/sha256-simd v1.0.1 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
Expand All @@ -108,6 +112,7 @@ require (
github.com/opencontainers/runtime-spec v1.1.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/polydawn/refmt v0.89.0 // indirect
github.com/prometheus/client_golang v1.17.0 // indirect
Expand All @@ -120,13 +125,20 @@ require (
github.com/quic-go/webtransport-go v0.6.0 // indirect
github.com/raulk/go-watchdog v1.3.0 // indirect
github.com/rs/cors v1.7.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/samber/lo v1.39.0 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/whyrusleeping/base32 v0.0.0-20170828182744-c30ac30633cc // indirect
github.com/whyrusleeping/cbor-gen v0.0.0-20230126041949-52956bd4c9aa // indirect
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect
github.com/x448/float16 v0.8.4 // indirect
go.deanishe.net/env v0.5.1 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/otel v1.17.0 // indirect
go.opentelemetry.io/otel/metric v1.17.0 // indirect
Expand All @@ -148,5 +160,7 @@ require (
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
gonum.org/v1/gonum v0.14.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/blake3 v1.2.1 // indirect
)
Loading

0 comments on commit 9cd5687

Please sign in to comment.