mirror of
https://github.com/syumai/workers.git
synced 2025-03-11 01:39:11 +00:00
32 lines
632 B
Go
32 lines
632 B
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/syumai/workers"
|
|
)
|
|
|
|
const (
|
|
userName = "user"
|
|
userPassword = "password"
|
|
)
|
|
|
|
func authenticate(req *http.Request) bool {
|
|
username, password, ok := req.BasicAuth()
|
|
return ok && username == userName && password == userPassword
|
|
}
|
|
|
|
func handleRequest(w http.ResponseWriter, req *http.Request) {
|
|
if !authenticate(req) {
|
|
w.Header().Add("WWW-Authenticate", `Basic realm="login is required"`)
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
w.Write([]byte("Unauthorized\n"))
|
|
return
|
|
}
|
|
w.Write([]byte("Authorized!\n"))
|
|
}
|
|
|
|
func main() {
|
|
workers.Serve(http.HandlerFunc(handleRequest))
|
|
}
|