Files
crossnokaye-interview-assig…/services/front/cmd/front/main.go

141 lines
3.9 KiB
Go

package main
import (
"context"
frontapi "crossnokaye-interview-assignment/services/front"
front "crossnokaye-interview-assignment/services/front/gen/front"
"flag"
"fmt"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"log"
"net"
"net/url"
"os"
"os/signal"
"sync"
"syscall"
)
func main() {
// Define command line flags, add any other flag required to configure the
// service.
var (
hostF = flag.String("host", "localhost", "Server host (valid values: localhost)")
domainF = flag.String("domain", "", "Host domain name (overrides host domain specified in service design)")
httpPortF = flag.String("http-port", "", "HTTP port (overrides host HTTP port specified in service design)")
secureF = flag.Bool("secure", false, "Use secure scheme (https or grpcs)")
dbgF = flag.Bool("debug", false, "Log request and response bodies")
itemAddr = flag.String("item-addr", ":8082", "Item service address")
characterAddr = flag.String("character-addr", ":8083", "Character service address")
inventoryAddr = flag.String("inventory-addr", ":8081", "Inventory service address")
)
flag.Parse()
// Setup logger. Replace logger with your own log package of choice.
var (
logger *log.Logger
)
{
logger = log.New(os.Stderr, "[frontapi] ", log.Ltime)
}
// Init gRPC services
itemClientConnection, err := grpc.Dial(*itemAddr,
grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf("failed to connect to item service", err)
os.Exit(1)
}
defer itemClientConnection.Close()
inventoryClientConnection, err := grpc.Dial(*inventoryAddr,
grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf("failed to connect to inventory service", err)
os.Exit(1)
}
defer inventoryClientConnection.Close()
characterClientConnection, err := grpc.Dial(*characterAddr,
grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf("failed to connect to character service", err)
os.Exit(1)
}
defer characterClientConnection.Close()
// Initialize the services.
var (
frontSvc front.Service
)
{
frontSvc = frontapi.NewFront(logger, itemClientConnection, characterClientConnection,
inventoryClientConnection)
}
// Wrap the services in endpoints that can be invoked from other services
// potentially running in different processes.
var (
frontEndpoints *front.Endpoints
)
{
frontEndpoints = front.NewEndpoints(frontSvc)
}
// Create channel used by both the signal handler and server goroutines
// to notify the main goroutine when to stop the server.
errc := make(chan error)
// Setup interrupt handler. This optional step configures the process so
// that SIGINT and SIGTERM signals cause the services to stop gracefully.
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
errc <- fmt.Errorf("%s", <-c)
}()
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())
// Start the servers and send errors (if any) to the error channel.
switch *hostF {
case "localhost":
{
addr := "http://localhost:8000"
u, err := url.Parse(addr)
if err != nil {
logger.Fatalf("invalid URL %#v: %s\n", addr, err)
}
if *secureF {
u.Scheme = "https"
}
if *domainF != "" {
u.Host = *domainF
}
if *httpPortF != "" {
h, _, err := net.SplitHostPort(u.Host)
if err != nil {
logger.Fatalf("invalid URL %#v: %s\n", u.Host, err)
}
u.Host = net.JoinHostPort(h, *httpPortF)
} else if u.Port() == "" {
u.Host = net.JoinHostPort(u.Host, "80")
}
handleHTTPServer(ctx, u, frontEndpoints, &wg, errc, logger, *dbgF)
}
default:
logger.Fatalf("invalid host argument: %q (valid hosts: localhost)\n", *hostF)
}
// Wait for signal.
logger.Printf("exiting (%v)", <-errc)
// Send cancellation signal to the goroutines.
cancel()
wg.Wait()
logger.Println("exited")
}