Last active
December 20, 2015 18:09
-
-
Save marcinwyszynski/6173835 to your computer and use it in GitHub Desktop.
Statically serve the content of a directory. By default it picks up the current working directory but you can also pass the path using the --path command-line flag.
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
// Statically serve the content of a directory. By default it picks up the | |
// current working directory but you can also pass the path using the --path | |
// command-line flag. | |
package main | |
import ( | |
"flag" | |
"fmt" | |
"log" | |
"net/http" | |
"os" | |
"time" | |
) | |
var ( | |
port = flag.Int("port", 3000, "Port on which to serve") | |
path = flag.String("path", "", "Path to the directory") | |
) | |
func main() { | |
flag.Parse() | |
if *path == "" { | |
pwd, err := os.Getwd() | |
if err != nil { | |
log.Fatal(err) | |
} | |
*path = pwd | |
} | |
// Make sure we 'touch' the path so that the browser does not do | |
// any caching which would be insanely confusing. | |
if err := os.Chtimes(*path, time.Now(), time.Now()); err != nil { | |
log.Fatal(err) | |
} | |
http.Handle("/", http.FileServer(http.Dir(*path))) | |
log.Printf("Serving content of '%s' on port %d", *path, *port) | |
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment