I
I
inbider2016-10-24 00:45:10
go
inbider, 2016-10-24 00:45:10

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

1 answer(s)
I
Ilya, 2016-10-24
@inbider

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)
}

made on headers
curl -H "login:admin" -H "password:password" 127.0.0.1:8000/test.txt

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question