Skip to content

Instantly share code, notes, and snippets.

@prasincs
Forked from jbuchbinder/gorilla_auth.go
Created March 20, 2017 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prasincs/93a558aa1ebb45f1df6d3188c951fdfb to your computer and use it in GitHub Desktop.
Save prasincs/93a558aa1ebb45f1df6d3188c951fdfb 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