fileserver

package module
v0.0.0-...-e956153 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 16, 2018 License: BSD-3-Clause Imports: 17 Imported by: 0

README

golang fileserver

What is this?

There was a feature request (golang/go#23898) posted on golang to serve directory listings in JSON instead of HTML. This would mimick similar behavior found in NGINX's ngx_http_autoindex_module.

How do I use it?

This is a direct drop-in replacement for func FileServer(root FileSystem) Handler. Just import this package and use the New function.

Any request sent to the file server with the Accept header set to application/json will respond with a json response.

Example
// main.go
package main

import (
	"log"
	"net/http"

	"github.com/jasonwbarnett/fileserver"
)

func main() {
	// Simple static webserver:
	log.Fatal(http.ListenAndServe(":8080", fileserver.New(http.Dir("/usr/share/doc"))))
}
go run main.go

List directory contents as JSON

$ curl 'http://127.0.0.1:8080/' -H 'Accept: application/json' | jq '.'
[
  {
    "name": "bash",
    "type": "directory",
    "mtime": "2017-07-15T14:09:01-07:00"
  },
  {
    "name": "cups",
    "type": "directory",
    "mtime": "2017-12-10T16:16:59-08:00"
  },
  {
    "name": "groff",
    "type": "directory",
    "mtime": "2017-07-15T14:07:46-07:00"
  },
  {
    "name": "ntp",
    "type": "directory",
    "mtime": "2017-05-04T17:21:03-07:00"
  },
  {
    "name": "postfix",
    "type": "directory",
    "mtime": "2017-07-25T18:47:14-07:00"
  }
]

List directory contents as HTML

$ curl 'http://127.0.0.1:8080/'
<pre>
<a href="bash/">bash/</a>
<a href="cups/">cups/</a>
<a href="groff/">groff/</a>
<a href="ntp/">ntp/</a>
<a href="postfix/">postfix/</a>
</pre>

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(root http.FileSystem) http.Handler

New returns a handler that serves HTTP requests with the contents of the file system rooted at root.

To use the operating system's file system implementation, use http.Dir:

http.Handle("/", fileserver.New(http.Dir("/tmp")))

As a special case, the returned file server redirects any request ending in "/index.html" to the same path, without the final "index.html".

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL