Skip to content

Commit

Permalink
Add a prefix option for serving from non-root urls
Browse files Browse the repository at this point in the history
  • Loading branch information
lxt2 committed Aug 5, 2017
1 parent e37fe65 commit 178f5bb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 22 deletions.
4 changes: 4 additions & 0 deletions main.go
Expand Up @@ -281,6 +281,10 @@ func configureServerCommand(app *kingpin.Application) error {
Default(s.Bind).
StringVar(&s.Bind)

cmd.Flag("prefix", "A path prefix for the server").
Default("/").
StringVar(&s.PathPrefix)

cmd.Flag("passphrase", "Require a passphrase to view web interface").
Short('p').
StringVar(&s.Passphrase)
Expand Down
58 changes: 37 additions & 21 deletions server/handler.go
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"net/http"
"net/url"
"path"
"strings"

"github.com/Sirupsen/logrus"
Expand All @@ -25,18 +26,12 @@ const (
)

var (
log = logger.Logger
apiRoutePrefixes = []string{
"/torznab/",
"/torrentpotato/",
"/download/",
"/xhr/",
"/debug/",
}
log = logger.Logger
)

type Params struct {
BaseURL string
PathPrefix string
APIKey []byte
Passphrase string
Config config.Config
Expand All @@ -52,13 +47,22 @@ type handler struct {

func NewHandler(p Params) (http.Handler, error) {
h := &handler{
Params: p,
FileHandler: http.FileServer(FS(false)),
indexers: map[string]torznab.Indexer{},
Params: p,
FileHandler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("Loading %s from fs", r.URL.RequestURI())
http.FileServer(FS(false)).ServeHTTP(w, r)
}),
indexers: map[string]torznab.Indexer{},
}

router := mux.NewRouter()

// apply the path prefix
if h.Params.PathPrefix != "/" && h.Params.PathPrefix != "" {
router = router.PathPrefix(h.Params.PathPrefix).Subrouter()
h.FileHandler = http.StripPrefix(h.Params.PathPrefix, h.FileHandler)
}

// torznab routes
router.HandleFunc("/torznab/{indexer}", h.torznabHandler).Methods("GET")
router.HandleFunc("/torznab/{indexer}/api", h.torznabHandler).Methods("GET")
Expand All @@ -82,6 +86,10 @@ func NewHandler(p Params) (http.Handler, error) {
router.HandleFunc("/xhr/auth", h.postAuthHandler).Methods("POST")
router.HandleFunc("/xhr/version", h.getVersionHandler).Methods("GET")

// anything else
router.PathPrefix("/").Handler(h.FileHandler)
router.PathPrefix("/static").Handler(h.FileHandler)

h.Handler = router
return h, h.initialize()
}
Expand All @@ -108,15 +116,30 @@ func (h *handler) initialize() error {
}
h.Params.APIKey = k
}

// Walk routes for debugging
err := h.Handler.(*mux.Router).Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
path, err := route.GetPathTemplate()
if err != nil {
return err
}
log.Debugf("Responds to %s", path)
return nil
})
if err != nil {
return err
}

return nil
}

func (h *handler) baseURL(r *http.Request, path string) (*url.URL, error) {
func (h *handler) baseURL(r *http.Request, appendPath string) (*url.URL, error) {
proto := "http"
if r.TLS != nil {
proto = "https"
}
return url.Parse(fmt.Sprintf("%s://%s%s", proto, r.Host, path))
return url.Parse(fmt.Sprintf("%s://%s%s", proto, r.Host,
path.Join(h.Params.PathPrefix, appendPath)))
}

func (h *handler) createIndexer(key string) (torznab.Indexer, error) {
Expand Down Expand Up @@ -190,14 +213,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
"remote": r.RemoteAddr,
}).Debugf("%s %s", r.Method, r.URL.RequestURI())

for _, prefix := range apiRoutePrefixes {
if strings.HasPrefix(r.URL.Path, prefix) {
h.Handler.ServeHTTP(w, r)
return
}
}

h.FileHandler.ServeHTTP(w, r)
h.Handler.ServeHTTP(w, r)
}

func (h *handler) torznabHandler(w http.ResponseWriter, r *http.Request) {
Expand Down
10 changes: 9 additions & 1 deletion server/server.go
Expand Up @@ -13,6 +13,7 @@ import (
// Server is an http server which wraps the Handler
type Server struct {
Bind, Port, Passphrase string
PathPrefix string
Hostname string
version string
config config.Config
Expand All @@ -29,6 +30,11 @@ func New(conf config.Config, version string) (*Server, error) {
return nil, err
}

prefix, err := config.GetGlobalConfig("pathprefix", "", conf)
if err != nil {
return nil, err
}

passphrase, err := config.GetGlobalConfig("passphrase", "", conf)
if err != nil {
return nil, err
Expand All @@ -47,6 +53,7 @@ func New(conf config.Config, version string) (*Server, error) {
Bind: bind,
Port: port,
Passphrase: passphrase,
PathPrefix: prefix,
config: conf,
version: version,
}, nil
Expand Down Expand Up @@ -94,8 +101,9 @@ func (s *Server) Listen() error {
logger.Logger.Infof("Listening on %s", listenOn)

h, err := NewHandler(Params{
BaseURL: fmt.Sprintf("http://%s:%s/", s.Hostname, s.Port),
BaseURL: fmt.Sprintf("http://%s:%s%s", s.Hostname, s.Port, s.PathPrefix),
Passphrase: s.Passphrase,
PathPrefix: s.PathPrefix,
Config: s.config,
Version: s.version,
})
Expand Down

0 comments on commit 178f5bb

Please sign in to comment.