Skip to content

Commit fa2f8b2

Browse files
committed
refactor: Refactor into services and repositories
1 parent 7a1957d commit fa2f8b2

30 files changed

+623
-542
lines changed

build.gradle.kts

+11-18
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,11 @@ tasks {
1111
expand("projectVersion" to project.version)
1212
}
1313

14-
/*
15-
Dummy task to hack gradle-semantic-release-plugin to release this project.
16-
17-
Explanation:
18-
SemVer is a standard for versioning libraries.
19-
For that reason the semantic-release plugin uses the "publish" task to publish libraries.
20-
However, this subproject is not a library, and the "publish" task is not available for this subproject.
21-
Because semantic-release is not designed to handle this case, we need to hack it.
22-
23-
RE: https://github.com/KengoTODA/gradle-semantic-release-plugin/issues/435
24-
*/
14+
// Needed by gradle-semantic-release-plugin.
15+
// Tracking: https://github.com/KengoTODA/gradle-semantic-release-plugin/issues/435
2516
register<DefaultTask>("publish") {
2617
group = "publishing"
27-
description = "Dummy task to hack gradle-semantic-release-plugin to release ReVanced API"
28-
dependsOn(startShadowScripts)
18+
dependsOn(shadowJar)
2919
}
3020
}
3121

@@ -42,8 +32,15 @@ ktor {
4232
repositories {
4333
mavenCentral()
4434
google()
45-
maven { url = uri("https://jitpack.io") }
4635
mavenLocal()
36+
maven {
37+
// A repository must be specified for some reason. "registry" is a dummy.
38+
url = uri("https://maven.pkg.github.com/revanced/registry")
39+
credentials {
40+
username = project.findProperty("gpr.user") as String? ?: System.getenv("GITHUB_ACTOR")
41+
password = project.findProperty("gpr.key") as String? ?: System.getenv("GITHUB_TOKEN")
42+
}
43+
}
4744
}
4845

4946
dependencies {
@@ -78,8 +75,4 @@ dependencies {
7875
implementation(libs.revanced.patcher)
7976
implementation(libs.revanced.library)
8077
implementation(libs.caffeine)
81-
82-
testImplementation(libs.mockk)
83-
testImplementation(libs.ktor.server.tests)
84-
testImplementation(libs.kotlin.test.junit)
8578
}

gradle/libs.versions.toml

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
[versions]
2-
kotlin = "1.9.22"
2+
kotlin = "2.0.0"
33
logback = "1.4.14"
44
exposed = "0.41.1"
55
h2 = "2.2.224"
66
koin = "3.5.3"
77
dotenv = "6.4.1"
88
ktor = "2.3.7"
99
ktoml = "0.5.1"
10-
picocli = "4.7.3"
10+
picocli = "4.7.5"
1111
datetime = "0.5.0"
12-
mockk = "1.13.9"
13-
revanced-patcher = "19.2.0"
14-
revanced-library = "1.5.0"
12+
revanced-patcher = "19.3.1"
13+
revanced-library = "2.3.0"
1514
caffeine = "3.1.8"
1615

1716
[libraries]
@@ -39,13 +38,10 @@ exposed-jdbc = { module = "org.jetbrains.exposed:exposed-jdbc", version.ref = "e
3938
exposed-dao = { module = "org.jetbrains.exposed:exposed-dao", version.ref = "exposed" }
4039
exposed-kotlin-datetime = { module = "org.jetbrains.exposed:exposed-kotlin-datetime", version.ref = "exposed" }
4140
dotenv-kotlin = { module = "io.github.cdimascio:dotenv-kotlin", version.ref = "dotenv" }
42-
ktor-server-tests = { module = "io.ktor:ktor-server-tests" }
43-
kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin" }
4441
ktoml-core = { module = "com.akuleshov7:ktoml-core", version.ref = "ktoml" }
4542
ktoml-file = { module = "com.akuleshov7:ktoml-file", version.ref = "ktoml" }
4643
picocli = { module = "info.picocli:picocli", version.ref = "picocli" }
4744
kotlinx-datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "datetime" }
48-
mockk = { module = "io.mockk:mockk", version.ref = "mockk" }
4945
revanced-patcher = { module = "app.revanced:revanced-patcher", version.ref = "revanced-patcher" }
5046
revanced-library = { module = "app.revanced:revanced-library", version.ref = "revanced-library" }
5147
caffeine = { module = "com.github.ben-manes.caffeine:caffeine", version.ref = "caffeine" }

gradle/wrapper/gradle-wrapper.jar

-15.7 KB
Binary file not shown.
+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
3+
distributionSha256Sum=a4b4158601f8636cdeeab09bd76afb640030bb5b144aafe261a5e8af027dc612
4+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
5+
networkTimeout=10000
6+
validateDistributionUrl=true
47
zipStoreBase=GRADLE_USER_HOME
58
zipStorePath=wrapper/dists

gradlew

+28-13
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
# Darwin, MinGW, and NonStop.
5656
#
5757
# (3) This script is generated from the Groovy template
58-
# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
58+
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
5959
# within the Gradle project.
6060
#
6161
# You can find Gradle at https://github.com/gradle/gradle/.
@@ -80,13 +80,11 @@ do
8080
esac
8181
done
8282

83-
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
84-
85-
APP_NAME="Gradle"
83+
# This is normally unused
84+
# shellcheck disable=SC2034
8685
APP_BASE_NAME=${0##*/}
87-
88-
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
89-
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
86+
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
87+
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
9088

9189
# Use the maximum available, or set MAX_FD != -1 to use that value.
9290
MAX_FD=maximum
@@ -133,22 +131,29 @@ location of your Java installation."
133131
fi
134132
else
135133
JAVACMD=java
136-
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
134+
if ! command -v java >/dev/null 2>&1
135+
then
136+
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
137137
138138
Please set the JAVA_HOME variable in your environment to match the
139139
location of your Java installation."
140+
fi
140141
fi
141142

142143
# Increase the maximum file descriptors if we can.
143144
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
144145
case $MAX_FD in #(
145146
max*)
147+
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
148+
# shellcheck disable=SC2039,SC3045
146149
MAX_FD=$( ulimit -H -n ) ||
147150
warn "Could not query maximum file descriptor limit"
148151
esac
149152
case $MAX_FD in #(
150153
'' | soft) :;; #(
151154
*)
155+
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
156+
# shellcheck disable=SC2039,SC3045
152157
ulimit -n "$MAX_FD" ||
153158
warn "Could not set maximum file descriptor limit to $MAX_FD"
154159
esac
@@ -193,18 +198,28 @@ if "$cygwin" || "$msys" ; then
193198
done
194199
fi
195200

