Skip to content

Instantly share code, notes, and snippets.

@druska
Created October 4, 2013 00:17
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 druska/6819136 to your computer and use it in GitHub Desktop.
Save druska/6819136 to your computer and use it in GitHub Desktop.
A simple HTTP server which serves static files in the working directory. By default serves on port 8080 or takes a port argument. Created as a fast replacement to Python's (python -m SimpleHTTPServer). Example usage: gosimplehttp 8000
package main
import (
"flag"
"fmt"
"net/http"
"os"
)
func main() {
// Get the port
flag.Parse()
port := flag.Arg(0)
if len(port) == 0 {
port = "8080"
}
port = ":" + port
// Get the working directory
wd, err := os.Getwd()
if err != nil {
panic(err)
}
// Start the server
fmt.Printf("Server listening on http://localhost%s\nWatching directory %s\n", port, wd)
panic(http.ListenAndServe(port, http.FileServer(http.Dir(wd))))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment