added redirect anchors
This commit is contained in:
parent
5e5b73884b
commit
b13ff70a2d
4 changed files with 44 additions and 18 deletions
2
Makefile
2
Makefile
|
|
@ -37,7 +37,7 @@ govet:
|
||||||
$(CC) vet ./...
|
$(CC) vet ./...
|
||||||
|
|
||||||
misspell:
|
misspell:
|
||||||
$(GOPATH)/bin/misspell cmd/* pkg/* Makefile README.md sri.sh
|
$(GOPATH)/bin/misspell cmd/* pkg/* Makefile README.md
|
||||||
|
|
||||||
staticcheck:
|
staticcheck:
|
||||||
$(GOPATH)/bin/staticcheck ./...
|
$(GOPATH)/bin/staticcheck ./...
|
||||||
|
|
|
||||||
|
|
@ -7,14 +7,9 @@ import (
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type dirContents struct {
|
|
||||||
AllPaths []string `xml:"a"`
|
|
||||||
Dirs []string `xml:"-"`
|
|
||||||
Files []string `xml:"-"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type statusInterceptor struct {
|
type statusInterceptor struct {
|
||||||
w http.ResponseWriter
|
w http.ResponseWriter
|
||||||
status int
|
status int
|
||||||
|
|
@ -23,13 +18,16 @@ type statusInterceptor struct {
|
||||||
func (s *statusInterceptor) Header() http.Header {
|
func (s *statusInterceptor) Header() http.Header {
|
||||||
return s.w.Header()
|
return s.w.Header()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *statusInterceptor) Write(p []byte) (int, error) {
|
func (s *statusInterceptor) Write(p []byte) (int, error) {
|
||||||
return s.w.Write(p)
|
return s.w.Write(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *statusInterceptor) WriteHeader(statusCode int) {
|
func (s *statusInterceptor) WriteHeader(statusCode int) {
|
||||||
s.w.WriteHeader(statusCode)
|
s.w.WriteHeader(statusCode)
|
||||||
s.status = statusCode
|
s.status = statusCode
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *statusInterceptor) Status() int {
|
func (s *statusInterceptor) Status() int {
|
||||||
if s.status == 0 {
|
if s.status == 0 {
|
||||||
return http.StatusOK
|
return http.StatusOK
|
||||||
|
|
@ -37,6 +35,23 @@ func (s *statusInterceptor) Status() int {
|
||||||
return s.status
|
return s.status
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type dirContents struct {
|
||||||
|
AllPaths []string `xml:"a"`
|
||||||
|
Dirs []string `xml:"-"`
|
||||||
|
Files files `xml:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type file struct {
|
||||||
|
Name string
|
||||||
|
Anchor string
|
||||||
|
}
|
||||||
|
|
||||||
|
type files []file
|
||||||
|
|
||||||
|
func (f files) Len() int { return len(f) }
|
||||||
|
func (f files) Less(i, j int) bool { return f[i].Name < f[j].Name }
|
||||||
|
func (f files) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
|
||||||
|
|
||||||
type responseInterceptor struct {
|
type responseInterceptor struct {
|
||||||
buf bytes.Buffer
|
buf bytes.Buffer
|
||||||
w http.ResponseWriter
|
w http.ResponseWriter
|
||||||
|
|
@ -46,18 +61,22 @@ type responseInterceptor struct {
|
||||||
func (r *responseInterceptor) Header() http.Header {
|
func (r *responseInterceptor) Header() http.Header {
|
||||||
return r.w.Header()
|
return r.w.Header()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *responseInterceptor) Write(p []byte) (int, error) {
|
func (r *responseInterceptor) Write(p []byte) (int, error) {
|
||||||
return r.buf.Write(p)
|
return r.buf.Write(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *responseInterceptor) WriteHeader(statusCode int) {
|
func (r *responseInterceptor) WriteHeader(statusCode int) {
|
||||||
r.status = statusCode
|
r.status = statusCode
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *responseInterceptor) Status() int {
|
func (r *responseInterceptor) Status() int {
|
||||||
if r.status == 0 {
|
if r.status == 0 {
|
||||||
return http.StatusOK
|
return http.StatusOK
|
||||||
}
|
}
|
||||||
return r.status
|
return r.status
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *responseInterceptor) GetPaths() (dir dirContents, err error) {
|
func (r *responseInterceptor) GetPaths() (dir dirContents, err error) {
|
||||||
err = xml.Unmarshal(r.buf.Bytes(), &dir)
|
err = xml.Unmarshal(r.buf.Bytes(), &dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -67,10 +86,18 @@ func (r *responseInterceptor) GetPaths() (dir dirContents, err error) {
|
||||||
if p[len(p)-1] == '/' {
|
if p[len(p)-1] == '/' {
|
||||||
dir.Dirs = append(dir.Dirs, p)
|
dir.Dirs = append(dir.Dirs, p)
|
||||||
} else {
|
} else {
|
||||||
dir.Files = append(dir.Files, p)
|
dir.Files = append(dir.Files, file{Name: p, Anchor: anchor(p)})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sort.Strings(dir.Dirs)
|
sort.Strings(dir.Dirs)
|
||||||
sort.Strings(dir.Files)
|
sort.Sort(dir.Files)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func anchor(s string) string {
|
||||||
|
if i := strings.LastIndex(s, "/"); i > 0 && i+1 < len(s) {
|
||||||
|
s = s[i+1:]
|
||||||
|
}
|
||||||
|
s = strings.ReplaceAll(s, " ", "_")
|
||||||
|
return strings.ToLower(s)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -37,9 +37,10 @@ func (fs *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fs.log.Info("access",
|
fs.log.Info("access",
|
||||||
"uri", r.RequestURI,
|
"client", r.RemoteAddr,
|
||||||
"method", r.Method,
|
"method", r.Method,
|
||||||
"status", s.Status(),
|
"status", s.Status(),
|
||||||
|
"uri", r.RequestURI,
|
||||||
)
|
)
|
||||||
}()
|
}()
|
||||||
upath := r.URL.Path
|
upath := r.URL.Path
|
||||||
|
|
@ -65,13 +66,11 @@ func (fs *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
if !d.IsDir() {
|
if !d.IsDir() {
|
||||||
if r.FormValue("p") == "t" {
|
if r.FormValue("p") == "t" {
|
||||||
fs.fs.Preload(p)
|
fs.fs.Preload(p)
|
||||||
idx := strings.LastIndex(p, "/")
|
rdir := "/"
|
||||||
if idx > 0 {
|
if i := strings.LastIndex(p, "/"); i > 0 {
|
||||||
p = p[:idx]
|
rdir = p[:i]
|
||||||
} else {
|
|
||||||
p = "/"
|
|
||||||
}
|
}
|
||||||
http.Redirect(w, r, p, http.StatusFound)
|
http.Redirect(w, r, rdir+"#"+anchor(p), http.StatusFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if r.FormValue("n") == "t" {
|
if r.FormValue("n") == "t" {
|
||||||
|
|
|
||||||
|
|
@ -28,8 +28,8 @@ var (
|
||||||
<tr><td><a href="{{$s}}">{{$s}}</a></td><td></td></tr>
|
<tr><td><a href="{{$s}}">{{$s}}</a></td><td></td></tr>
|
||||||
{{end -}}
|
{{end -}}
|
||||||
{{range $s := .Files -}}
|
{{range $s := .Files -}}
|
||||||
<tr><td><a href="{{$s}}">{{$s}}</a></td>
|
<tr><td><a id="{{$s.Anchor}}" href="{{$s.Name}}">{{$s.Name}}</a></td>
|
||||||
<td><a href="{{$s}}?v=t">[v]</a> <a href="{{$s}}?n=t">[n]</a> <a href="{{$s}}?p=t">[p]</a></td></tr>
|
<td><a href="{{$s.Name}}?v=t">[v]</a> <a href="{{$s.Name}}?n=t">[n]</a> <a href="{{$s.Name}}?p=t">[p]</a></td></tr>
|
||||||
{{end -}}
|
{{end -}}
|
||||||
</table>
|
</table>
|
||||||
</article>
|
</article>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue