Answer the question
In order to leave comments, you need to log in
How to organize access to static files?
Tell me if there is any simple way to organize access to static files using the standard library. Those. Roughly speaking, you need to allow access to the stat. files to authorized users and deny guests.
Answer the question
In order to leave comments, you need to log in
type MyFileServer struct {
server http.Handler
}
func (h MyFileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
//Свою логику проверки паролей/логинов
if r.Header.Get("login") == "admin" && r.Header.Get("password") == "password" {
h.server.ServeHTTP(w, r)
} else {
http.NotFound(w, r)
}
}
func main() {
fs := MyFileServer{server: http.FileServer(http.Dir("static"))}
http.Handle("/", fs)
http.ListenAndServe(":8000", nil)
}
curl -H "login:admin" -H "password:password" 127.0.0.1:8000/test.txt
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question