Created
June 19, 2013 18:22
-
-
Save jbuchbinder/5816604 to your computer and use it in GitHub Desktop.
Gorilla mux + basic authentication from htpasswd file
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
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