Skip to content
This repository was archived by the owner on Oct 5, 2023. It is now read-only.

Commit d05bc63

Browse files
authored
Mirakurun やアカウントの資格情報を未設定でも使用できるように改善 (#16)
* Update README.md * Mirakurun の導入しなくても動作するように設計変更 * 資格情報などが設定されず連携機能を提供できない場合にログを出力 * URL を Pages に変更 * Update README.md * PR 時に走る CI を修正 * ci7lus/elaina:latest に関する記述を追加 * ログレベルを変更可能にした * バッジの CI 名を修正
1 parent 58880d8 commit d05bc63

File tree

14 files changed

+173
-55
lines changed

14 files changed

+173
-55
lines changed

.github/workflows/check.yml

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Check PR
2+
3+
on:
4+
pull_request:
5+
types:
6+
- opened
7+
- synchronize
8+
paths-ignore:
9+
- '*.md'
10+
11+
workflow_dispatch:
12+
13+
jobs:
14+
check:
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- name: Checkout Repository
19+
uses: actions/checkout@v2
20+
21+
- name: Using Caches
22+
uses: actions/cache@v2.1.3
23+
with:
24+
path: ~/.gradle/caches
25+
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*') }}
26+
restore-keys: |
27+
${{ runner.os }}-gradle-
28+
- name: Setup JDK
29+
uses: actions/setup-java@v1.4.3
30+
with:
31+
java-version: 1.8
32+
33+
- name: Grant Execute Permission to gradlew
34+
run: chmod +x gradlew
35+
36+
- name: Build with Gradle
37+
run: ./gradlew build

.github/workflows/main.yml .github/workflows/docker.yml

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: CI
1+
name: Docker
22

33
on:
44
push:
@@ -9,13 +9,6 @@ on:
99
branches-ignore:
1010
- 'releases/**'
1111

12-
pull_request:
13-
types:
14-
- opened
15-
- synchronize
16-
paths-ignore:
17-
- '*.md'
18-
1912
release:
2013
types:
2114
- published
@@ -43,14 +36,14 @@ jobs:
4336
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
4437
uses: docker/build-push-action@v2
4538
with:
46-
push: ${{ github.event_name != 'pull_request' }}
39+
push: true
4740
tags: ${{ env.DOCKER_BASE_NAME }}:latest
4841

4942
- name: Build & Push (dev)
5043
if: github.event_name == 'push' && github.ref == 'refs/heads/dev'
5144
uses: docker/build-push-action@v2
5245
with:
53-
push: ${{ github.event_name != 'pull_request' }}
46+
push: true
5447
tags: ${{ env.DOCKER_BASE_NAME }}:dev
5548

5649
- name: Build & Push (Release)

README.md

+47-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
# saya: Japanese DTV backend service with powerful features
22

33
[![Kotlin](https://img.shields.io/badge/Kotlin-1.4.21-blue)](https://kotlinlang.org)
4-
[![Docker Build Status](https://img.shields.io/docker/build/slashnephy/saya)](https://hub.docker.com/r/slashnephy/saya)
4+
[![GitHub release (latest by date)](https://img.shields.io/github/v/release/SlashNephy/saya)](https://github.com/SlashNephy/saya/releases)
5+
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/SlashNephy/saya/Docker)](https://hub.docker.com/r/slashnephy/saya)
56
[![Docker Image Size (latest by date)](https://img.shields.io/docker/image-size/slashnephy/saya)](https://hub.docker.com/r/slashnephy/saya)
7+
[![Docker Pulls](https://img.shields.io/docker/pulls/slashnephy/saya)](https://hub.docker.com/r/slashnephy/saya)
68
[![license](https://img.shields.io/github/license/SlashNephy/saya)](https://github.com/SlashNephy/saya/blob/master/LICENSE)
79
[![issues](https://img.shields.io/github/issues/SlashNephy/saya)](https://github.com/SlashNephy/saya/issues)
810
[![pull requests](https://img.shields.io/github/issues-pr/SlashNephy/saya)](https://github.com/SlashNephy/saya/pulls)
911

1012
saya is still in heavy development.
1113

12-
- [REST API docs](https://atmos.starry.blue/saya)
14+
- [REST API docs](https://slashnephy.github.io/saya)
1315
- [Roadmap](https://github.com/SlashNephy/saya/projects/1)
1416

1517
---
@@ -34,14 +36,25 @@ saya is still in heavy development.
3436
次のプロジェクトとの併用を想定しています。
3537

3638
- [Chinachu/Mirakurun](https://github.com/Chinachu/Mirakurun) or [mirakc/mirakc](https://github.com/mirakc/mirakc)
37-
- Mirakurun と mirakc のどちらでも動作します。
39+
- Mirakurun と mirakc のどちらでも動作します。なくても動作しますが一部制約が生じます。
3840
- [l3tnun/EPGStation](https://github.com/l3tnun/EPGStation)
41+
- saya を動作させる上では不要です。elaina 上で番組を再生する場合に必要です。
3942
- [ci7lus/elaina](https://github.com/ci7lus/elaina)
43+
- EPGStation を介した番組プレイヤーです。saya の API をフロントエンドで利用しています。
4044

41-
# Setup
45+
# Docker
4246

4347
環境構築が容易なので Docker で導入することをおすすめします。
4448

49+
現在のベースイメージは `alpine` です。いくつかタグを用意しています。
50+
51+
- `:latest`
52+
master ブランチへのプッシュの際にビルドされます。安定しています。
53+
- `:dev`
54+
dev ブランチへのプッシュの際にビルドされます。開発版のため, 不安定である可能性があります。
55+
- `:v<tag>`
56+
GitHub 上のリリースに対応します。
57+
4558
`docker-compose.yml`
4659

4760
```yaml
@@ -53,7 +66,7 @@ services:
5366
image: slashnephy/saya:latest
5467
restart: always
5568
ports:
56-
- 1017:1017/tcp
69+
- 1017:1017/tcp # いれいな
5770
# 環境変数で各種設定を行います
5871
# () 内の値はデフォルト値を示します
5972
environment:
@@ -64,6 +77,9 @@ services:
6477
# HTTP サーバのベース URI ("/")
6578
# リバースプロキシ時に直下以外に置きたい場合に変更します
6679
SAYA_BASE_URI: /
80+
# ログレベル ("INFO")
81+
# 利用可能な値: ALL, TRACE, DEBUG, INFO, WARN, ERROR, OFF
82+
SAYA_LOG: DEBUG
6783
# Mirakurun のホスト, ポート番号 ("mirakurun", 40772)
6884
MIRAKURUN_HOST: mirakurun
6985
MIRAKURUN_PORT: 40772
@@ -76,13 +92,38 @@ services:
7692
TWITTER_AT: xxx
7793
TWITTER_ATS: xxx
7894

95+
elaina:
96+
container_name: elaina
97+
image: ci7lus/elaina:latest
98+
restart: always
99+
ports:
100+
- 1234:1234/tcp
101+
79102
mirakurun:
80103
epgstation:
81104
# https://github.com/l3tnun/docker-mirakurun-epgstation 等を参考にしてください。
82105
# サービス名, ポート番号等の変更がある場合には `MIRAKURUN_HOST`, `MIRAKURUN_PORT` の修正が必要になります。
83106
```
84107

85-
`http://localhost:1017/` にサーバが起動しているはずです。
108+
```console
109+
# イメージ更新
110+
docker pull slashnephy/saya:latest
111+
112+
# 起動
113+
docker-compose up -d
114+
115+
# ログ表示
116+
docker-compose logs -f
117+
118+
# 停止
119+
docker-compose down
120+
```
121+
122+
up すると `http://localhost:1017/` に saya が, `http://localhost:1234/` に elaina が起動しているはずです。
123+
124+
# Endpoints
125+
126+
TODO...
86127

87128
# Acknowledgments
88129

src/main/kotlin/blue/starry/saya/Module.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package blue.starry.saya
22

33
import blue.starry.saya.common.Env
4+
import blue.starry.saya.common.createSayaLogger
45
import blue.starry.saya.endpoints.getIndex
56
import blue.starry.saya.endpoints.wsLiveComments
67
import blue.starry.saya.endpoints.wsTimeshiftComments
@@ -36,7 +37,7 @@ fun Application.module() {
3637
allowCredentials = true
3738
}
3839
install(CallLogging) {
39-
logger = KotlinLogging.logger("saya.server")
40+
logger = KotlinLogging.createSayaLogger("saya.server")
4041
format { call ->
4142
when (val status = call.response.status()) {
4243
HttpStatusCode.Found -> "$status: ${call.request.toLogString()} -> ${call.response.headers[HttpHeaders.Location]}"

src/main/kotlin/blue/starry/saya/common/Env.kt

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ object Env {
66
val SAYA_HOST by string { "0.0.0.0" }
77
val SAYA_PORT by int { 1017 }
88
val SAYA_BASE_URI by string { "/" }
9+
val SAYA_LOG by string { "INFO" }
910

1011
val MIRAKURUN_HOST by string { "mirakurun" }
1112
val MIRAKURUN_PORT by int { 40772 }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package blue.starry.saya.common
2+
3+
import ch.qos.logback.classic.Level
4+
import mu.KLogger
5+
import mu.KotlinLogging
6+
7+
internal fun KotlinLogging.createSayaLogger(name: String): KLogger {
8+
val logger = logger(name)
9+
val underlying = logger.underlyingLogger
10+
if (underlying is ch.qos.logback.classic.Logger) {
11+
underlying.level = Level.toLevel(Env.SAYA_LOG, Level.INFO)
12+
}
13+
14+
return logger
15+
}

src/main/kotlin/blue/starry/saya/common/ReadOnlyContainer.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import kotlinx.coroutines.sync.Mutex
88
import kotlinx.coroutines.sync.withLock
99
import kotlin.time.minutes
1010

11-
class ReadOnlyContainer<T: Any>(private val block: suspend () -> List<T>) {
11+
class ReadOnlyContainer<T: Any>(private val block: suspend () -> List<T>?) {
1212
private val mutex = Mutex()
1313
private val collection = mutableListOf<T>()
1414

1515
private val initialJob = GlobalScope.launch {
16-
collection.addAll(block())
16+
collection.addAll(block() ?: return@launch)
1717
}
1818

1919
init {
@@ -29,7 +29,7 @@ class ReadOnlyContainer<T: Any>(private val block: suspend () -> List<T>) {
2929
initialJob.join()
3030

3131
mutex.withLock {
32-
val new = block()
32+
val new = block() ?: return
3333

3434
collection.clear()
3535
collection.addAll(new)

src/main/kotlin/blue/starry/saya/endpoints/Comments.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package blue.starry.saya.endpoints
22

3+
import blue.starry.saya.common.createSayaLogger
34
import blue.starry.saya.models.Comment
45
import blue.starry.saya.models.JikkyoChannel
56
import blue.starry.saya.models.TimeshiftCommentControl
@@ -32,7 +33,7 @@ import mu.KotlinLogging
3233
import java.util.concurrent.atomic.AtomicLong
3334
import kotlin.math.roundToLong
3435

35-
private val logger = KotlinLogging.logger("saya.endpoints")
36+
private val logger = KotlinLogging.createSayaLogger("saya.endpoints")
3637

3738
private suspend fun DefaultWebSocketSession.rejectWs(message: () -> String) {
3839
close(CloseReason(CloseReason.Codes.CANNOT_ACCEPT, message()))

src/main/kotlin/blue/starry/saya/services/Client.kt

+27-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import blue.starry.penicillin.core.session.config.application
66
import blue.starry.penicillin.core.session.config.httpClient
77
import blue.starry.penicillin.core.session.config.token
88
import blue.starry.saya.common.Env
9+
import blue.starry.saya.services.mirakurun.MirakurunApi
910
import blue.starry.saya.services.miyoutv.MiyouTVApi
1011
import io.ktor.client.*
1112
import io.ktor.client.engine.cio.*
@@ -22,6 +23,7 @@ import mu.KotlinLogging
2223
import kotlin.time.minutes
2324

2425
const val SayaUserAgent = "saya/2.0 (+https://github.com/SlashNephy/saya)"
26+
private val logger = KotlinLogging.logger("saya.client")
2527

2628
val SayaHttpClient = run {
2729
HttpClient(CIO) {
@@ -57,6 +59,7 @@ val SayaTwitterClient = run {
5759
val (ck, cs) = Env.TWITTER_CK to Env.TWITTER_CS
5860
val (at, ats) = Env.TWITTER_AT to Env.TWITTER_ATS
5961
if (ck == null || cs == null || at == null || ats == null) {
62+
logger.info { "Twitter の資格情報が設定されていません。Twitter 連携機能は提供しません。" }
6063
return@run null
6164
}
6265

@@ -70,12 +73,19 @@ val SayaTwitterClient = run {
7073
}
7174

7275
val SayaAnnictClient = run {
73-
AnnictClient(Env.ANNICT_TOKEN ?: return@run null)
76+
val token = Env.ANNICT_TOKEN
77+
if (token == null) {
78+
logger.info { "Annict の資格情報が設定されていません。Annict 連携機能は提供しません。" }
79+
return@run null
80+
}
81+
82+
AnnictClient(token)
7483
}
7584

7685
val SayaMiyouTVApi = run {
7786
val (email, pass) = Env.MORITAPO_EMAIL to Env.MORITAPO_PASSWORD
7887
if (email == null || pass == null) {
88+
logger.info { "MiyouTV の資格情報が設定されていません。MiyouTV 連携機能は提供しません。" }
7989
return@run null
8090
}
8191

@@ -85,3 +95,19 @@ val SayaMiyouTVApi = run {
8595

8696
MiyouTVApi(login.token)
8797
}
98+
99+
val SayaMirakurunApi = run {
100+
try {
101+
val api = MirakurunApi(Env.MIRAKURUN_HOST, Env.MIRAKURUN_PORT)
102+
103+
// 接続テスト
104+
runBlocking {
105+
api.getStatus()
106+
}
107+
108+
api
109+
} catch (e: ResponseException) {
110+
logger.info { "Mirakurun/mirakc に接続できません。Mirakurun/mirakc 連携機能は提供しません。" }
111+
null
112+
}
113+
}

src/main/kotlin/blue/starry/saya/services/mirakurun/Conversion.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package blue.starry.saya.services.mirakurun
22

33
import blue.starry.jsonkt.jsonObjectOf
4+
import blue.starry.saya.common.createSayaLogger
45
import blue.starry.saya.models.TunerProcess
56
import kotlinx.serialization.json.jsonPrimitive
67
import mu.KotlinLogging
@@ -10,7 +11,7 @@ import blue.starry.saya.models.Program as SayaProgram
1011
import blue.starry.saya.models.Service as SayaService
1112
import blue.starry.saya.models.Tuner as SayaTuner
1213

13-
private val logger = KotlinLogging.logger("saya.mirakurun")
14+
private val logger = KotlinLogging.createSayaLogger("saya.mirakurun")
1415

1516
fun Service.toSayaService(): SayaService? {
1617
return SayaService(

0 commit comments

Comments
 (0)