Skip to content

Commit 78886a3

Browse files
committed
switch to not using bullshit
1 parent 54d16ad commit 78886a3

File tree

7 files changed

+60
-74
lines changed

7 files changed

+60
-74
lines changed

build.gradle.kts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ group = "dev.lythium.pulsar"
1717
version = "0.0.1"
1818

1919
application {
20-
mainClass.set("io.ktor.server.netty.EngineMain")
20+
mainClass.set("dev.lythium.pulsar.ApplicationKt")
2121
}
2222

2323
repositories {
@@ -35,8 +35,7 @@ dependencies {
3535
implementation("io.ktor:ktor-server-content-negotiation-jvm:$ktorVersion")
3636
implementation("io.ktor:ktor-serialization-kotlinx-json-jvm:$ktorVersion")
3737

38-
implementation("io.ktor:ktor-client-core:$ktorVersion")
39-
implementation("io.ktor:ktor-client-jetty:$ktorVersion")
38+
implementation("com.squareup.okhttp3:okhttp:4.12.0")
4039

4140
implementation("org.jetbrains.exposed:exposed-core:$exposedVersion")
4241
implementation("org.jetbrains.exposed:exposed-jdbc:$exposedVersion")
@@ -48,3 +47,8 @@ dependencies {
4847
implementation("io.github.cdimascio:dotenv-kotlin:$dotenvVersion")
4948
}
5049

50+
ktor {
51+
fatJar {
52+
archiveFileName.set("pulsar-backend-prod.jar")
53+
}
54+
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ktorVersion=2.3.8
1+
ktorVersion=3.0.0-beta-1
22
kotlinVersion=1.9.22
33
logbackVersion=1.4.14
44
kotlin.code.style=official

src/main/kotlin/dev/lythium/pulsar/Addons.kt

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package dev.lythium.pulsar
22

3-
import io.ktor.client.*
4-
import io.ktor.client.engine.jetty.*
5-
import io.ktor.client.request.*
6-
import io.ktor.client.statement.*
73
import io.ktor.http.*
84
import kotlinx.serialization.Serializable
95
import kotlinx.serialization.json.Json
10-
import org.eclipse.jetty.util.ssl.SslContextFactory
6+
import okhttp3.OkHttpClient
7+
import okhttp3.Request
118
import java.util.*
129

1310
@Serializable
@@ -36,34 +33,24 @@ data class PurchaseData(
3633
object Addons {
3734
private var addons: Array<Addon>? = null
3835

39-
suspend fun get(): Array<Addon>? {
36+
fun get(): Array<Addon>? {
4037
if (this.addons?.isEmpty() == true || this.addons == null) {
41-
val httpClient = HttpClient(Jetty) {
42-
engine {
43-
sslContextFactory = SslContextFactory.Client()
44-
clientCacheSize = 12
45-
}
46-
}
38+
val client = OkHttpClient.Builder()
39+
.build()
4740

48-
val response: HttpResponse =
49-
httpClient.request("https://www.gmodstore.com/api/v3/teams/${Environment.dotenv.get("GMS_TEAM_ID")}/products") {
50-
method = HttpMethod.Get
51-
headers {
52-
append(HttpHeaders.Authorization, "Bearer " + Environment.dotenv.get("GMS_API_KEY"))
53-
}
54-
parameters {
55-
append("perPage", "100")
56-
}
57-
}
41+
val request = Request.Builder()
42+
.url("https://www.gmodstore.com/api/v3/teams/${Environment.dotenv.get("GMS_TEAM_ID")}/products")
43+
.addHeader(HttpHeaders.Authorization, "Bearer " + Environment.dotenv.get("GMS_API_KEY"))
44+
.build()
5845

59-
val responseBody = response.bodyAsText()
46+
val response = client.newCall(request).execute()
47+
48+
val responseBody = response.body?.string()
6049

6150
val json = Json { ignoreUnknownKeys = true }
62-
val addonResponse = json.decodeFromString<AddonResponse>(responseBody)
51+
val addonResponse = json.decodeFromString<AddonResponse>(responseBody!!)
6352

6453
this.addons = addonResponse.data.map { Addon(it.id, it.name) }.toTypedArray()
65-
66-
httpClient.close()
6754
}
6855

6956
return this.addons

src/main/kotlin/dev/lythium/pulsar/Application.kt

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.lythium.pulsar
22

3+
import dev.lythium.pulsar.Tickets.addon
34
import dev.lythium.pulsar.routes.ticketRoutes
45
import dev.lythium.pulsar.routes.userRoutes
56
import io.github.cdimascio.dotenv.dotenv
@@ -35,30 +36,32 @@ suspend fun main(args: Array<String>) {
3536
val username = Environment.dotenv.get("DATABASE_USERNAME")
3637
val password = Environment.dotenv.get("DATABASE_PASSWORD")
3738

38-
if (connectionString == null) {
39+
connectionString ?: run {
3940
println("DATABASE_URL NOT SET!")
4041
exitProcess(1)
4142
}
42-
if (username == null) {
43+
44+
username ?: run {
4345
println("DATABASE_USERNAME NOT SET!")
4446
exitProcess(1)
4547
}
46-
if (password == null) {
48+
49+
password ?: run {
4750
println("DATABASE_PASSWORD NOT SET!")
4851
exitProcess(1)
4952
}
5053

51-
if (Environment.dotenv.get("API_KEY") == null) {
54+
Environment.dotenv.get("API_KEY") ?: run {
5255
println("API_KEY NOT SET!")
5356
exitProcess(1)
5457
}
5558

56-
if (Environment.dotenv.get("GMS_API_KEY") == null) {
59+
Environment.dotenv.get("GMS_API_KEY") ?: run {
5760
println("GMS_API_KEY NOT SET!")
5861
exitProcess(1)
5962
}
6063

61-
if (Environment.dotenv.get("GMS_TEAM_ID") == null) {
64+
Environment.dotenv.get("GMS_TEAM_ID") ?: run {
6265
println("GMS_TEAM_ID NOT SET!")
6366
exitProcess(1)
6467
}
@@ -121,12 +124,8 @@ fun Application.module() {
121124
call.respond(addons!!)
122125
}
123126
get("/addons/{id}") {
124-
val id = call.parameters["id"]?.toString()
125-
126-
if (id == null) {
127-
call.respond(HttpStatusCode.BadRequest, "Invalid addon parameter.")
128-
return@get
129-
}
127+
val id =
128+
call.parameters["id"] ?: return@get call.respond(HttpStatusCode.BadRequest, "Invalid addon parameter.")
130129

131130
val addonId: UUID?
132131
try {
@@ -136,13 +135,11 @@ fun Application.module() {
136135
return@get
137136
}
138137

139-
val addon = Addons.getAddon(addonId)
140-
if (addon != null) {
141-
call.respond(HttpStatusCode.OK, addon)
142-
} else {
143-
call.respond(HttpStatusCode.NotFound, "Addon not found.")
144-
}
138+
val addon = Addons.getAddon(addonId) ?: return@get call.respond(HttpStatusCode.NotFound, "Addon not found.")
139+
140+
call.respond(HttpStatusCode.OK, addon)
145141
}
142+
146143
get("*") {
147144
call.respondText("404 Not Found", status = HttpStatusCode.NotFound)
148145
}

src/main/kotlin/dev/lythium/pulsar/User.kt

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package dev.lythium.pulsar
22

3-
import io.ktor.client.*
4-
import io.ktor.client.engine.jetty.*
5-
import io.ktor.client.request.*
6-
import io.ktor.client.statement.*
73
import io.ktor.http.*
84
import kotlinx.serialization.ExperimentalSerializationApi
95
import kotlinx.serialization.KSerializer
@@ -12,7 +8,8 @@ import kotlinx.serialization.Serializer
128
import kotlinx.serialization.encoding.Decoder
139
import kotlinx.serialization.encoding.Encoder
1410
import kotlinx.serialization.json.Json
15-
import org.eclipse.jetty.util.ssl.SslContextFactory
11+
import okhttp3.OkHttpClient
12+
import okhttp3.Request
1613
import org.jetbrains.exposed.dao.id.UUIDTable
1714
import java.util.*
1815

@@ -36,33 +33,22 @@ class User(
3633
val discordId: Long
3734
) {
3835
suspend fun getOwnedAddons(): Array<Addon?> {
39-
val httpClient = HttpClient(Jetty) {
40-
engine {
41-
sslContextFactory = SslContextFactory.Client()
42-
clientCacheSize = 12
43-
}
44-
}
36+
val client = OkHttpClient.Builder()
37+
.build()
4538

4639
val productIdFilter = Addons.get()?.joinToString(",") { it.id.toString() }
4740

48-
val response: HttpResponse =
49-
httpClient.request("https://www.gmodstore.com/api/v3/users/${this.gmodstoreId}/purchases") {
50-
method = HttpMethod.Get
51-
headers {
52-
append(HttpHeaders.Authorization, "Bearer " + Environment.dotenv.get("GMS_API_KEY"))
53-
}
54-
url {
55-
parameters.append("perPage", "100")
56-
parameters.append("filter[revoked]", "false")
57-
parameters.append("filter[productId]", "$productIdFilter")
58-
}
59-
}
41+
val request = Request.Builder()
42+
.url("https://www.gmodstore.com/api/v3/users/${this.gmodstoreId}/purchases?perPage=100&filter[revoked]=false&filter[productId]=$productIdFilter")
43+
.addHeader(HttpHeaders.Authorization, "Bearer " + Environment.dotenv.get("GMS_API_KEY"))
44+
.build()
6045

61-
httpClient.close()
46+
val response = client.newCall(request).execute()
47+
48+
val responseBody = response.body?.string()
6249

63-
val responseBody = response.bodyAsText()
6450
val json = Json { ignoreUnknownKeys = true }
65-
val addonResponse = json.decodeFromString<PurchaseResponse>(responseBody)
51+
val addonResponse = json.decodeFromString<PurchaseResponse>(responseBody!!)
6652

6753
val ownedAddons = addonResponse.data.map {
6854
Addons.getAddon(it.productId)

src/main/resources/application.conf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ ktor {
22
development = true
33
deployment {
44
port = 8080
5+
port = ${?PORT}
6+
sslPort = 8443
7+
sslPort = ${?SSL_PORT}
58
watch = [ classes, resources ]
69
}
710

@@ -10,4 +13,13 @@ development = true
1013
dev.lythium.pulsar.ApplicationKt.module
1114
]
1215
}
16+
17+
security {
18+
ssl {
19+
keyStore = test.jks
20+
keyAlias = testkey
21+
keyStorePassword = Password
22+
privateKeyPassword = Password
23+
}
24+
}
1325
}

test.jks

4.31 KB
Binary file not shown.

0 commit comments

Comments
 (0)