Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A static file server in Go
// A simple HTTP static file server.
// Usage:
// go run --root ~/Pictures --port 8001
package main
import (
// Router is a http.Handler that dispatches requests to different
// handler functions.
type Router struct {
// Root directory for serving files
root string
// Override the main http method to only serve files
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if !isValidPath(req.URL.Path) {
http.NotFound(w, req)
fs := http.FileServer(http.Dir(r.root))
fs.ServeHTTP(w, req)
// Check if the path is something we want to serve
func isValidPath(path string) bool {
isValid, err := regexp.Compile(`^([[:alnum:]/]+)(.png|.jpg)$`)
if err != nil {
return false
return isValid.MatchString(path)
// Check if the root path specified for the server is valid
func checkRoot(root string) error {
_, err := os.Stat(root)
if err != nil {
return errors.New(fmt.Sprintf("No such directory: %s", root))
return nil
var port = flag.String("port", ":8001", "Port on which the server will listen")
var root = flag.String("root", "/var/www/", "Root directory from where the files will be served")
func init() {
log.Println("Serving from", *root, "on port", *port)
err := checkRoot(*root)
if err != nil {
func main() {
err := http.ListenAndServe(*port, &Router{root: *root})
if err != nil {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment