Skip to content

Instantly share code, notes, and snippets.

@marklap
Created October 12, 2015 23:31
Show Gist options
  • Save marklap/5c8d848b7db337984f9c to your computer and use it in GitHub Desktop.
Save marklap/5c8d848b7db337984f9c to your computer and use it in GitHub Desktop.
A very simple HTTP file server - no security, customize directory to serve and customize listening IP:port via envvars (that's it)
////////////////////////////////////////////////////////////////////////////////
// The MIT License (MIT)
//
// Copyright (c) 2015 Mark LaPerriere
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
////////////////////////////////////////////////////////////////////////////////
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
)
func main() {
flag.Usage = func() {
fmt.Println(`usage: httpfileserv
export HTTPFS_WORKINGDIR=<dir> sets the working dir of the HTTP file server [default: .]
export HTTPFS_BINDADDRESS=<ip:port> sets the listening IP address and port [default ':8080']
`)
}
flag.Parse()
workingDir := os.Getenv("HTTPFS_WORKINGDIR")
if workingDir == "" {
workingDir, _ = filepath.Abs(".")
}
bindAddress := os.Getenv("HTTPFS_BINDADDRESS")
if bindAddress == "" {
bindAddress = ":8080"
}
log.Println(fmt.Sprintf("Serving files in '%s' on '%s'", workingDir, bindAddress))
log.Fatal(http.ListenAndServe(bindAddress, http.FileServer(http.Dir(workingDir))))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment