Update handlers

This commit is contained in:
Milan Nikolic
2025-06-13 20:36:55 +02:00
parent e5ee1a2049
commit 933b5eef22
5 changed files with 22 additions and 12 deletions

View File

@@ -32,12 +32,13 @@ func (h *HTML) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" && r.Method != "HEAD" {
msg := fmt.Sprintf("405 Method Not Allowed (%s)", r.Method)
http.Error(w, msg, http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(h.Template)
_, _ = w.Write(h.Template)
}
var html = `<html>

View File

@@ -17,10 +17,11 @@ func NewIndex() *Index {
func (i *Index) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" && r.Method != "HEAD" {
http.Error(w, "405 Method Not Allowed", http.StatusMethodNotAllowed)
return
}
w.Write([]byte(`<html>
_, _ = w.Write([]byte(`<html>
<head><title>cam2ip</title></head>
<body>
<h1>cam2ip</h1>

View File

@@ -22,6 +22,7 @@ func NewJPEG(reader reader.ImageReader) *JPEG {
func (j *JPEG) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" && r.Method != "HEAD" {
http.Error(w, "405 Method Not Allowed", http.StatusMethodNotAllowed)
return
}
@@ -32,12 +33,14 @@ func (j *JPEG) ServeHTTP(w http.ResponseWriter, r *http.Request) {
img, err := j.reader.Read()
if err != nil {
log.Printf("jpeg: read: %v", err)
return
}
err = image.NewEncoder(w).Encode(img)
if err != nil {
log.Printf("jpeg: encode: %v", err)
return
}
}

View File

@@ -3,14 +3,13 @@ package handlers
import (
"fmt"
"github.com/gen2brain/cam2ip/image"
"github.com/gen2brain/cam2ip/reader"
"log"
"mime/multipart"
"net/http"
"net/textproto"
"time"
"github.com/gen2brain/cam2ip/image"
"github.com/gen2brain/cam2ip/reader"
)
// MJPEG handler.
@@ -28,22 +27,23 @@ func NewMJPEG(reader reader.ImageReader, delay int) *MJPEG {
func (m *MJPEG) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" && r.Method != "HEAD" {
http.Error(w, "405 Method Not Allowed", http.StatusMethodNotAllowed)
return
}
mimeWriter := multipart.NewWriter(w)
mimeWriter.SetBoundary("--boundary")
_ = mimeWriter.SetBoundary("--boundary")
w.Header().Add("Connection", "close")
w.Header().Add("Cache-Control", "no-store, no-cache")
w.Header().Add("Content-Type", fmt.Sprintf("multipart/x-mixed-replace;boundary=%s", mimeWriter.Boundary()))
cn := w.(http.CloseNotifier).CloseNotify()
done := r.Context().Done()
loop:
for {
select {
case <-cn:
case <-done:
break loop
default:
@@ -68,9 +68,11 @@ loop:
continue
}
time.Sleep(time.Duration(m.delay) * time.Millisecond)
if m.delay > 0 {
time.Sleep(time.Duration(m.delay) * time.Millisecond)
}
}
}
mimeWriter.Close()
_ = mimeWriter.Close()
}

View File

@@ -29,6 +29,7 @@ func (s *Socket) ServeHTTP(w http.ResponseWriter, r *http.Request) {
conn, err := websocket.Accept(w, r, nil)
if err != nil {
log.Printf("socket: accept: %v", err)
return
}
@@ -56,8 +57,10 @@ func (s *Socket) ServeHTTP(w http.ResponseWriter, r *http.Request) {
break
}
time.Sleep(time.Duration(s.delay) * time.Millisecond)
if s.delay > 0 {
time.Sleep(time.Duration(s.delay) * time.Millisecond)
}
}
conn.Close(websocket.StatusNormalClosure, "")
_ = conn.Close(websocket.StatusNormalClosure, "")
}