Created
November 25, 2015 00:15
-
-
Save gorakhargosh/7bab8be451205ba21b08 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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