Fix up host ports and env variables

This commit is contained in:
Marc Di Luzio 2020-06-13 11:17:52 +01:00
parent 7c830f58be
commit 98249948a1
8 changed files with 38 additions and 55 deletions

View file

@ -15,7 +15,6 @@ import (
"google.golang.org/grpc"
)
var address = os.Getenv("HOST_ADDRESS")
var data = os.Getenv("DATA_PATH")
// accountantServer is the internal object to manage the requests
@ -82,8 +81,9 @@ func (a *accountantServer) GetValue(_ context.Context, in *accounts.DataKey) (*a
// main
func main() {
// Verify the input
var address = os.Getenv("ROVE_ACCOUNTANT_GRPC")
if len(address) == 0 {
log.Fatal("No address set with $HOST_ADDRESS")
log.Fatal("No address set with $ROVE_ACCOUNTANT_GRPC")
}
persistence.SetPath(data)

View file

@ -2,44 +2,42 @@ package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"github.com/golang/glog"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"google.golang.org/grpc"
"github.com/mdiluz/rove/pkg/rove"
)
var endpoint = os.Getenv("GRPC_ENDPOINT")
var address = os.Getenv("HOST_ADDRESS")
func run() error {
func main() {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
// Create a new mux and register it with the gRPC engpoint
var endpoint = os.Getenv("ROVE_GRPC")
if len(endpoint) == 0 {
log.Fatal("Must set $ROVE_GRPC")
}
var address = os.Getenv("ROVE_HTTP")
if len(address) == 0 {
log.Fatal("Must set $ROVE_HTTP")
}
// Create a new mux and register it with the gRPC endpoint
fmt.Printf("Hosting reverse-proxy on %s for %s\n", address, endpoint)
mux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithInsecure()}
err := rove.RegisterRoveHandlerFromEndpoint(ctx, mux, endpoint, opts)
if err != nil {
return err
if err := rove.RegisterRoveHandlerFromEndpoint(ctx, mux, endpoint, opts); err != nil {
log.Fatal(err)
}
// Start the HTTP server and proxy calls to gRPC endpoint when needed
return http.ListenAndServe(address, mux)
}
func main() {
flag.Parse()
defer glog.Flush()
if err := run(); err != nil {
glog.Fatal(err)
if err := http.ListenAndServe(address, mux); err != nil {
log.Fatal(err)
}
}

View file

@ -17,8 +17,6 @@ import (
"google.golang.org/grpc"
)
var accountantAddress = os.Getenv("ACCOUNTANT_ADDRESS")
const (
// PersistentData will allow the server to load and save it's state
PersistentData = iota
@ -106,8 +104,9 @@ func (s *Server) Initialise(fillWorld bool) (err error) {
s.sync.Add(1)
// Connect to the accountant
accountantAddress := os.Getenv("ROVE_ACCOUNTANT_GRPC")
if len(accountantAddress) == 0 {
log.Fatal("must set ACCOUNTANT_ADDRESS")
log.Fatal("must set $ROVE_ACCOUNTANT_GRPC")
}
log.Printf("Dialing accountant on %s\n", accountantAddress)
s.clientConn, err = grpc.Dial(accountantAddress, grpc.WithInsecure())

View file

@ -1,6 +1,7 @@
package internal
import (
"os"
"testing"
)
@ -30,6 +31,7 @@ func TestNewServer_OptionPersistentData(t *testing.T) {
}
func TestServer_Run(t *testing.T) {
os.Setenv("ROVE_ACCOUNTANT_GRPC", "n/a")
server := NewServer()
if server == nil {
t.Error("Failed to create server")
@ -45,6 +47,7 @@ func TestServer_Run(t *testing.T) {
}
func TestServer_RunPersistentData(t *testing.T) {
os.Setenv("ROVE_ACCOUNTANT_GRPC", "n/a")
server := NewServer(OptionPersistentData())
if server == nil {
t.Error("Failed to create server")

View file

@ -7,7 +7,6 @@ import (
"os/signal"
"strconv"
"syscall"
"time"
"github.com/mdiluz/rove/cmd/rove-server/internal"
"github.com/mdiluz/rove/pkg/persistence"
@ -15,10 +14,6 @@ import (
)
var ver = flag.Bool("version", false, "Display version number")
var quit = flag.Int("quit", 0, "Quit after n seconds, useful for testing")
// Address to host the server on, automatically selected if empty
var address = os.Getenv("HOST_ADDRESS")
// Path for persistent storage
var data = os.Getenv("DATA_PATH")
@ -35,8 +30,10 @@ func InnerMain() {
return
}
// Address to host the server on, automatically selected if empty
var address = os.Getenv("ROVE_GRPC")
if len(address) == 0 {
log.Fatalf("Must set HOST_ADDRESS")
log.Fatalf("Must set $ROVE_GRPC")
}
log.Printf("Initialising version %s...\n", version.Version)
@ -76,14 +73,6 @@ func InnerMain() {
}
}()
// Quit after a time if requested
if *quit != 0 {
go func() {
time.Sleep(time.Duration(*quit) * time.Second)
syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
}()
}
// Run the server
s.Run()

View file

@ -10,9 +10,3 @@ func Test_InnerMain_Version(t *testing.T) {
InnerMain()
flag.Set("version", "0")
}
func Test_InnerMain_Quit(t *testing.T) {
flag.Set("quit", "1")
InnerMain()
flag.Set("quit", "0")
}

View file

@ -15,9 +15,9 @@ import (
func Test_InnerMain(t *testing.T) {
var address = os.Getenv("ROVE_SERVER_ADDRESS")
var address = os.Getenv("ROVE_GRPC")
if len(address) == 0 {
log.Fatal("Must set ROVE_SERVER_ADDRESS")
log.Fatal("Must set $ROVE_GRPC")
}
// Set up the flags to act locally and use a temporary file