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:

  1. If the path exists:
    1. If the path is a file, its contents are served
    2. If the path is a directory:
      1. If the directory contains a file named “index.html,” that file’s
        contents are served
      2. Otherwise, a default directory listing, containing a list of directory
        entries, is rendered
  2. If the path does not exist:
    1. If a file with the same name but a “.html” extension appended exists in
      the same directory, that file is served
    2. 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

  1. Clone the repository and cd into the directory
  2. Run go build
  3. Optionally, move the newly created fileserver executable to a directory in your
    PATH such as /usr/local/bin/

GitHub

View Github