mirror of
https://github.com/gen2brain/cam2ip.git
synced 2025-12-16 04:18:39 +00:00
Update handlers
This commit is contained in:
@@ -32,12 +32,13 @@ func (h *HTML) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
if r.Method != "GET" && r.Method != "HEAD" {
|
if r.Method != "GET" && r.Method != "HEAD" {
|
||||||
msg := fmt.Sprintf("405 Method Not Allowed (%s)", r.Method)
|
msg := fmt.Sprintf("405 Method Not Allowed (%s)", r.Method)
|
||||||
http.Error(w, msg, http.StatusMethodNotAllowed)
|
http.Error(w, msg, http.StatusMethodNotAllowed)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
w.Write(h.Template)
|
_, _ = w.Write(h.Template)
|
||||||
}
|
}
|
||||||
|
|
||||||
var html = `<html>
|
var html = `<html>
|
||||||
|
|||||||
@@ -17,10 +17,11 @@ func NewIndex() *Index {
|
|||||||
func (i *Index) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (i *Index) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != "GET" && r.Method != "HEAD" {
|
if r.Method != "GET" && r.Method != "HEAD" {
|
||||||
http.Error(w, "405 Method Not Allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "405 Method Not Allowed", http.StatusMethodNotAllowed)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Write([]byte(`<html>
|
_, _ = w.Write([]byte(`<html>
|
||||||
<head><title>cam2ip</title></head>
|
<head><title>cam2ip</title></head>
|
||||||
<body>
|
<body>
|
||||||
<h1>cam2ip</h1>
|
<h1>cam2ip</h1>
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ func NewJPEG(reader reader.ImageReader) *JPEG {
|
|||||||
func (j *JPEG) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (j *JPEG) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != "GET" && r.Method != "HEAD" {
|
if r.Method != "GET" && r.Method != "HEAD" {
|
||||||
http.Error(w, "405 Method Not Allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "405 Method Not Allowed", http.StatusMethodNotAllowed)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,12 +33,14 @@ func (j *JPEG) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
img, err := j.reader.Read()
|
img, err := j.reader.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("jpeg: read: %v", err)
|
log.Printf("jpeg: read: %v", err)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = image.NewEncoder(w).Encode(img)
|
err = image.NewEncoder(w).Encode(img)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("jpeg: encode: %v", err)
|
log.Printf("jpeg: encode: %v", err)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,14 +3,13 @@ package handlers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/gen2brain/cam2ip/image"
|
||||||
|
"github.com/gen2brain/cam2ip/reader"
|
||||||
"log"
|
"log"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/textproto"
|
"net/textproto"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gen2brain/cam2ip/image"
|
|
||||||
"github.com/gen2brain/cam2ip/reader"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// MJPEG handler.
|
// MJPEG handler.
|
||||||
@@ -28,22 +27,23 @@ func NewMJPEG(reader reader.ImageReader, delay int) *MJPEG {
|
|||||||
func (m *MJPEG) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (m *MJPEG) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != "GET" && r.Method != "HEAD" {
|
if r.Method != "GET" && r.Method != "HEAD" {
|
||||||
http.Error(w, "405 Method Not Allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "405 Method Not Allowed", http.StatusMethodNotAllowed)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
mimeWriter := multipart.NewWriter(w)
|
mimeWriter := multipart.NewWriter(w)
|
||||||
mimeWriter.SetBoundary("--boundary")
|
_ = mimeWriter.SetBoundary("--boundary")
|
||||||
|
|
||||||
w.Header().Add("Connection", "close")
|
w.Header().Add("Connection", "close")
|
||||||
w.Header().Add("Cache-Control", "no-store, no-cache")
|
w.Header().Add("Cache-Control", "no-store, no-cache")
|
||||||
w.Header().Add("Content-Type", fmt.Sprintf("multipart/x-mixed-replace;boundary=%s", mimeWriter.Boundary()))
|
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:
|
loop:
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-cn:
|
case <-done:
|
||||||
break loop
|
break loop
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@@ -68,9 +68,11 @@ loop:
|
|||||||
continue
|
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()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ func (s *Socket) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
conn, err := websocket.Accept(w, r, nil)
|
conn, err := websocket.Accept(w, r, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("socket: accept: %v", err)
|
log.Printf("socket: accept: %v", err)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,8 +57,10 @@ func (s *Socket) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
break
|
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, "")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user