diff --git a/README.md b/README.md
index fcf75ad..7d9a3d8 100644
--- a/README.md
+++ b/README.md
@@ -23,7 +23,7 @@ Maven
gg.flyte
twilight
- 1.1.10
+ 1.1.11
```
@@ -33,14 +33,14 @@ maven {
url "https://repo.flyte.gg/releases"
}
-implementation "gg.flyte:twilight:1.1.10"
+implementation "gg.flyte:twilight:1.1.11"
```
Gradle (Kotlin DSL)
```kotlin
maven("https://repo.flyte.gg/releases")
-implementation("gg.flyte:twilight:1.1.10")
+implementation("gg.flyte:twilight:1.1.11")
```
Certain features of Twilight require configuration, which can be done via the Twilight class. To setup a Twilight class instance, you can use the `twilight` function as shown below:
@@ -437,44 +437,63 @@ You can use the following Environment variables for your Redis Server:
REDIS_HOST="your redis server host"
REDIS_PORT="your redis server port"
REDIS_TIMEOUT="your redis connection timeout"
-REDIS_USING_PASSWORD="false"
-REDIS_USERNAME:""
-REDIS_PASSWORD:""
+REDIS_AUTHENTICATION="NONE"
```
-Alternativley, if your Redis server requires a Username + Password in order to access, you can use the following:
+Alternatively, if your Redis server requires a Username + Password in order to access, you can use the following:
```env
REDIS_HOST="your redis server host"
REDIS_PORT="your redis server port"
REDIS_TIMEOUT="your redis connection timeout"
-REDIS_USING_PASSWORD="true"
+REDIS_AUTHENTICATION="USERNAME_PASSWORD"
REDIS_USERNAME:"coolUsername"
REDIS_PASSWORD:"coolPassword"
```
+Alternatively, if your Redis server requires a URL in order to access, you can use the following:
+```env
+REDIS_HOST="your redis server host"
+REDIS_PORT="your redis server port"
+REDIS_TIMEOUT="your redis connection timeout"
+REDIS_AUTHENTICATION="URL"
+REDIS_URL="coolURL"
+```
+
#### Builder
When building your Twilight instance, you can specify your host and port like so:
```kotlin
val twilight = twilight(plugin) {
redis {
+ authentication = Authentication.NONE
host = "your redis server host"
port = 6379 // Default Redis Port
timeout = 500 // 500 Milliseconds Timeout
- isUsingPassword = false // False by default
}
}
```
-Alternativley, if your Redis server requires a Username + Password in order to access, you can use the following:
+Alternatively, if your Redis server requires a Username + Password in order to access, you can use the following:
```kotlin
val twilight = twilight(plugin) {
redis {
+ authentication = Authentication.USERNAME_PASSWORD
host = "your redis server host"
port = 6379 // Default Redis Port
timeout = 500 // 500 Milliseconds Timeout
- isUsingPassword = true
username = "coolUsername"
password = "coolPassword"
}
}
```
+Alternatively, if your Redis server requires a URL in order to access, you can use the following:
+```kotlin
+val twilight = twilight(plugin) {
+ redis {
+ authentication = Authentication.URL
+ host = "your redis server host"
+ port = 6379 // Default Redis Port
+ timeout = 500 // 500 Milliseconds Timeout
+ url = "coolURL"
+ }
+}
+```
#### String Key-Value Pairs
You can Set/Get/Delete String Key-Value pairs on your Redis server like so: (All of those functions are Async and return a CompleteableFuture)
```kotlin
diff --git a/build.gradle.kts b/build.gradle.kts
index 65c0b68..a759b4d 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -4,7 +4,7 @@ plugins {
}
group = "gg.flyte"
-version = "1.1.10"
+version = "1.1.11"
repositories {
mavenLocal()
diff --git a/src/main/kotlin/gg/flyte/twilight/data/Redis.kt b/src/main/kotlin/gg/flyte/twilight/data/Redis.kt
index 9141e0c..9e0797d 100644
--- a/src/main/kotlin/gg/flyte/twilight/data/Redis.kt
+++ b/src/main/kotlin/gg/flyte/twilight/data/Redis.kt
@@ -11,15 +11,19 @@ import java.util.concurrent.Executors
object Redis {
private lateinit var jedis: Jedis
+ private lateinit var settings: Settings
private val executor: Executor = Executors.newCachedThreadPool()
fun redis(redis: Settings) {
- if (redis.isUsingPassword){
- val config = DefaultJedisClientConfig.builder().user(redis.username).password(redis.password).timeoutMillis(redis.timeout).build()
- jedis = Jedis(redis.host, redis.port, config)
- return
- }
- jedis = Jedis(redis.host, redis.port, redis.timeout)
+ jedis = getClient(redis)
+ settings = redis
+ }
+
+ private fun getClient(redis: Settings): Jedis = when (redis.authentication){
+ Authentication.NONE -> Jedis(redis.host, redis.port)
+ Authentication.USERNAME_PASSWORD -> Jedis(redis.host, redis.port, DefaultJedisClientConfig.builder().user(redis.username).password(redis.password).timeoutMillis(redis.timeout).build())
+ Authentication.URL -> Jedis(redis.url)
}
+
private fun publishSync(channel: String, message: String) = jedis.publish(channel, message)
fun publish(channel: String, message: String): CompletableFuture = CompletableFuture.supplyAsync({ publishSync(channel, message) }, executor)
private fun setSync(key: String, value: String) = jedis.set(key, value)
@@ -30,22 +34,44 @@ object Redis {
fun delete(key: String): CompletableFuture = CompletableFuture.supplyAsync({ deleteSync(key) }, executor)
fun addListener(listener: TwilightRedisListener): TwilightRedisListener {
- jedis.subscribe(listener, listener.channel)
+ val client = getClient(settings)
+ client.subscribe(listener, listener.channel)
+ client.close()
return listener
}
fun addListener(channel: String, block: RedisMessage.() -> Unit): TwilightRedisListener {
+ val client = getClient(settings)
val listener = RedisListener(channel, block)
- jedis.subscribe(listener, channel)
+ client.subscribe(listener, channel)
+ client.close()
return listener
}
class Settings {
+ var authentication = if (Twilight.usingEnv) Authentication.fromString(Environment.get("REDIS_AUTHENTICATION")) else Authentication.NONE
var host: String = if (Twilight.usingEnv) Environment.get("REDIS_HOST") else "localhost"
var port: Int = if (Twilight.usingEnv) Environment.get("REDIS_PORT").toInt() else 6379
var timeout: Int = if (Twilight.usingEnv) Environment.get("REDIS_TIMEOUT").toInt() else 0
- var isUsingPassword: Boolean = if (Twilight.usingEnv) Environment.get("REDIS_USING_PASSWORD").toBoolean() else false
- val username: String = if (Twilight.usingEnv && isUsingPassword) Environment.get("REDIS_USERNAME") else ""
- var password: String = if (Twilight.usingEnv && isUsingPassword) Environment.get("REDIS_PASSWORD") else ""
+ val username: String = if (Twilight.usingEnv && authentication == Authentication.USERNAME_PASSWORD) Environment.get("REDIS_USERNAME") else ""
+ var password: String = if (Twilight.usingEnv && authentication == Authentication.USERNAME_PASSWORD) Environment.get("REDIS_PASSWORD") else ""
+ var url: String = if (Twilight.usingEnv && authentication == Authentication.URL) Environment.get("REDIS_URL") else ""
+ }
+
+ enum class Authentication {
+ NONE,
+ USERNAME_PASSWORD,
+ URL;
+
+ companion object {
+ fun fromString(value: String): Authentication = when (value.uppercase()) {
+ "NONE" -> NONE
+ "USERNAME_PASSWORD" -> USERNAME_PASSWORD
+ "URL" -> URL
+ else -> throw IllegalArgumentException("Invalid authentication type: $value")
+ }
+ }
+
}
+
}
data class RedisMessage(val channel: String, val message: String, val listener: TwilightRedisListener)