Skip to content

Commit

Permalink
web: include basePath in hyperlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdecaf committed May 21, 2021
1 parent cb00d54 commit 70c24df
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cmd/ach-web-viewer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func main() {
// Register API routes with listers
listers := createFileListers(env.Config.Sources)
api.AppendRoutes(env.Logger, env.PublicRouter, listers)
web.AppendRoutes(env, listers)
web.AppendRoutes(env, listers, env.Config.Servers.Public.BasePath)

stopServers := env.RunServers(termListener)
defer stopServers()
Expand Down
14 changes: 7 additions & 7 deletions pkg/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import (
"github.com/gorilla/mux"
)

func AppendRoutes(env *service.Environment, listers filelist.Listers) {
env.PublicRouter.Methods("GET").Path("/").HandlerFunc(listFiles(env.Logger, listers))
env.PublicRouter.Methods("GET").PathPrefix("/sources/{sourceID}/").HandlerFunc(getFile(env.Logger, env.Config.Display, listers))
func AppendRoutes(env *service.Environment, listers filelist.Listers, basePath string) {
env.PublicRouter.Methods("GET").Path("/").HandlerFunc(listFiles(env.Logger, listers, basePath))
env.PublicRouter.Methods("GET").PathPrefix("/sources/{sourceID}/").HandlerFunc(getFile(env.Logger, env.Config.Display, listers, basePath))
}

func listFiles(logger log.Logger, listers filelist.Listers) http.HandlerFunc {
func listFiles(logger log.Logger, listers filelist.Listers, basePath string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
resp, err := listers.GetFiles()
if err != nil {
Expand All @@ -32,17 +32,17 @@ func listFiles(logger log.Logger, listers filelist.Listers) http.HandlerFunc {
fmt.Fprintf(w, "<strong>%s</strong> (%s)", files.SourceID, files.SourceType)
for i := range files.Files {
fullName := fmt.Sprintf("%s%s", files.Files[i].StoragePath, files.Files[i].Name)
fmt.Fprintf(w, `<br /><a href="/sources/%s/%s">%s</a>`, files.SourceID, fullName, files.Files[i].Name)
fmt.Fprintf(w, `<br /><a href="%s/sources/%s/%s">%s</a>`, basePath, files.SourceID, fullName, files.Files[i].Name)
}
fmt.Fprint(w, "<br /><br />")
}
}
}

func getFile(logger log.Logger, cfg service.DisplayConfig, listers filelist.Listers) http.HandlerFunc {
func getFile(logger log.Logger, cfg service.DisplayConfig, listers filelist.Listers, basePath string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
sourceID := mux.Vars(r)["sourceID"]
path := strings.TrimPrefix(r.URL.Path, fmt.Sprintf("/sources/%s/", sourceID))
path := strings.TrimPrefix(r.URL.Path, fmt.Sprintf("%s/sources/%s/", basePath, sourceID))

file, err := listers.GetFile(sourceID, path)
if err != nil {
Expand Down

0 comments on commit 70c24df

Please sign in to comment.