Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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