8000 GitHub - mwalto7/httpware: A collection of useful Go HTTP middleware functions.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

mwalto7/httpware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

httpware PkgGoDev Go Report Card Test

A collection of useful Go HTTP middleware functions.

go get github.com/mwalto7/httpware

Authentication

Need HTTP Basic Auth for your routes? Simply wrap your handlers with httpware.BasicAuth:

package main

import (
    "fmt"
    "net/http"

    "github.com/mwalto7/httpware"
)

func main() {
    authenticate := httpware.BasicAuth(httpware.BasicAuthOptions{
        Realm: "My super secure path.",
        AuthFunc: func(username, password string, _ *http.Request) bool {
            // Don't do this in production.
            return username == "user" && password == "pass"
        },
    })
    mux := http.NewServeMux()
    mux.Handle("/foo", authenticate(handleFoo()))
    _ = http.ListenAndServe(":8080", mux)
}

func handleFoo() http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        w.Header().Add("Content-Type", "text/plain")
        w.WriteHeader(http.StatusOK)
        _, _ = fmt.Fprintln(w, "You're authenticated 🙂") 
    }
}

About

A collection of useful Go HTTP middleware functions.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

0