add error handler middleware

This commit is contained in:
Leandro Moreira
2024-01-29 16:31:34 -03:00
parent 5bfd75f3d8
commit 89b63f1809
4 changed files with 32 additions and 90 deletions

View File

@@ -1,12 +0,0 @@
package handlers
import "net/http"
func SetError(w http.ResponseWriter, err error) {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
func SetSuccessJson(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
}

View File

@@ -1,48 +0,0 @@
package handlers
import (
"encoding/json"
"log"
"net/http"
astisrt "github.com/asticode/go-astisrt/pkg"
"github.com/flavioribeiro/donut/internal/entities"
)
type MediaHandler struct{}
func NewMediaHandler() *MediaHandler {
return &MediaHandler{}
}
func (m *MediaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
SetError(w, entities.ErrHTTPGetOnly)
return
}
params := entities.RequestParams{}
if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
SetError(w, err)
return
}
log.Println("Connecting to SRT ", params)
_, err := astisrt.Dial(astisrt.DialOptions{
ConnectionOptions: []astisrt.ConnectionOption{
astisrt.WithLatency(300),
astisrt.WithStreamid(params.SRTStreamID),
},
// Callback when the connection is disconnected
OnDisconnect: func(c *astisrt.Connection, err error) { log.Fatal("Disconnected from SRT") },
Host: params.SRTHost,
Port: params.SRTPort,
})
if err != nil {
SetError(w, err)
return
}
log.Println("Connected to SRT")
}

View File

@@ -33,11 +33,10 @@ func NewSignalingHandler(
} }
} }
func (h *SignalingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *SignalingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) error {
if r.Method != http.MethodPost { if r.Method != http.MethodPost {
h.l.Sugar().Errorw("unexpected method") h.l.Sugar().Errorw("unexpected method")
SetError(w, entities.ErrHTTPPostOnly) return entities.ErrHTTPPostOnly
return
} }
params := entities.RequestParams{} params := entities.RequestParams{}
@@ -45,15 +44,13 @@ func (h *SignalingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.l.Sugar().Errorw("error while decoding request params json", h.l.Sugar().Errorw("error while decoding request params json",
"error", err, "error", err,
) )
SetError(w, err) return err
return
} }
if err := params.Valid(); err != nil { if err := params.Valid(); err != nil {
h.l.Sugar().Errorw("invalid params", h.l.Sugar().Errorw("invalid params",
"error", err, "error", err,
) )
SetError(w, err) return err
return
} }
peer, err := h.webRTCController.CreatePeerConnection() peer, err := h.webRTCController.CreatePeerConnection()
@@ -61,8 +58,7 @@ func (h *SignalingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.l.Sugar().Errorw("error while setting up web rtc connection", h.l.Sugar().Errorw("error while setting up web rtc connection",
"error", err, "error", err,
) )
SetError(w, err) return err
return
} }
// TODO: create tracks according with SRT available streams // TODO: create tracks according with SRT available streams
@@ -77,8 +73,7 @@ func (h *SignalingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.l.Sugar().Errorw("error while creating a web rtc track", h.l.Sugar().Errorw("error while creating a web rtc track",
"error", err, "error", err,
) )
SetError(w, err) return err
return
} }
metadataSender, err := h.webRTCController.CreateDataChannel(peer, entities.MetadataChannelID) metadataSender, err := h.webRTCController.CreateDataChannel(peer, entities.MetadataChannelID)
@@ -86,15 +81,14 @@ func (h *SignalingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.l.Sugar().Errorw("error while createing a web rtc data channel", h.l.Sugar().Errorw("error while createing a web rtc data channel",
"error", err, "error", err,
) )
SetError(w, err) return err
} }
if err = h.webRTCController.SetRemoteDescription(peer, params.Offer); err != nil { if err = h.webRTCController.SetRemoteDescription(peer, params.Offer); err != nil {
h.l.Sugar().Errorw("error while setting a remote web rtc description", h.l.Sugar().Errorw("error while setting a remote web rtc description",
"error", err, "error", err,
) )
SetError(w, err) return err
return
} }
localDescription, err := h.webRTCController.GatheringWebRTC(peer) localDescription, err := h.webRTCController.GatheringWebRTC(peer)
@@ -102,17 +96,17 @@ func (h *SignalingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.l.Sugar().Errorw("error while preparing a local web rtc description", h.l.Sugar().Errorw("error while preparing a local web rtc description",
"error", err, "error", err,
) )
SetError(w, err) return err
return
} }
localOfferDescription, err := json.Marshal(*localDescription) w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
err = json.NewEncoder(w).Encode(*localDescription)
if err != nil { if err != nil {
h.l.Sugar().Errorw("error while encoding a local web rtc description", h.l.Sugar().Errorw("error while encoding a local web rtc description",
"error", err, "error", err,
) )
SetError(w, err) return err
return
} }
srtConnection, err := h.srtController.Connect(&params) srtConnection, err := h.srtController.Connect(&params)
@@ -120,18 +114,10 @@ func (h *SignalingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.l.Sugar().Errorw("error while connecting to an srt server", h.l.Sugar().Errorw("error while connecting to an srt server",
"error", err, "error", err,
) )
SetError(w, err) return err
return
} }
go h.streamingController.Stream(srtConnection, videoTrack, metadataSender) go h.streamingController.Stream(srtConnection, videoTrack, metadataSender)
if _, err := w.Write(localOfferDescription); err != nil { return nil
h.l.Sugar().Errorw("error responding the local web rtc offer description",
"error", err,
)
SetError(w, err)
return
}
SetSuccessJson(w)
} }

View File

@@ -4,17 +4,23 @@ import (
"net/http" "net/http"
"github.com/flavioribeiro/donut/internal/web/handlers" "github.com/flavioribeiro/donut/internal/web/handlers"
"go.uber.org/zap"
) )
type ErrorHTTPHandler interface {
ServeHTTP(w http.ResponseWriter, r *http.Request) error
}
func NewServeMux( func NewServeMux(
index *handlers.IndexHandler, index *handlers.IndexHandler,
signaling *handlers.SignalingHandler, signaling *handlers.SignalingHandler,
l *zap.Logger,
) *http.ServeMux { ) *http.ServeMux {
mux := http.NewServeMux() mux := http.NewServeMux()
mux.Handle("/", index) mux.Handle("/", index)
mux.Handle("/doSignaling", setCors(signaling)) mux.Handle("/doSignaling", setCors(errorHandler(l, signaling)))
mux.Handle("/demo", http.FileServer(http.Dir("./demo"))) mux.Handle("/demo", http.FileServer(http.Dir("./demo")))
return mux return mux
@@ -32,3 +38,13 @@ func setCors(next http.Handler) http.Handler {
next.ServeHTTP(w, r) next.ServeHTTP(w, r)
}) })
} }
func errorHandler(l *zap.Logger, next ErrorHTTPHandler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := next.ServeHTTP(w, r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
}