diff --git a/go.mod b/go.mod index 379c750..04bebba 100644 --- a/go.mod +++ b/go.mod @@ -4,11 +4,13 @@ go 1.13 require ( github.com/pelletier/go-toml v1.6.0 // indirect + github.com/prologic/go-gopher v0.0.0-20191226035442-664dbdb49f44 github.com/spf13/afero v1.2.2 // indirect github.com/spf13/cast v1.3.1 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/viper v1.6.2 + github.com/stryan/go-gopher v0.0.0-20191008152201-adba90945dcb // indirect golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4 // indirect golang.org/x/text v0.3.2 // indirect gopkg.in/ini.v1 v1.52.0 // indirect diff --git a/main.go b/main.go index d9ca666..40d0fa3 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "log" + "path" "sync" "github.com/prologic/go-gopher" @@ -51,8 +52,10 @@ func main() { log.Printf("Starting gopherhole %v %v", i, h.Hostname) go func(h interface{}) { hole := h.(GopherConfig) - gopher.Handle("/", gopher.FileServer(gopher.Dir(hole.RootDir))) - log.Fatal(gopher.ListenAndServe(hole.Hostname+":"+hole.Port, nil)) + gopher.Handle("/", index(gopher.Dir(hole.RootDir))) + //log.Fatal(gopher.ListenAndServe(hole.Hostname+":"+hole.Port, nil)) + server := &gopher.Server{Addr: ":" + hole.Port, Hostname: hole.Hostname, Handler: nil} + log.Fatal(server.ListenAndServe()) wg.Done() }(h) } @@ -80,3 +83,24 @@ func (c *GeminiConfig) String() string { func (c *GopherConfig) String() string { return fmt.Sprintf("Gopher Config: %v:%v Files:%v", c.Hostname, c.Port, c.RootDir) } + +type indexHandler struct { + rootPath string + rootHandler gopher.Handler +} + +func (f *indexHandler) ServeGopher(w gopher.ResponseWriter, r *gopher.Request) { + upath := r.Selector + if gopher.GetItemType(f.rootPath+upath) == gopher.DIRECTORY && upath != "/" { + w.WriteItem(&gopher.Item{ + Type: gopher.DIRECTORY, + Selector: path.Dir(upath), + Description: "Go Back", + }) + } + f.rootHandler.ServeGopher(w, r) +} + +func index(root gopher.FileSystem) *indexHandler { + return &indexHandler{root.Name(), gopher.FileServer(root)} +}