Skip to content

Commit 143f0f1

Browse files
committed
Adding a find endpoint route
1 parent 55e9971 commit 143f0f1

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed

controllers/v1/stream/stream.go

+32
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,38 @@ func (r *Repos) ListStreams(c echo.Context) error {
229229
return c.JSON(http.StatusOK, utils.NonNil(endpoints))
230230
}
231231

232+
// FindStream handles finding a stream
233+
// @Summary Finds stream
234+
// @Description finds existing stream
235+
// @ID find-stream
236+
// @Tags stream-endpoints
237+
// @Accept json
238+
// @Param endpoint body stream.FindEndpoint true "Find Endpoint object"
239+
// @Success 200 body stream.FindEndpoint
240+
// @Router /v1/internal/streams/find [get]
241+
func (r *Repos) FindStream(c echo.Context) error {
242+
var findEndpoint stream.FindEndpoint
243+
244+
err := c.Bind(&findEndpoint)
245+
if err != nil {
246+
err = fmt.Errorf("failed to bind to request json: %w", err)
247+
return echo.NewHTTPError(http.StatusBadRequest, err)
248+
}
249+
250+
if findEndpoint.EndpointID == 0 && (len(findEndpoint.Application) == 0 || len(findEndpoint.Name) == 0) {
251+
err = fmt.Errorf("failed to bind to request json: missing application, name or endpoint id")
252+
return echo.NewHTTPError(http.StatusBadRequest, err)
253+
}
254+
255+
_, err = r.stream.FindEndpoint(c.Request().Context(), &findEndpoint)
256+
if err != nil {
257+
err = fmt.Errorf("failed to find endpoint: %w", err)
258+
return echo.NewHTTPError(http.StatusInternalServerError, err)
259+
}
260+
261+
return c.JSON(http.StatusOK, utils.NonNil(findEndpoint))
262+
}
263+
232264
// NewStream handles a creating a stream endpoint
233265
//
234266
// @Summary NewStream stream endpoint

router.go

+1
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ func (r *Router) loadRoutes() {
305305
streamsAuthed := internal.Group("/streams", r.access.ManageStreamAuthMiddleware)
306306
{
307307
streamsAuthed.GET("", r.stream.ListStreams)
308+
streamsAuthed.GET("/find", r.stream.FindStream)
308309
streamsAuthed.POST("", r.stream.NewStream)
309310
streamAuthed := streamsAuthed.Group("/:endpointid")
310311
{

services/stream/stream.go

+44
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type (
1515
// Repo represents all stream endpoint interactions
1616
Repo interface {
1717
ListEndpoints(ctx context.Context) ([]EndpointDB, error)
18+
FindEndpoint(ctx context.Context, findEndpoint FindEndpoint) (EndpointDB, error)
1819
GetEndpointByID(ctx context.Context, endpointID int) (EndpointDB, error)
1920
GetEndpointByApplicationNamePwd(ctx context.Context, application, name, pwd string) (EndpointDB, error)
2021

@@ -63,6 +64,18 @@ type (
6364
AutoRemove bool `json:"autoRemove,omitempty"`
6465
}
6566

67+
// FindEndpoint used to find an endpoint
68+
FindEndpoint struct {
69+
// EndpointID is the unique database id of the stream
70+
EndpointID int `json:"endpointId,omitempty"`
71+
// Application defines which RTMP application this is valid for
72+
Application string `json:"application,omitempty"`
73+
// Name is the unique name given in an application
74+
Name string `json:"name,omitempty"`
75+
// Pwd defines an extra layer of security for authentication
76+
Pwd string `json:"pwd,omitempty"`
77+
}
78+
6679
// NewEditEndpoint encapsulates the creation of a stream endpoint
6780
NewEditEndpoint struct {
6881
// Application defines which RTMP application this is valid for
@@ -114,6 +127,37 @@ func (s *Store) ListEndpoints(ctx context.Context) ([]EndpointDB, error) {
114127
return e, nil
115128
}
116129

130+
func (s *Store) FindEndpoint(ctx context.Context, findEndpoint *FindEndpoint) (EndpointDB, error) {
131+
var e EndpointDB
132+
133+
builder := utils.PSQL().Select("*").
134+
From("web_api.stream_endpoints").
135+
Where(sq.Or{
136+
sq.Eq{"endpoint_id": findEndpoint.EndpointID},
137+
sq.And{
138+
sq.Eq{"application": findEndpoint.Application},
139+
sq.Eq{"name": findEndpoint.Name},
140+
sq.Eq{"pwd": findEndpoint.Pwd},
141+
},
142+
})
143+
144+
sql, args, err := builder.ToSql()
145+
if err != nil {
146+
panic(fmt.Errorf("failed to build sql for FindEndpoint: %w", err))
147+
}
148+
149+
err = s.db.GetContext(ctx, &e, sql, args...)
150+
if err != nil {
151+
return EndpointDB{}, fmt.Errorf("failed to find endpoint: %w", err)
152+
}
153+
154+
findEndpoint.EndpointID = e.EndpointID
155+
findEndpoint.Application = e.Application
156+
findEndpoint.Name = e.Name
157+
158+
return e, nil
159+
}
160+
117161
func (s *Store) GetEndpointByID(ctx context.Context, endpointID int) (EndpointDB, error) {
118162
var e EndpointDB
119163

swagger/docs.go

+53
Original file line numberDiff line numberDiff line change
@@ -2187,6 +2187,38 @@ const docTemplate = `{
21872187
}
21882188
}
21892189
},
2190+
"/v1/internal/streams/find": {
2191+
"get": {
2192+
"description": "finds existing stream",
2193+
"consumes": [
2194+
"application/json"
2195+
],
2196+
"tags": [
2197+
"stream-endpoints"
2198+
],
2199+
"summary": "Finds stream",
2200+
"operationId": "find-stream",
2201+
"parameters": [
2202+
{
2203+
"description": "Find Endpoint object",
2204+
"name": "endpoint",
2205+
"in": "body",
2206+
"required": true,
2207+
"schema": {
2208+
"$ref": "#/definitions/stream.FindEndpoint"
2209+
}
2210+
}
2211+
],
2212+
"responses": {
2213+
"200": {
2214+
"description": "OK",
2215+
"schema": {
2216+
"type": "body"
2217+
}
2218+
}
2219+
}
2220+
}
2221+
},
21902222
"/v1/internal/streams/{endpointid}": {
21912223
"put": {
21922224
"consumes": [
@@ -4027,6 +4059,27 @@ const docTemplate = `{
40274059
}
40284060
}
40294061
},
4062+
"stream.FindEndpoint": {
4063+
"type": "object",
4064+
"properties": {
4065+
"application": {
4066+
"description": "Application defines which RTMP application this is valid for",
4067+
"type": "string"
4068+
},
4069+
"endpointId": {
4070+
"description": "EndpointID is the unique database id of the stream",
4071+
"type": "integer"
4072+
},
4073+
"name": {
4074+
"description": "Name is the unique name given in an application",
4075+
"type": "string"
4076+
},
4077+
"pwd": {
4078+
"description": "Pwd defines an extra layer of security for authentication",
4079+
"type": "string"
4080+
}
4081+
}
4082+
},
40304083
"stream.NewEditEndpoint": {
40314084
"type": "object",
40324085
"properties": {

0 commit comments

Comments
 (0)