From 0a2e1b9a05474597b418201aa64c839d25f2edc1 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Mon, 29 Apr 2019 11:14:43 +0200 Subject: [PATCH] Add handler for single jpeg image Signed-off-by: Knut Ahlers --- main.go | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index dce9622..6229acd 100644 --- a/main.go +++ b/main.go @@ -10,9 +10,10 @@ import ( "strconv" "sync" - rconfig "github.com/Luzifer/rconfig/v2" "github.com/gofrs/uuid" log "github.com/sirupsen/logrus" + + rconfig "github.com/Luzifer/rconfig/v2" ) var ( @@ -46,7 +47,7 @@ func init() { } if cfg.VersionAndExit { - fmt.Printf("imgdecode %s\n", version) + fmt.Printf("cam2mjpeg %s\n", version) os.Exit(0) } @@ -59,6 +60,7 @@ func init() { func main() { http.HandleFunc("/mjpeg", handle) + http.HandleFunc("/snapshot.jpg", handleSnapshot) go func() { log.WithError(http.ListenAndServe(cfg.Listen, nil)).Fatal("HTTP server has gone") }() @@ -146,6 +148,26 @@ func handle(res http.ResponseWriter, r *http.Request) { handleMJPEG(res, r, imgChan, uid) } +func handleSnapshot(w http.ResponseWriter, r *http.Request) { + imgChan := make(chan []byte, 10) + uid := uuid.Must(uuid.NewV4()).String() + + defer func() { + deregisterImgChan(uid) + close(imgChan) + }() + + registerImgChan(uid, imgChan) + + img := <-imgChan + + w.Header().Add("Cache-Control", "no-store, no-cache") + w.Header().Add("Connection", "close") + w.Header().Set("Content-Type", "image/jpeg") + + w.Write(img) +} + func registerImgChan(id string, ic chan []byte) { requesterLock.Lock() defer requesterLock.Unlock()