Skip to content

Commit

Permalink
Stream routes can now return viewers via redis
Browse files Browse the repository at this point in the history
  • Loading branch information
tomouchuu committed Oct 8, 2023
1 parent 75f9007 commit 880c6de
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-files-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@haishin/backend': patch
---

Stream routes can now return viewers
15 changes: 11 additions & 4 deletions apps/backend/src/routes/streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { staticPlugin } from '@elysiajs/static'
import { getDuration, getPathsByUrl, urlUtils } from '@haishin/utils'
import { getStreamInfo } from '../stream/get-info'

import { setup } from '../app'

export const streamsFolder = path.join(
process.env.RAILWAY_VOLUME_MOUNT_PATH as string,
'streams'
Expand All @@ -23,7 +25,8 @@ if (!fs.existsSync(streamsFolder)) {
}

const streams = new Elysia()
.get('/streams', async () => {
.use(setup)
.get('/streams', async ({ store: { redis } }) => {
const streams = fs
.readdirSync(streamsFolder)
.filter((file) => !file.includes('.'))
Expand All @@ -40,21 +43,23 @@ const streams = new Elysia()
if (fs.existsSync(streamInfo.file))
started = fs.statSync(streamInfo.file).birthtime

const viewers = await redis.sCard(`users:${streamUrl}`)

return {
id: stream,
started,
duration: getDuration(streamInfo.file),
title,
thumbnail: `${apiStreamsUrl}/${stream}/stream.jpg`,
url: streamUrl,
viewers: 0
viewers
}
})
)

return enhancedStreamData
})
.get('/streams/:id', async ({ params: { id } }) => {
.get('/streams/:id', async ({ params: { id }, store: { redis } }) => {
const streamUrl = urlUtils.decodeUrl(id)

const streamInfo = await getStreamInfo(streamUrl)
Expand All @@ -65,14 +70,16 @@ const streams = new Elysia()
if (fs.existsSync(streamInfo.file))
started = fs.statSync(streamInfo.file).birthtime

const viewers = await redis.sCard(`users:${streamUrl}`)

const stream = {
id,
started,
duration: getDuration(streamInfo.file),
title,
thumbnail: `${apiStreamsUrl}/${id}/stream.jpg`,
url: streamUrl,
viewers: 0
viewers
}

return stream
Expand Down

0 comments on commit 880c6de

Please sign in to comment.