Вопрос пользователя:
Ответ нейросети:
package main
import ( "fmt" "net/http" "strings" )
func main() { http.HandleFunc("/answer/", Authorization(AnswerHandler)) http.ListenAndServe(":8080", nil) }
func Authorization(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
authHeader := r.Header.Get("Authorization")
if authHeader == "" {
w.Header().Set("WWW-Authenticate", Basic realm="Restricted"
)
w.WriteHeader(http.StatusUnauthorized)
return
}
auth := strings.SplitN(authHeader, " ", 2)
if len(auth) != 2 || auth[0] != "Basic" {
w.WriteHeader(http.StatusUnauthorized)
return
}
// Decode the base64 encoded credentials
credentials, err := base64.StdEncoding.DecodeString(auth[1])
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
// Check the credentials (for example, username:password = admin:password)
if string(credentials) != "admin:password" {
w.WriteHeader(http.StatusUnauthorized)
return
}
// Call the next handler if authorization is successful
next(w, r)
}
}
func AnswerHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "The answer is 42") }
Напишите нам, если в вопросе есть ваши персональные данные ([email protected])
Обратите внимание: ответы, предоставляемые искусственным интеллектом, могут не всегда быть точными. Не рассчитывайте на них в критически важных областях, таких как медицина, юриспруденция, финансы или в вопросах, связанных с безопасностью. Для важных решений всегда обращайтесь к квалифицированным специалистам. Администрация сайта не несет ответственности за контент, сгенерированный автоматически.