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

Support custom basepath in HotROD #1894

Merged
merged 12 commits into from
Dec 16, 2019
1 change: 1 addition & 0 deletions examples/hotrod/cmd/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var frontendCmd = &cobra.Command{
options.DriverHostPort = net.JoinHostPort("0.0.0.0", strconv.Itoa(driverPort))
options.CustomerHostPort = net.JoinHostPort("0.0.0.0", strconv.Itoa(customerPort))
options.RouteHostPort = net.JoinHostPort("0.0.0.0", strconv.Itoa(routePort))
options.Basepath = basepath

zapLogger := logger.With(zap.String("service", "frontend"))
logger := log.NewFactory(zapLogger)
Expand Down
9 changes: 9 additions & 0 deletions examples/hotrod/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ var (
driverPort int
frontendPort int
routePort int

basepath string
)

// RootCmd represents the base command when called without any subcommands
Expand Down Expand Up @@ -73,6 +75,9 @@ func init() {
RootCmd.PersistentFlags().IntVarP(&frontendPort, "frontend-service-port", "f", 8080, "Port for frontend service")
RootCmd.PersistentFlags().IntVarP(&routePort, "route-service-port", "r", 8083, "Port for routing service")

// Flag for serving frontend at custom basepath url
RootCmd.PersistentFlags().StringVarP(&basepath, "basepath", "b", "", "Basepath for frontend service")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a default value / it makes the help command more self explanatory.

  -b, --basepath string                  Basepath for frontend service (default "/")

instead of

  -b, --basepath string                  Basepath for frontend service


rand.Seed(int64(time.Now().Nanosecond()))
logger, _ = zap.NewDevelopment(zap.AddStacktrace(zapcore.FatalLevel))
cobra.OnInitialize(onInitialize)
Expand Down Expand Up @@ -118,6 +123,10 @@ func onInitialize() {
if routePort != 8083 {
logger.Info("changing route service port", zap.Int("old", 8083), zap.Int("new", routePort))
}

if basepath != "" {
logger.Info("changing basepath for frontend", zap.String("old", "/"), zap.String("new", basepath))
}
}

func logError(logger *zap.Logger, err error) error {
Expand Down
53 changes: 27 additions & 26 deletions examples/hotrod/services/frontend/gen_assets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions examples/hotrod/services/frontend/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package frontend
import (
"encoding/json"
"net/http"
"path"

"github.com/opentracing/opentracing-go"
"go.uber.org/zap"
Expand All @@ -34,6 +35,7 @@ type Server struct {
logger log.Factory
bestETA *bestETA
assetFS http.FileSystem
basepath string
}

// ConfigOptions used to make sure service clients
Expand All @@ -43,6 +45,7 @@ type ConfigOptions struct {
DriverHostPort string
CustomerHostPort string
RouteHostPort string
Basepath string
}

// NewServer creates a new frontend.Server
Expand All @@ -54,20 +57,22 @@ func NewServer(options ConfigOptions, tracer opentracing.Tracer, logger log.Fact
logger: logger,
bestETA: newBestETA(tracer, logger, options),
assetFS: assetFS,
basepath: options.Basepath,
}
}

// Run starts the frontend server
func (s *Server) Run() error {
mux := s.createServeMux()
s.logger.Bg().Info("Starting", zap.String("address", "http://"+s.hostPort))
s.logger.Bg().Info("Starting", zap.String("address", "http://"+path.Join(s.hostPort, s.basepath)))
return http.ListenAndServe(s.hostPort, mux)
}

func (s *Server) createServeMux() http.Handler {
mux := tracing.NewServeMux(s.tracer)
mux.Handle("/", http.FileServer(s.assetFS))
mux.Handle("/dispatch", http.HandlerFunc(s.dispatch))
p := path.Join("/", s.basepath)
mux.Handle(p, http.StripPrefix(p, http.FileServer(s.assetFS)))
mux.Handle(path.Join(p, "/dispatch"), http.HandlerFunc(s.dispatch))
return mux
}

Expand Down
7 changes: 6 additions & 1 deletion examples/hotrod/services/frontend/web_assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ <h4><em>Rides On Demand</em></h4>
};
console.log(headers);
var before = Date.now();
$.ajax('/dispatch?customer=' + customer + '&nonse=' + Math.random(), {

// Use current URI as basepath for ajax requests
var pathPrefix = window.location.pathname;
pathPrefix = pathPrefix != "/" ? pathPrefix : '';

$.ajax(pathPrefix + '/dispatch?customer=' + customer + '&nonse=' + Math.random(), {
jan25 marked this conversation as resolved.
Show resolved Hide resolved
headers: headers,
method: 'GET',
success: function(data, textStatus) {
Expand Down