Skip to content

Instantly share code, notes, and snippets.

@lambrospetrou
Last active September 13, 2017 21:18
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 lambrospetrou/7b582954357778a42cc6e98477d32910 to your computer and use it in GitHub Desktop.
Save lambrospetrou/7b582954357778a42cc6e98477d32910 to your computer and use it in GitHub Desktop.
A simple HTTP server to serve a directory instead of python SimpleHTTPServer
// To get the code git clone it as shown below:
// git clone https://gist.github.com/lambrospetrou/7b582954357778a42cc6e98477d32910.git gohttp
//
// Run either by `go run gohttp.go`
// Or do a `go install` and run it from your path directly with `gohttp`
// Example: `gohttp -d someDir/`
package main
import (
"flag"
"fmt"
"log"
"net/http"
"path"
)
func main() {
port := flag.String("p", "11111", "Port to use")
directory := flag.String("d", ".", "Directory to serve")
flag.Parse()
var servedDir string = *directory
if !path.IsAbs(*directory) {
servedDir = path.Join(path.Dir("."), servedDir)
}
fmt.Println("Starting gohttp at ", *port, "and serving ", servedDir)
log.Fatal(http.ListenAndServe(":"+*port, http.FileServer(http.Dir(servedDir))))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment