Skip to content

Commit

Permalink
Merge pull request #2 from miutaku/develop
Browse files Browse the repository at this point in the history
frontend以外を完成
  • Loading branch information
miutaku authored Jan 25, 2025
2 parents b16802a + 061a643 commit a3b90f1
Show file tree
Hide file tree
Showing 13 changed files with 725 additions and 17 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/manage-backend-docker-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
name: manage-twitcasting backend docker build
on:
push:
branches:
- develop
paths:
- 'manage-backend/**'

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: create meta
id: meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.actor }}/manage-backend-rec-twitcasting
tags: |
type=raw,value=latest
type=sha,prefix=,suffix=,format=short
- name: Build container image
uses: docker/build-push-action@v5
with:
file: ./manage-backend/Dockerfile
push: false
platforms: linux/amd64,linux/arm64
44 changes: 44 additions & 0 deletions .github/workflows/manage-backend-docker-push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
name: manage-twitcasting backend docker push
on:
push:
branches:
- main
paths:
- 'manage-backend/**'

jobs:
push:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Log in to ghcr.io
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: create image meta
id: meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.actor }}/manage-backend-rec-twitcasting
tags: |
type=raw,value=latest
type=sha,prefix=,suffix=,format=short
- name: push container image
uses: docker/build-push-action@v5
with:
file: ./manage-backend/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
platforms: linux/amd64,linux/arm64
cache-from: type=gha
cache-to: type=gha,mode=max
136 changes: 136 additions & 0 deletions api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# TwitCasting Recording API

This API provides a way to check if a TwitCasting user is live streaming and record their stream using `ffmpeg`. The stream is saved in a specified output directory.

## Table of Contents

- [Setup](#setup)
- [Environment Variables](#environment-variables)
- [Endpoints](#endpoints)
- [Recording Output](#recording-output)
- [How to Run](#how-to-run)
- [License](#license)

## Setup

1. Clone this repository and go to this directory.

2. Install dependencies:

Ensure `ffmpeg` is installed on your system. If not, install it:

```bash
# For macOS
brew install ffmpeg
# For Debian based
sudo apt update
sudo apt install ffmpeg
# For RHEL based
sudo dnf install ffmpeg
```

3. Set up environment variables in `.env` or export them:

```bash
export TWITCASTING_CLIENT_ID=<your_client_id>
export TWITCASTING_CLIENT_SECRET=<your_client_secret>
export DB_HOST=localhost
export DB_PORT=5432
export DB_USER=user
export DB_PASSWORD=password
export DB_NAME=dbname
export DB_TABLE_NAME=tablename
export OUTPUT_DIR=./recorded
export LOG_LEVEL=debug
```

4. Run the server:

```bash
go run main.go
```

## Environment Variables

| Variable | Description | Default Value |
|-------------------------|-----------------------------------------------------------------------------|---------------------|
| `TWITCASTING_CLIENT_ID`| Your TwitCasting Client ID. | None (required) |
| `TWITCASTING_CLIENT_SECRET`| Your TwitCasting Client Secret. | None (required) |
| `DB_HOST` | Database host. | `localhost` |
| `DB_PORT` | Database port. | `5432` |
| `DB_USER` | Database user. | `user` |
| `DB_PASSWORD` | Database password. | `password` |
| `DB_NAME` | Database name. | `dbname` |
| `DB_TABLE_NAME` | Database table name. | `tablename` |
| `OUTPUT_DIR` | Directory to save recorded videos. | `./recorded` |
| `LOG_LEVEL` | Set to `debug` to see detailed logs. | None |

## Endpoints

### `GET /check-live`

Checks if a TwitCasting user is live streaming and records their stream if live.

#### Request

- **URL Query Parameters**:
- `username` (string): The TwitCasting username to check and record.

#### Example

```bash
curl "http://localhost:8080/check-live?username=<twitcasting_username>"
```

#### Responses

- **200 OK**:
- User is not live streaming:
```
User is not live streaming.
```
- Recording finished:
```
Recording finished. Saved as: <output_file_path>
```
- **400 Bad Request**:
- Missing `username` parameter:
```
username parameter is required
```
- **500 Internal Server Error**:
- Errors while checking stream or updating recording state:
```
Failed to get current live information: <error_details>
```
## Recording Output
Recorded streams are saved in the directory specified by the `OUTPUT_DIR` environment variable. The directory structure is as follows:
```
recorded/
└── <username>/
└── <YYYY-MM-DD>/
└── <HH-MM>_<title>.mp4
```
- `<username>`: The TwitCasting username.
- `<YYYY-MM-DD>`: The date of the recording.
- `<HH-MM>`: The time of the recording.
- `<title>`: The sanitized title of the live stream.
## How to Run
1. Ensure all environment variables are correctly set.
2. Start the server:
```bash
go run main.go
```

3. Access the endpoint to check and record a live stream:

```bash
curl "http://localhost:8080/check-live?username=<twitcasting_username>"
```
10 changes: 10 additions & 0 deletions batch/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# TwitCasting Recording Application

This application is a job that periodically executes an API based on the userdata information in the DB for the TwitCasting recording application.

This application was created because Docker Compose does not provide a feature equivalent to cronjob.

# Flow

1. Retrieve the TwitCasting usernames from the DB where `recording_state` is `false`.
2. Request api-rec-twitcasting to start recording the broadcasts of the retrieved users.
3 changes: 2 additions & 1 deletion batch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ func task() {
fmt.Fprintf(os.Stderr, "Unable to scan row: %v\n", err)
return
}
resp, err := http.Get(fmt.Sprintf("http://api-rec-twitcasting:8080/check-live?username=%s", name))
apiURL := os.Getenv("API_REC_TWITCASTING_URL")
resp, err := http.Get(fmt.Sprintf("%s/check-live?username=%s", apiURL, name))
if err != nil {
fmt.Fprintf(os.Stderr, "HTTP request failed: %v\n", err)
return
Expand Down
38 changes: 26 additions & 12 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,21 @@ services:
#build:
# context: .
# dockerfile: batch/Dockerfile
depends_on:
- postgres-rec-twitcasting
restart: unless-stopped
container_name: batch-rec-twitcasting
environment:
- FETCH_INTERVAL_SEC=1
- API_REC_TWITCASTING_URL=http://api-rec-twitcasting:8080
- DB_HOST=postgres-rec-twitcasting
- DB_PORT=5432
- DB_USER=rec-twitcasting-user
- DB_PASSWORD=rec-twitcasting-pass
- DB_NAME=rec_twitcasting
- DB_TABLE_NAME=speakers
#- LOG_LEVEL=debug

# db
postgres-rec-twitcasting:
image: postgres:17.2
Expand All @@ -53,19 +57,29 @@ services:
- ./postgres/init.sql:/docker-entrypoint-initdb.d/init.sql
- ./postgres/data:/var/lib/postgresql/data

## manage
# manage
# manage-frontend-rec-twitcasting:
# image: ghcr.io/miutaku/manage-frontend-rec-twitcasting:latest
# container_name: frontend-rec-twitcasting
# #image: ghcr.io/miutaku/manage-frontend-rec-twitcasting:latest
# build:
# context: .
# dockerfile: manage-frontend/Dockerfile
# container_name: manage-frontend-rec-twitcasting
# restart: unless-stopped
# ports:
# - 8888:8888
#
# manage-backend-rec-twitcasting:
# image: ghcr.io/miutaku/manage-backend-rec-twitcasting:latest
# container_name: backend-rec-twitcasting
# restart: unless-stopped
# environment:
# - TZ=Asia/Tokyo
# #- DATABASE_URL=YOUR_DATABASE_URL
#

manage-backend-rec-twitcasting:
image: ghcr.io/miutaku/manage-backend-rec-twitcasting:latest
#build:
# context: .
# dockerfile: manage-backend/Dockerfile
container_name: manage-backend-rec-twitcasting
restart: unless-stopped
environment:
- TZ=Asia/Tokyo
- DB_HOST=postgres-rec-twitcasting
- DB_PORT=5432
- DB_USER=rec-twitcasting-user
- DB_PASSWORD=rec-twitcasting-pass
- DB_NAME=rec_twitcasting
- DB_TABLE_NAME=speakers
13 changes: 13 additions & 0 deletions manage-backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Build stage
FROM golang:1.23 AS builder
ENV CGO_ENABLED=0
WORKDIR /app
COPY manage-backend .
RUN go build -o manage-backend-rec-twitcasting ./main.go

# Runtime stage
FROM alpine:latest
WORKDIR /root/
COPY --from=builder /app/manage-backend-rec-twitcasting .
RUN apk add --no-cache libc6-compat
CMD ["/root/manage-backend-rec-twitcasting"]
Loading

0 comments on commit a3b90f1

Please sign in to comment.