Skip to content

Commit

Permalink
Support custom basepath in HotROD (#1894)
Browse files Browse the repository at this point in the history
* fix index.html

Signed-off-by: Abhilash Gnan <abhilashgnan@gmail.com>

* Add basepath arg

Signed-off-by: Abhilash Gnan <abhilashgnan@gmail.com>

* Minor refactor

Signed-off-by: Abhilash Gnan <abhilashgnan@gmail.com>

* Polish

Signed-off-by: Abhilash Gnan <abhilashgnan@gmail.com>

* Fix typos

Signed-off-by: Abhilash Gnan <abhilashgnan@gmail.com>

* use path from stdlib

Signed-off-by: Abhilash Gnan <abhilashgnan@gmail.com>

* Update help doc

Signed-off-by: Abhilash Gnan <abhilashgnan@gmail.com>
  • Loading branch information
jan25 authored and pavolloffay committed Dec 16, 2019
1 parent ae0b54b commit 65ba726
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 30 deletions.
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(default "/")`)

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(), {
headers: headers,
method: 'GET',
success: function(data, textStatus) {
Expand Down

0 comments on commit 65ba726

Please sign in to comment.