Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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