Skip to content

Instantly share code, notes, and snippets.

@gorakhargosh
Created November 25, 2015 00:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gorakhargosh/7bab8be451205ba21b08 to your computer and use it in GitHub Desktop.
Save gorakhargosh/7bab8be451205ba21b08 to your computer and use it in GitHub Desktop.
// A static file server written in Go.
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
)
type Options struct {
// The port on which the server should listen.
Port int
// The address on which the server should listen.
Host string
}
func main() {
var options Options
flag.IntVar(&options.Port, "port", 3000, "the port on which to listen")
flag.StringVar(&options.Host, "host", "localhost", "the host on which to listen")
flag.Parse()
var dir string
var err error
if len(flag.Args()) > 0 {
dir = flag.Arg(0)
} else {
dir, err = os.Getwd()
if err != nil {
log.Fatal("cannot determine current working directory")
}
}
fs := http.FileServer(http.Dir(dir))
http.Handle("/", fs)
log.Printf("Listening on %s:%d...\n", options.Host, options.Port)
http.ListenAndServe(fmt.Sprintf(":%d", options.Port), nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment