Skip to content

Commit

Permalink
Extract struct splitMux
Browse files Browse the repository at this point in the history
  • Loading branch information
emlun committed Jul 2, 2022
1 parent a9c1304 commit 9785197
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions lib/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,22 +368,23 @@ func (s *service) Serve(ctx context.Context) error {
}

// The main routing handler
unauthenticatedMux := http.NewServeMux()
mux.Handle("/rest/", noCacheRestMux)
unauthenticatedMux.Handle("/rest/", handler)
mux.HandleFunc("/qr/", s.getQR)
unauthenticatedMux.Handle("/qr/", handler)
mainMuxBuilder := splitMux{
fallbackHandler: &handler,
fallbackMux: mux, // Authenticated section
mainMux: http.NewServeMux(), // Unauthenticated section
}
mainMuxBuilder.Handle("/rest/", noCacheRestMux)
mainMuxBuilder.HandleFunc("/qr/", s.getQR)

// Handle the special meta.js path
mux.HandleFunc("/meta.js", s.getJSMetadata)
unauthenticatedMux.Handle("/meta.js", handler)
mainMuxBuilder.HandleFunc("/meta.js", s.getJSMetadata)

// Serve compiled in assets unless an asset directory was set (for development)
unauthenticatedMux.Handle("/", s.statics)
mainMuxBuilder.mainMux.Handle("/", s.statics)

srv := http.Server{
// Redirect to HTTPS if we are supposed to
Handler: debugMiddleware(redirectToHTTPSMiddleware(guiCfg.UseTLS(), unauthenticatedMux)),
Handler: debugMiddleware(redirectToHTTPSMiddleware(guiCfg.UseTLS(), mainMuxBuilder.mainMux)),
// ReadTimeout must be longer than SyncthingController $scope.refresh
// interval to avoid HTTP keepalive/GUI refresh race.
ReadTimeout: 15 * time.Second,
Expand Down Expand Up @@ -446,6 +447,27 @@ func (s *service) Serve(ctx context.Context) error {
return err
}

// The purpose of splitMux is to help have an unauthenticated section of the API
// and have everything else fall back to the authenticated segment.
// The Handle and HandleFunc methods here let you call a "handle" function once
// but have the path set up in both the unauthenticated "top" mux and the fallback mux.
// The fallbackHandler is also needed since the fallbackHandler wraps the fallbackMux,
// but routing paths are registered in the fallbackMux,
// and the mainMux needs to be configured with the same paths.
type splitMux struct {
fallbackHandler *http.Handler
fallbackMux *http.ServeMux
mainMux *http.ServeMux
}
func (s *splitMux) Handle(path string, handler http.Handler) {
s.fallbackMux.Handle(path, handler)
s.mainMux.Handle(path, *s.fallbackHandler)
}
func (s *splitMux) HandleFunc(path string, handler http.HandlerFunc) {
s.fallbackMux.Handle(path, handler)
s.mainMux.Handle(path, *s.fallbackHandler)
}

// Complete implements suture.IsCompletable, which signifies to the supervisor
// whether to stop restarting the service.
func (s *service) Complete() bool {
Expand Down

0 comments on commit 9785197

Please sign in to comment.