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

Add cl flag to make default endpoint serve caches or origins #36

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions cmd/director.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,10 @@ func init() {
if err != nil {
panic(err)
}

directorServeCmd.Flags().StringP("default-endpoint", "e", "", "Set whether the default endpoint should redirect to caches or origins")
err = viper.BindPFlag("DirectorDefaultEndpoint", directorServeCmd.Flags().Lookup("default-endpoint"))
if err != nil {
panic(err)
}
}
12 changes: 9 additions & 3 deletions cmd/director_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"crypto/elliptic"
"errors"
"os"
"os/signal"
"syscall"
Expand Down Expand Up @@ -52,9 +53,14 @@ func serveDirector( /*cmd*/ *cobra.Command /*args*/, []string) error {
return err
}

// Use the shortcut middleware so that GET /foo/bar
// acts the same as GET /api/v1.0/director/object/foo/bar
engine.Use(director.ShortcutMiddleware())
// Configure the shortcut middleware to either redirect to a cache
// or to an origin
defaultEndpoint := viper.GetString("DirectorDefaultEndpoint")
if !(defaultEndpoint == "cache" || defaultEndpoint == "origin" || defaultEndpoint == "") {
return errors.New("The director's default endpoint must either be set to 'cache' or 'origin'." +
" Was there a typo?")
}
engine.Use(director.ShortcutMiddleware(defaultEndpoint))
director.RegisterDirector(engine.Group("/"))

log.Info("Starting web engine...")
Expand Down
31 changes: 21 additions & 10 deletions director/redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,29 @@ func RedirectToOrigin(ginCtx *gin.Context) {

// Middleware sends GET /foo/bar to the RedirectToCache function, as if the
// original request had been made to /api/v1.0/director/object/foo/bar
func ShortcutMiddleware() gin.HandlerFunc {
func ShortcutMiddleware(defaultEndpoint string) gin.HandlerFunc {
return func(c *gin.Context) {
if !strings.HasPrefix(c.Request.URL.Path, "/api/v1.0/director") {
// If not, redirect to the RedirectToCache route
c.Request.URL.Path = "/api/v1.0/director/object" + c.Request.URL.Path
RedirectToCache(c)
c.Abort()
return
}
// If we're configured for cache mode or we haven't set the flag,
// we should use cache middleware
if defaultEndpoint == "cache" || defaultEndpoint == "" {
if !strings.HasPrefix(c.Request.URL.Path, "/api/v1.0/director") {
c.Request.URL.Path = "/api/v1.0/director/object" + c.Request.URL.Path
RedirectToCache(c)
c.Abort()
return
}

// If the path starts with the correct prefix, continue with the next handler
c.Next()
// If the path starts with the correct prefix, continue with the next handler
c.Next()
} else if defaultEndpoint == "origin" {
if !strings.HasPrefix(c.Request.URL.Path, "/api/v1.0/director") {
c.Request.URL.Path = "/api/v1.0/director/origin" + c.Request.URL.Path
RedirectToOrigin(c)
c.Abort()
return
}
c.Next()
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion images/supervisord/pelican_director_serve.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[program:pelican_director_serve]
command=/pelican/osdf-client director serve -p %(ENV_OSDF_DIRECTOR_PORT)s
command=/pelican/osdf-client director serve -p %(ENV_OSDF_DIRECTOR_PORT)s -e %(ENV_OSDF_DIRECTOR_DEFAULT_ENDPOINT)s
autostart=false
autorestart=true
redirect_stderr=true