Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make grpc server and http server bind addr configurable #75

Merged
merged 3 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion pkg/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"fmt"
"html/template"
"log"
"net"
"net/http"
"os/signal"
"runtime"
"strings"
"syscall"
"time"

"github.com/cenkalti/backoff/v4"
"github.com/openfga/openfga/internal/build"
"github.com/openfga/openfga/pkg/cmd/service"
"github.com/openfga/openfga/pkg/logger"
Expand Down Expand Up @@ -72,6 +74,21 @@ func run(_ *cobra.Command, _ []string) {

fileServer := http.FileServer(http.Dir("./static"))

policy := backoff.NewExponentialBackOff()
policy.MaxElapsedTime = 3 * time.Second

var conn net.Conn
err = backoff.Retry(
func() error {
conn, err = net.Dial("tcp", config.HTTPAddr)
return err
},
policy,
)
if err != nil {
logger.Fatal("failed to establish Playground connection to HTTP server", zap.Error(err))
}

mux := http.NewServeMux()
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

Expand All @@ -80,7 +97,7 @@ func run(_ *cobra.Command, _ []string) {
err = tmpl.Execute(w, struct {
HTTPServerURL string
}{
HTTPServerURL: fmt.Sprintf("localhost:%d", config.HTTPPort),
HTTPServerURL: conn.RemoteAddr().String(),
})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
Expand Down
8 changes: 4 additions & 4 deletions pkg/cmd/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ type Config struct {
DatastoreConnectionURI string `split_words:"true"`
DatastoreMaxCacheSize int `default:"100000" split_words:"true"`
ServiceName string `default:"openfga" split_words:"true"`
HTTPPort int `default:"8080" split_words:"true"`
RPCPort int `default:"8081" split_words:"true"`
HTTPAddr string `default:":8080" split_words:"true"`
GRPCAddr string `default:":8081" split_words:"true"`
MaxTuplesPerWrite int `default:"100" split_words:"true"`
MaxTypesPerAuthorizationModel int `default:"100" split_words:"true"`
// ChangelogHorizonOffset is an offset in minutes from the current time. Changes that occur after this offset will not be included in the response of ReadChanges.
Expand Down Expand Up @@ -184,11 +184,11 @@ func BuildService(config Config, logger logger.Logger) (*service, error) {
}, &server.Config{
ServiceName: config.ServiceName,
GRPCServer: server.GRPCServerConfig{
Addr: config.RPCPort,
Addr: config.GRPCAddr,
TLSConfig: grpcTLSConfig,
},
HTTPServer: server.HTTPServerConfig{
Addr: config.HTTPPort,
Addr: config.HTTPAddr,
TLSConfig: httpTLSConfig,
CORSAllowedOrigins: config.CORSAllowedOrigins,
CORSAllowedHeaders: config.CORSAllowedHeaders,
Expand Down
8 changes: 4 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ type Config struct {
}

type GRPCServerConfig struct {
Addr int
Addr string
TLSConfig *TLSConfig
}

type HTTPServerConfig struct {
Addr int
Addr string
TLSConfig *TLSConfig
CORSAllowedOrigins []string
CORSAllowedHeaders []string
Expand Down Expand Up @@ -426,7 +426,7 @@ func (s *Server) Run(ctx context.Context) error {
grpcServer := grpc.NewServer(opts...)
openfgapb.RegisterOpenFGAServiceServer(grpcServer, s)

rpcAddr := fmt.Sprintf("localhost:%d", s.config.GRPCServer.Addr)
rpcAddr := s.config.GRPCServer.Addr
lis, err := net.Listen("tcp", rpcAddr)
if err != nil {
return err
Expand Down Expand Up @@ -475,7 +475,7 @@ func (s *Server) Run(ctx context.Context) error {
}

httpServer := &http.Server{
Addr: fmt.Sprintf(":%d", s.config.HTTPServer.Addr),
Addr: s.config.HTTPServer.Addr,
Handler: cors.New(cors.Options{
AllowedOrigins: s.config.HTTPServer.CORSAllowedOrigins,
AllowCredentials: true,
Expand Down