mirror of
https://github.com/owncast/owncast.git
synced 2024-11-24 05:38:58 +03:00
Rework utils/restendpointhelper to use the new chi router functionality (#3750)
* Remove old implementation, add new function to work with the chi router * Use new URL Param function to get clientID instead * Remove usage of old restendpoint functions * Fix typo in url param name * Remove unused tests
This commit is contained in:
parent
a529502809
commit
414a8aeed8
4 changed files with 9 additions and 103 deletions
|
@ -261,7 +261,7 @@ func SendSystemMessage(integration user.ExternalAPIUser, w http.ResponseWriter,
|
||||||
// SendSystemMessageToConnectedClient will handle incoming requests to send a single message to a single connected client by ID.
|
// SendSystemMessageToConnectedClient will handle incoming requests to send a single message to a single connected client by ID.
|
||||||
func SendSystemMessageToConnectedClient(integration user.ExternalAPIUser, w http.ResponseWriter, r *http.Request) {
|
func SendSystemMessageToConnectedClient(integration user.ExternalAPIUser, w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
clientIDText, err := utils.ReadRestURLParameter(r, "clientId")
|
clientIDText, err := utils.GetURLParam(r, "clientId")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
controllers.BadRequestHandler(w, err)
|
controllers.BadRequestHandler(w, err)
|
||||||
return
|
return
|
||||||
|
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"github.com/owncast/owncast/controllers/admin"
|
"github.com/owncast/owncast/controllers/admin"
|
||||||
"github.com/owncast/owncast/core/user"
|
"github.com/owncast/owncast/core/user"
|
||||||
"github.com/owncast/owncast/router/middleware"
|
"github.com/owncast/owncast/router/middleware"
|
||||||
"github.com/owncast/owncast/utils"
|
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -20,8 +19,6 @@ func (*ServerInterfaceImpl) SendSystemMessageOptions(w http.ResponseWriter, r *h
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*ServerInterfaceImpl) SendSystemMessageToConnectedClient(w http.ResponseWriter, r *http.Request, clientId int) {
|
func (*ServerInterfaceImpl) SendSystemMessageToConnectedClient(w http.ResponseWriter, r *http.Request, clientId int) {
|
||||||
// doing this hack to make the new system work with the old system
|
|
||||||
r.Header[utils.RestURLPatternHeaderKey] = []string{`/api/integrations/chat/system/client/{clientId}`}
|
|
||||||
middleware.RequireExternalAPIAccessToken(user.ScopeCanSendSystemMessages, admin.SendSystemMessageToConnectedClient)(w, r)
|
middleware.RequireExternalAPIAccessToken(user.ScopeCanSendSystemMessages, admin.SendSystemMessageToConnectedClient)(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,68 +2,16 @@ package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
const RestURLPatternHeaderKey = "Owncast-Resturl-Pattern"
|
// GetURLParam retrieves the specified URL param from a given request.
|
||||||
|
func GetURLParam(r *http.Request, key string) (value string, err error) {
|
||||||
// takes the segment pattern of an Url string and returns the segment before the first dynamic REST parameter.
|
value = chi.URLParam(r, key)
|
||||||
func getPatternForRestEndpoint(pattern string) string {
|
if value == "" {
|
||||||
firstIndex := strings.Index(pattern, "/{")
|
err = errors.New("Request does not contain requested URL param")
|
||||||
if firstIndex == -1 {
|
|
||||||
return pattern
|
|
||||||
}
|
|
||||||
|
|
||||||
return strings.TrimRight(pattern[:firstIndex], "/") + "/"
|
|
||||||
}
|
|
||||||
|
|
||||||
func zip2D(iterable1 *[]string, iterable2 *[]string) map[string]string {
|
|
||||||
dict := make(map[string]string)
|
|
||||||
for index, key := range *iterable1 {
|
|
||||||
dict[key] = (*iterable2)[index]
|
|
||||||
}
|
|
||||||
return dict
|
|
||||||
}
|
|
||||||
|
|
||||||
func mapPatternWithRequestURL(pattern string, requestURL string) (map[string]string, error) {
|
|
||||||
patternSplit := strings.Split(pattern, "/")
|
|
||||||
requestURLSplit := strings.Split(requestURL, "/")
|
|
||||||
|
|
||||||
if len(patternSplit) == len(requestURLSplit) {
|
|
||||||
return zip2D(&patternSplit, &requestURLSplit), nil
|
|
||||||
}
|
|
||||||
return nil, errors.New("the length of pattern and request Url does not match")
|
|
||||||
}
|
|
||||||
|
|
||||||
func readParameter(pattern string, requestURL string, paramName string) (string, error) {
|
|
||||||
all, err := mapPatternWithRequestURL(pattern, requestURL)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
if value, exists := all[fmt.Sprintf("{%s}", paramName)]; exists {
|
|
||||||
return value, nil
|
|
||||||
}
|
|
||||||
return "", fmt.Errorf("parameter with name %s not found", paramName)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadRestURLParameter will return the parameter from the request of the requested name.
|
|
||||||
func ReadRestURLParameter(r *http.Request, parameterName string) (string, error) {
|
|
||||||
pattern, found := r.Header[RestURLPatternHeaderKey]
|
|
||||||
if !found {
|
|
||||||
return "", fmt.Errorf("this HandlerFunc is not marked as REST-Endpoint. Cannot read Parameter '%s' from Request", parameterName)
|
|
||||||
}
|
|
||||||
|
|
||||||
return readParameter(pattern[0], r.URL.Path, parameterName)
|
|
||||||
}
|
|
||||||
|
|
||||||
// RestEndpoint wraps a handler to use the rest endpoint helper.
|
|
||||||
func RestEndpoint(pattern string, handler http.HandlerFunc) (string, http.HandlerFunc) {
|
|
||||||
baseURL := getPatternForRestEndpoint(pattern)
|
|
||||||
return baseURL, func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
r.Header[RestURLPatternHeaderKey] = []string{pattern}
|
|
||||||
handler(w, r)
|
|
||||||
}
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,39 +0,0 @@
|
||||||
package utils
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestGetPatternForRestEndpoint(t *testing.T) {
|
|
||||||
expected := "/hello/"
|
|
||||||
endpoints := [...]string{"/hello/{param1}", "/hello/{param1}/{param2}", "/hello/{param1}/world/{param2}"}
|
|
||||||
for _, endpoint := range endpoints {
|
|
||||||
if ep := getPatternForRestEndpoint(endpoint); ep != expected {
|
|
||||||
t.Errorf("%s p does not match expected %s", ep, expected)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReadParameter(t *testing.T) {
|
|
||||||
expected := "world"
|
|
||||||
endpoints := [...]string{
|
|
||||||
"/hello/{p1}",
|
|
||||||
"/hello/cruel/{p1}",
|
|
||||||
"/hello/{p1}/my/friend",
|
|
||||||
"/hello/{p1}/{p2}/friend",
|
|
||||||
"/hello/{p2}/{p3}/{p1}",
|
|
||||||
"/{p1}/is/nice",
|
|
||||||
"/{p1}/{p1}/{p1}",
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, ep := range endpoints {
|
|
||||||
v, err := readParameter(ep, strings.Replace(ep, "{p1}", expected, -1), "p1")
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Unexpected error when reading parameter: %s", err.Error())
|
|
||||||
}
|
|
||||||
if v != expected {
|
|
||||||
t.Errorf("'%s' should have returned %s", ep, expected)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue