Add images handler
This commit is contained in:
parent
859ee3e8be
commit
c531433d23
|
@ -0,0 +1,56 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type imagesHandler struct{
|
||||
name string
|
||||
auth func(*http.Request) (bool)
|
||||
hndlr http.Handler
|
||||
getter func(fname string) (Picture, error)
|
||||
}
|
||||
|
||||
func ImagesHandler(name string, auth func(*http.Request) (bool), hndlr http.Handler, getter func(fname string) (Picture, error)) (imagesHandler) {
|
||||
return imagesHandler{name, auth, hndlr, getter}
|
||||
}
|
||||
|
||||
func (i imagesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("Handling %s %s request from %s: %s [%s]\n", r.Method, i.name, r.RemoteAddr, r.URL.Path, r.UserAgent())
|
||||
|
||||
// Don't handle subdirectories
|
||||
if strings.Contains(r.URL.Path[1:], "/") {
|
||||
http.Error(w, "Image not found.", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Refuse most methods
|
||||
if r.Method != "GET" && r.Method != "HEAD" {
|
||||
http.Error(w, "Method not allowed.", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// Refuse content
|
||||
if r.ContentLength != 0 {
|
||||
http.Error(w, "This request doesn't accept content.", http.StatusRequestEntityTooLarge)
|
||||
return
|
||||
}
|
||||
|
||||
// Check rights
|
||||
if ! i.auth(r) {
|
||||
w.Header().Set("WWW-Authenticate", "Basic realm=\"YouP0m\"")
|
||||
http.Error(w, "You are not allowed to perform this request.", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
// Search the picture
|
||||
if pict, err := i.getter(r.URL.Path[1:]); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
return
|
||||
} else {
|
||||
r.URL.Path = "/" + pict.basename
|
||||
i.hndlr.ServeHTTP(w, r)
|
||||
}
|
||||
}
|
28
main.go
28
main.go
|
@ -44,8 +44,34 @@ func main() {
|
|||
authFunc := func (r *http.Request) (*User){ return Authenticate(*htpasswd, r) }
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/api/", http.StripPrefix("/api", ApiHandler(authFunc)))
|
||||
mux.Handle("/", http.FileServer(http.Dir("./static/")))
|
||||
mux.Handle("/api/", http.StripPrefix("/api", ApiHandler(authFunc)))
|
||||
|
||||
mux.Handle("/images/", http.StripPrefix("/images", ImagesHandler(
|
||||
"images",
|
||||
func (*http.Request) (bool){ return true; },
|
||||
http.FileServer(http.Dir(PublishedImgDir)),
|
||||
GetPublishedImage,
|
||||
)))
|
||||
mux.Handle("/images/next/", http.StripPrefix("/images/next", ImagesHandler(
|
||||
"next",
|
||||
func (r *http.Request) (bool){ return authFunc(r) != nil; },
|
||||
http.FileServer(http.Dir(NextImgDir)),
|
||||
GetNextImage,
|
||||
)))
|
||||
|
||||
mux.Handle("/images/thumbs/", http.StripPrefix("/images/thumbs", ImagesHandler(
|
||||
"thumbs",
|
||||
func (*http.Request) (bool){ return true; },
|
||||
http.FileServer(http.Dir(ThumbsDir)),
|
||||
GetPublishedImage,
|
||||
)))
|
||||
mux.Handle("/images/next/thumbs/", http.StripPrefix("/images/next/thumbs", ImagesHandler(
|
||||
"nexthumbs",
|
||||
func (r *http.Request) (bool){ return authFunc(r) != nil; },
|
||||
http.FileServer(http.Dir(ThumbsDir)),
|
||||
GetNextImage,
|
||||
)))
|
||||
|
||||
log.Println("Ready, listening on", *bind)
|
||||
if err := http.ListenAndServe(*bind, mux); err != nil {
|
||||
|
|
Loading…
Reference in New Issue