196-
# Collect all arguments for the java command;
197-
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
198-
# shell script including quotes and variable substitutions, so put them in
199-
# double quotes to make sure that they get re-expanded; and
200-
# * put everything else in single quotes, so that it's not re-expanded.
201+
202+
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
203+
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
204+
205+
# Collect all arguments for the java command:
206+
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
207+
# and any embedded shellness will be escaped.
208+
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
209+
# treated as '${Hostname}' itself on the command line.
201210

202211
set -- \
203212
"-Dorg.gradle.appname=$APP_BASE_NAME" \
204213
-classpath "$CLASSPATH" \
205214
org.gradle.wrapper.GradleWrapperMain \
206215
"$@"
207216

217+
# Stop when "xargs" is not available.
218+
if ! command -v xargs >/dev/null 2>&1
219+
then
220+
die "xargs is not available"
221+
fi
222+
208223
# Use "xargs" to parse quoted args.
209224
#
210225
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.

gradlew.bat

+19-16
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
@rem limitations under the License.
1515
@rem
1616

17-
@if "%DEBUG%" == "" @echo off
17+
@if "%DEBUG%"=="" @echo off
1818
@rem ##########################################################################
1919
@rem
2020
@rem Gradle startup script for Windows
@@ -25,7 +25,8 @@
2525
if "%OS%"=="Windows_NT" setlocal
2626

2727
set DIRNAME=%~dp0
28-
if "%DIRNAME%" == "" set DIRNAME=.
28+
if "%DIRNAME%"=="" set DIRNAME=.
29+
@rem This is normally unused
2930
set APP_BASE_NAME=%~n0
3031
set APP_HOME=%DIRNAME%
3132

@@ -40,13 +41,13 @@ if defined JAVA_HOME goto findJavaFromJavaHome
4041

4142
set JAVA_EXE=java.exe
4243
%JAVA_EXE% -version >NUL 2>&1
43-
if "%ERRORLEVEL%" == "0" goto execute
44+
if %ERRORLEVEL% equ 0 goto execute
4445

45-
echo.
46-
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
47-
echo.
48-
echo Please set the JAVA_HOME variable in your environment to match the
49-
echo location of your Java installation.
46+
echo. 1>&2
47+
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
48+
echo. 1>&2
49+
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
50+
echo location of your Java installation. 1>&2
5051

5152
goto fail
5253

@@ -56,11 +57,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe
5657

5758
if exist "%JAVA_EXE%" goto execute
5859

59-
echo.
60-
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
61-
echo.
62-
echo Please set the JAVA_HOME variable in your environment to match the
63-
echo location of your Java installation.
60+
echo. 1>&2
61+
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
62+
echo. 1>&2
63+
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
64+
echo location of your Java installation. 1>&2
6465

6566
goto fail
6667

@@ -75,13 +76,15 @@ set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
7576

7677
:end
7778
@rem End local scope for the variables with windows NT shell
78-
if "%ERRORLEVEL%"=="0" goto mainEnd
79+
if %ERRORLEVEL% equ 0 goto mainEnd
7980

8081
:fail
8182
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
8283
rem the _cmd.exe /c_ return code!
83-
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
84-
exit /b 1
84+
set EXIT_CODE=%ERRORLEVEL%
85+
if %EXIT_CODE% equ 0 set EXIT_CODE=1
86+
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
87+
exit /b %EXIT_CODE%
8588

8689
:mainEnd
8790
if "%OS%"=="Windows_NT" endlocal

