Skip to content

Instantly share code, notes, and snippets.

@jbuchbinder
Created June 19, 2013 18:22
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jbuchbinder/5816604 to your computer and use it in GitHub Desktop.
Save jbuchbinder/5816604 to your computer and use it in GitHub Desktop.
Gorilla mux + basic authentication from htpasswd file
package main
import (
auth "github.com/abbot/go-http-auth"
"github.com/gorilla/mux"
"log"
"net/http"
"time"
)
func main() {
r := mux.NewRouter()
// Define paths
sub := r.PathPrefix("/api").Subrouter()
// ... define other handling bits and routing
s := &http.Server{
Addr: BIND_ADDRESS,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
h := auth.HtpasswdFileProvider(HTPASSWD_FILE)
a := auth.NewBasicAuthenticator("Basic Realm", h)
http.Handle("/", a.Wrap(func(w http.ResponseWriter, ar *auth.AuthenticatedRequest) {
r.ServeHTTP(w, &ar.Request)
}))
log.Err(s.ListenAndServe().Error())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment