Fileserver
A simple static fileserver. Serves requests to local files in a directory of your choosing so that you can preview files in your browser.
Behavior
When visiting a path in your browser, the fileserver will perform the following actions to determine what to respond with:
- If the path exists:
- If the path is a file, its contents are served
- If the path is a directory:
- If the directory contains a file named "index.html," that file's contents are served
- Otherwise, a default directory listing, containing a list of directory entries, is rendered
- If the path does not exist:
- If a file with the same name but a ".html" extension appended exists in the same directory, that file is served
- Otherwise, a page indicating that the file could not be found is rendered
Usage
The fileserver can be invoked from either the command line or the Go API. In both instances you can choose which port to bind the server to, and which directory to serve files from.
CLI
$ fileserver # serves the current directory at localhost:4000
$ fileserver --port 3000 # serves the current directory at localhost:3000
$ fileserver ./documents # serves the ./documents directory at localhost:4000
Go
package main
import (
"github.com/itsliamegan/fileserver"
)
func main() {
root := "./"
addr := "127.0.0.1:4000"
fileserver.Start(root, addr)
}
Installation
- Clone the repository and
cd
into the directory - Run
go build
- Optionally, move the newly created
fileserver
executable to a directory in yourPATH
such as/usr/local/bin/