src/main/kotlin/app/revanced/api/command/StartAPICommand.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package app.revanced.api.command
22

3-
import app.revanced.api.modules.*
3+
import app.revanced.api.configuration.*
4+
import app.revanced.api.configuration.routing.configureRouting
45
import io.ktor.server.engine.*
56
import io.ktor.server.netty.*
67
import picocli.CommandLine
@@ -27,7 +28,7 @@ internal object StartAPICommand : Runnable {
2728
override fun run() {
2829
embeddedServer(Netty, port, host) {
2930
configureDependencies()
30-
configureHTTP()
31+
configureHTTP(allowedHost = host)
3132
configureSerialization()
3233
configureSecurity()
3334
configureRouting()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package app.revanced.api.configuration
2+
3+
import app.revanced.api.repository.AnnouncementRepository
4+
import app.revanced.api.repository.ConfigurationRepository
5+
import app.revanced.api.repository.backend.BackendRepository
6+
import app.revanced.api.repository.backend.github.GitHubBackendRepository
7+
import app.revanced.api.services.AnnouncementService
8+
import app.revanced.api.services.ApiService
9+
import app.revanced.api.services.AuthService
10+
import app.revanced.api.services.PatchesService
11+
import com.akuleshov7.ktoml.Toml
12+
import com.akuleshov7.ktoml.source.decodeFromStream
13+
import io.github.cdimascio.dotenv.Dotenv
14+
import io.ktor.server.application.*
15+
import org.jetbrains.exposed.sql.Database
16+
import org.koin.core.module.dsl.singleOf
17+
import org.koin.dsl.bind
18+
import org.koin.dsl.module
19+
import org.koin.ktor.plugin.Koin
20+
import java.io.File
21+
22+
fun Application.configureDependencies() {
23+
val globalModule = module {
24+
single {
25+
Dotenv.configure()
26+
.systemProperties()
27+
.load()
28+
}
29+
}
30+
31+
val repositoryModule = module {
32+
single {
33+
val dotenv = get<Dotenv>()
34+
35+
Database.connect(
36+
url = dotenv["DB_URL"],
37+
user = dotenv["DB_USER"],
38+
password = dotenv["DB_PASSWORD"],
39+
driver = "org.h2.Driver",
40+
)
41+
}
42+
43+
single {
44+
val configFilePath = get<Dotenv>()["CONFIG_FILE_PATH"]
45+
val configFile = File(configFilePath).inputStream()
46+
47+
Toml.decodeFromStream<ConfigurationRepository>(configFile)
48+
}
49+
50+
singleOf(::AnnouncementRepository)
51+
}
52+
53+
val serviceModule = module {
54+
single {
55+
val dotenv = get<Dotenv>()
56+
57+
val jwtSecret = dotenv["JWT_SECRET"]
58+
val issuer = dotenv["JWT_ISSUER"]
59+
val validityInMin = dotenv["JWT_VALIDITY_IN_MIN"].toInt()
60+
61+
val basicUsername = dotenv["BASIC_USERNAME"]
62+
val basicPassword = dotenv["BASIC_PASSWORD"]
63+
64+
AuthService(issuer, validityInMin, jwtSecret, basicUsername, basicPassword)
65+
}
66+
single {
67+
val token = get<Dotenv>()["GITHUB_TOKEN"]
68+
69+
GitHubBackendRepository(token)
70+
} bind BackendRepository::class
71+
singleOf(::AnnouncementService)
72+
singleOf(::PatchesService)
73+
singleOf(::ApiService)
74+
}
75+
76+
install(Koin) {
77+
modules(
78+
globalModule,
79+
repositoryModule,
80+
serviceModule,
81+
)
82+
}
83+
}

src/main/kotlin/app/revanced/api/modules/HTTP.kt renamed to src/main/kotlin/app/revanced/api/configuration/HTTP.kt

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package app.revanced.api.modules
1+
package app.revanced.api.configuration
22

33
import io.ktor.http.*
44
import io.ktor.http.content.*
@@ -8,15 +8,17 @@ import io.ktor.server.plugins.conditionalheaders.*
88
import io.ktor.server.plugins.cors.routing.*
99
import kotlin.time.Duration.Companion.minutes
1010

11-
fun Application.configureHTTP() {
11+
fun Application.configureHTTP(
12+
allowedHost: String,
13+
) {
1214
install(ConditionalHeaders)
1315
install(CORS) {
1416
allowMethod(HttpMethod.Options)
1517
allowMethod(HttpMethod.Put)
1618
allowMethod(HttpMethod.Delete)
1719
allowMethod(HttpMethod.Patch)
1820
allowHeader(HttpHeaders.Authorization)
19-
anyHost() // @TODO: Don't do this in production if possible. Try to limit it.
21+
allowHost(allowedHost)
2022
}
2123
install(CachingHeaders) {
2224
options { _, _ -> CachingOptions(CacheControl.MaxAge(maxAgeSeconds = 5.minutes.inWholeSeconds.toInt())) }

0 commit comments

Comments
 (0)