mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
API resiolution with error handling.
This commit is contained in:
parent
0c40eda36b
commit
d48b857dfe
@ -4,12 +4,9 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/go-chi/render"
|
"github.com/go-chi/render"
|
||||||
)
|
|
||||||
|
|
||||||
type ErrResponse struct {
|
"demodesk/neko/internal/api/utils"
|
||||||
Code int `json:"code"`
|
)
|
||||||
Message string `json:"message"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type ResolutionStruct struct {
|
type ResolutionStruct struct {
|
||||||
Width int `json:"width"`
|
Width int `json:"width"`
|
||||||
@ -17,15 +14,15 @@ type ResolutionStruct struct {
|
|||||||
Rate int `json:"rate"`
|
Rate int `json:"rate"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *ResolutionStruct) Bind(r *http.Request) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (h *RoomHandler) ResolutionGet(w http.ResponseWriter, r *http.Request) {
|
func (h *RoomHandler) ResolutionGet(w http.ResponseWriter, r *http.Request) {
|
||||||
size := h.remote.GetScreenSize()
|
size := h.remote.GetScreenSize()
|
||||||
|
|
||||||
if size == nil {
|
if size == nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
render.Render(w, r, utils.ErrMessage(500, "Not implmented."))
|
||||||
render.JSON(w, r, ErrResponse{
|
|
||||||
Code: -1,
|
|
||||||
Message: "Unable to get current screen resolution.",
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,25 +34,22 @@ func (h *RoomHandler) ResolutionGet(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *RoomHandler) ResolutionChange(w http.ResponseWriter, r *http.Request) {
|
func (h *RoomHandler) ResolutionChange(w http.ResponseWriter, r *http.Request) {
|
||||||
// data := &ResolutionStruct{}
|
data := &ResolutionStruct{}
|
||||||
// if err := render.Bind(r, data); err != nil {
|
if err := render.Bind(r, data); err != nil {
|
||||||
// render.JSON(w, r, ErrResponse{
|
render.Render(w, r, utils.ErrInvalidRequest(err))
|
||||||
// Code: -1,
|
return
|
||||||
// Message: "Invalid Request.",
|
}
|
||||||
// })
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
|
|
||||||
render.JSON(w, r, ErrResponse{
|
if err := h.remote.ChangeResolution(data.Width, data.Height, data.Rate); err != nil {
|
||||||
Code: -1,
|
render.Render(w, r, utils.ErrInvalidRequest(err))
|
||||||
Message: "Not implmented.",
|
return
|
||||||
})
|
}
|
||||||
|
|
||||||
|
// TODO: WebSocket notify.
|
||||||
|
|
||||||
|
render.JSON(w, r, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *RoomHandler) ResolutionList(w http.ResponseWriter, r *http.Request) {
|
func (h *RoomHandler) ResolutionList(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
render.Render(w, r, utils.ErrMessage(500, "Not implmented."))
|
||||||
render.JSON(w, r, ErrResponse{
|
|
||||||
Code: -1,
|
|
||||||
Message: "Not implmented.",
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
60
internal/api/utils/error.go
Normal file
60
internal/api/utils/error.go
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/go-chi/render"
|
||||||
|
)
|
||||||
|
|
||||||
|
//--
|
||||||
|
// Error response payloads & renderers
|
||||||
|
//--
|
||||||
|
|
||||||
|
// ErrResponse renderer type for handling all sorts of errors.
|
||||||
|
//
|
||||||
|
// In the best case scenario, the excellent github.com/pkg/errors package
|
||||||
|
// helps reveal information on the error, setting it on Err, and in the Render()
|
||||||
|
// method, using it to set the application-specific error code in AppCode.
|
||||||
|
type ErrResponse struct {
|
||||||
|
Err error `json:"-"` // low-level runtime error
|
||||||
|
HTTPStatusCode int `json:"-"` // http response status code
|
||||||
|
|
||||||
|
StatusText string `json:"status"` // user-level status message
|
||||||
|
AppCode int64 `json:"code,omitempty"` // application-specific error code
|
||||||
|
ErrorText string `json:"error,omitempty"` // application-level error message, for debugging
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *ErrResponse) Render(w http.ResponseWriter, r *http.Request) error {
|
||||||
|
render.Status(r, e.HTTPStatusCode)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ErrMessage(HTTPStatusCode int, StatusText string) render.Renderer {
|
||||||
|
return &ErrResponse{
|
||||||
|
HTTPStatusCode: HTTPStatusCode,
|
||||||
|
StatusText: StatusText,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ErrInvalidRequest(err error) render.Renderer {
|
||||||
|
return &ErrResponse{
|
||||||
|
Err: err,
|
||||||
|
HTTPStatusCode: 400,
|
||||||
|
StatusText: "Invalid request.",
|
||||||
|
ErrorText: err.Error(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ErrRender(err error) render.Renderer {
|
||||||
|
return &ErrResponse{
|
||||||
|
Err: err,
|
||||||
|
HTTPStatusCode: 422,
|
||||||
|
StatusText: "Error rendering response.",
|
||||||
|
ErrorText: err.Error(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var ErrNotFound = &ErrResponse{
|
||||||
|
HTTPStatusCode: 404,
|
||||||
|
StatusText: "Resource not found.",
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user