Skip to content

Instantly share code, notes, and snippets.

@cassava
Created January 28, 2015 16:01
Show Gist options
  • Save cassava/b7af4e2779a3494a3f63 to your computer and use it in GitHub Desktop.
Save cassava/b7af4e2779a3494a3f63 to your computer and use it in GitHub Desktop.
Serve a part of your filesystem while password protecting it.
// Copyright (c) 2014, Ben Morgan. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
package main
import (
"flag"
"log"
"net/http"
"github.com/goji/httpauth"
)
var (
listen = flag.String("http", "localhost:34537", "listen on this address")
username = flag.String("user", "nobody", "username for authentication")
password = flag.String("pass", "", "password for authentication")
)
func main() {
flag.Parse()
root := "."
if flag.NArg() == 1 {
root = flag.Arg(0)
}
log.Printf("Serving filesystem at %s.", root)
var handler http.Handler
if *username != "" && *password != "" {
log.Printf("Requiring authentication as %s.", *username)
handler = httpauth.SimpleBasicAuth(*username, *password)(http.FileServer(http.Dir(root)))
} else {
log.Println("Warning: no authentication required.")
handler = http.FileServer(http.Dir(root))
}
log.Printf("Listening on %s...", *listen)
log.Fatal(http.ListenAndServe(*listen, handler))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment