1
1
package tamer
2
2
3
+ import java .time .{Duration => JDuration }
4
+
3
5
import zio ._
4
6
5
- final case class RegistryConfig (url : String , cacheSize : Int )
7
+ final case class RegistryConfig (url : String , cacheSize : Int , expiration : JDuration )
6
8
object RegistryConfig {
7
9
def apply (url : String ): RegistryConfig = RegistryConfig (
8
10
url = url,
9
- cacheSize = 1000
11
+ cacheSize = 4 ,
12
+ expiration = 1 .hour
10
13
)
11
14
val config : Config [Option [RegistryConfig ]] =
12
- (Config .string(" url" ) ++ Config .int(" cache_size" ).withDefault(1000 )).map { case (url, cacheSize) =>
13
- RegistryConfig (url, cacheSize)
15
+ (Config .string(" url" ) ++ Config .int(" cache_size" ).withDefault(4 ) ++ Config .duration( " expiration " ).withDefault( 1 .hour)).map {
16
+ case (url, cacheSize, expiration) => RegistryConfig (url, cacheSize, expiration )
14
17
}.optional
15
18
}
16
19
17
- final case class SinkConfig (topic : String )
18
- object SinkConfig {
19
- val config : Config [SinkConfig ] = Config .string(" topic" ).map(SinkConfig .apply)
20
+ final case class TopicOptions (partitions : Int , replicas : Short )
21
+ object TopicOptions {
22
+ val config : Config [Option [TopicOptions ]] =
23
+ (Config .boolean(" auto_create" ).withDefault(false ) ++
24
+ Config .int(" partitions" ).withDefault(1 ) ++
25
+ Config .int(" replicas" ).map(_.toShort).withDefault(1 .toShort)).map {
26
+ case (true , partitions, replicas) => Some (TopicOptions (partitions, replicas))
27
+ case _ => None
28
+ }
20
29
}
21
30
22
- final case class StateConfig (topic : String , groupId : String , clientId : String )
23
- object StateConfig {
24
- val config : Config [StateConfig ] =
25
- (Config .string(" topic" ) ++ Config .string(" group_id" ) ++ Config .string(" client_id" )).map { case (stateTopic, stateGroupId, stateClientId) =>
26
- StateConfig (stateTopic, stateGroupId, stateClientId)
27
- }
31
+ final case class TopicConfig (topicName : String , maybeTopicOptions : Option [TopicOptions ])
32
+ object TopicConfig {
33
+ def apply (topicName : String ): TopicConfig = new TopicConfig (
34
+ topicName = topicName,
35
+ maybeTopicOptions = None
36
+ )
37
+ val config : Config [TopicConfig ] = (Config .string(" topic" ) ++ TopicOptions .config).map { case (topicName, maybeTopicOptions) =>
38
+ TopicConfig (topicName, maybeTopicOptions)
39
+ }
28
40
}
29
41
30
42
final case class KafkaConfig (
31
43
brokers : List [String ],
32
44
maybeRegistry : Option [RegistryConfig ],
33
45
closeTimeout : Duration ,
34
46
bufferSize : Int ,
35
- sink : SinkConfig ,
36
- state : StateConfig ,
47
+ sink : TopicConfig ,
48
+ state : TopicConfig ,
49
+ groupId : String ,
50
+ clientId : String ,
37
51
transactionalId : String ,
38
52
properties : Map [String , AnyRef ]
39
53
)
@@ -43,8 +57,10 @@ object KafkaConfig {
43
57
maybeRegistry : Option [RegistryConfig ],
44
58
closeTimeout : Duration ,
45
59
bufferSize : Int ,
46
- sink : SinkConfig ,
47
- state : StateConfig ,
60
+ sink : TopicConfig ,
61
+ state : TopicConfig ,
62
+ groupId : String ,
63
+ clientId : String ,
48
64
transactionalId : String
49
65
): KafkaConfig = new KafkaConfig (
50
66
brokers = brokers,
@@ -53,6 +69,8 @@ object KafkaConfig {
53
69
bufferSize = bufferSize,
54
70
sink = sink,
55
71
state = state,
72
+ groupId = groupId,
73
+ clientId = clientId,
56
74
transactionalId = transactionalId,
57
75
properties = Map .empty
58
76
)
@@ -62,11 +80,13 @@ object KafkaConfig {
62
80
RegistryConfig .config.nested(" schema_registry" ) ++
63
81
Config .duration(" close_timeout" ) ++
64
82
Config .int(" buffer_size" ) ++
65
- SinkConfig .config.nested(" sink" ) ++
66
- StateConfig .config.nested(" state" ) ++
83
+ TopicConfig .config.nested(" sink" ) ++
84
+ TopicConfig .config.nested(" state" ) ++
85
+ Config .string(" group_id" ) ++
86
+ Config .string(" client_id" ) ++
67
87
Config .string(" transactional_id" )
68
- ).map { case (brokers, maybeRegistry, closeTimeout, bufferSize, sink, state, transactionalId) =>
69
- KafkaConfig (brokers, maybeRegistry, closeTimeout, bufferSize, sink, state, transactionalId)
88
+ ).map { case (brokers, maybeRegistry, closeTimeout, bufferSize, sink, state, groupId, clientId, transactionalId) =>
89
+ KafkaConfig (brokers, maybeRegistry, closeTimeout, bufferSize, sink, state, groupId, clientId, transactionalId)
70
90
}.nested(" kafka" )
71
91
72
92
final val fromEnvironment : TaskLayer [KafkaConfig ] = ZLayer {
0 commit comments