package Ġno . nav . sy fo Ċ Ċ import Ġcom . auth 0 . j wk . J wk Provider Builder Ċ import Ġcom . fasterxml . jackson . databind . ObjectMapper Ċ import Ġcom . fasterxml . jackson . databind . Serialization Feature Ċ import Ġcom . fasterxml . jackson . datatype . js r 3 1 0 . Java Time Module Ċ import Ġcom . fasterxml . jackson . module . kotlin . readValue Ċ import Ġcom . fasterxml . jackson . module . kotlin . register Kotlin Module Ċ import Ġio . ktor . util . Internal API Ċ import Ġio . ktor . util . K tor Experimental API Ċ import Ġjava . net . URL Ċ import Ġjava . time . Duration Ċ import Ġjava . util . concurrent . TimeUnit Ċ import Ġkotlinx . coroutines . Coroutine Scope Ċ import Ġkotlinx . coroutines . Global Scope Ċ import Ġkotlinx . coroutines . Job Ċ import Ġkotlinx . coroutines . delay Ċ import Ġkotlinx . coroutines . launch Ċ import Ġnet . logstash . log back . argument . Structured Arguments Ċ import Ġno . nav . sy fo . application . Application Server Ċ import Ġno . nav . sy fo . application . Application State Ċ import Ġno . nav . sy fo . application . create Application Engine Ċ import Ġno . nav . sy fo . application . get Well Known Ċ import Ġno . nav . sy fo . client . Opp g ave Client Ċ import Ġno . nav . sy fo . clients . HttpClient s Ċ import Ġno . nav . sy fo . clients . Kafka Consum ers Ċ import Ġno . nav . sy fo . clients . Kafka Pro ducers Ċ import Ġno . nav . sy fo . db . Database Ċ import Ġno . nav . sy fo . db . Vault Credential Service Ċ import Ġno . nav . sy fo . model . P ap ir Sm Register ing Ċ import Ġno . nav . sy fo . persist ering . handle Rec ived Message Ċ import Ġno . nav . sy fo . service . Authorization Service Ċ import Ġno . nav . sy fo . service . Man ue ll Opp g ave Service Ċ import Ġno . nav . sy fo . sy k meld ing . Sy k meld ing Job Runner Ċ import Ġno . nav . sy fo . sy k meld ing . Sy k meld ing JobService Ċ import Ġno . nav . sy fo . util . Logging Meta Ċ import Ġno . nav . sy fo . util . Track able Exception Ċ import Ġno . nav . sy fo . util . getFile AsString Ċ import Ġno . nav . sy fo . vault . Renew Vault Service Ċ import Ġorg . apache . kafka . clients . consumer . Kafka Consumer Ċ import Ġorg . slf 4 j . Logger Ċ import Ġorg . slf 4 j . LoggerFactory Ċ Ċ val ĠobjectMapper : ĠObjectMapper Ġ= ĠObjectMapper () ĊĠĠĠ Ġ. register Module ( Java Time Module ()) ĊĠĠĠ Ġ. register Kotlin Module () ĊĠĠĠ Ġ. configure ( Serialization Feature . WRITE _ DAT ES _ AS _ TIMESTAMP S , Ġfalse ) Ċ Ċ val Ġlog : ĠLogger Ġ= ĠLoggerFactory . getLogger (" no . nav . sy fo . sm register ing backend ") Ċ Ċ @ Internal API Ċ @ K tor Experimental API Ċ fun Ġmain () Ġ{ ĊĠĠĠ Ġval Ġenv Ġ= ĠEnvironment () ĊĠĠĠ Ġval Ġvault Secrets Ġ= ĠVault Secrets ( ĊĠĠĠĠĠĠĠ Ġservice user Username Ġ= ĠgetFile AsString ( env . service user Username Path ), ĊĠĠĠĠĠĠĠ Ġservice user Password Ġ= ĠgetFile AsString ( env . service user Password Path ), ĊĠĠĠĠĠĠĠ Ġoid c Well Known Uri Ġ= ĠgetFile AsString ( env . oidc Well Known Uri Path ) ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġval Ġwell Known Ġ= Ġget Well Known ( vault Secrets . oidc Well Known Uri ) ĊĠĠĠ Ġval Ġj wk Provider Ġ= ĠJ wk Provider Builder ( URL ( well Known . jw ks _ uri )) ĊĠĠĠĠĠĠĠ Ġ. cached ( 1 0 , Ġ 2 4 , ĠTimeUnit . HOUR S ) ĊĠĠĠĠĠĠĠ Ġ. rate Limit ed ( 1 0 , Ġ 1 , ĠTimeUnit . MIN UTES ) ĊĠĠĠĠĠĠĠ Ġ. build () ĊĊĠĠĠ Ġval Ġvault Credential Service Ġ= ĠVault Credential Service () ĊĠĠĠ Ġval Ġdatabase Ġ= ĠDatabase ( env , Ġvault Credential Service ) ĊĊĠĠĠ Ġval Ġapplication State Ġ= ĠApplication State () ĊĊĠĠĠ Ġval Ġman ue ll Opp g ave Service Ġ= ĠMan ue ll Opp g ave Service ( database ) ĊĊĠĠĠ Ġval Ġkafka Consum ers Ġ= ĠKafka Consum ers ( env , Ġvault Secrets ) ĊĠĠĠ Ġval Ġkafka Pro ducers Ġ= ĠKafka Pro ducers ( env , Ġvault Secrets ) ĊĠĠĠ Ġval Ġhttp Clients Ġ= ĠHttpClient s ( env , Ġvault Secrets ) ĊĊĠĠĠ Ġval Ġsy k meld ing JobService Ġ= ĠS yk meld ing JobService ( database Interface Ġ= Ġdatabase ) ĊĠĠĠ Ġval Ġsy k meld ing Job Runner Ġ= ĠS yk meld ing Job Runner ( application State , Ġsy k meld ing JobService , Ġkafka Pro ducers . kafka Rec ieved Sy k meld ing Producer , Ġkafka Pro ducers . kafka Sy f oser vice Producer ) ĊĊĠĠĠ Ġval Ġapplication Engine Ġ= Ġcreate Application Engine ( sy k meld ing JobService , ĊĠĠĠĠĠĠĠ Ġenv , ĊĠĠĠĠĠĠĠ Ġapplication State , ĊĠĠĠĠĠĠĠ Ġvault Secrets , ĊĠĠĠĠĠĠĠ Ġj wk Provider , ĊĠĠĠĠĠĠĠ Ġwell Known . issuer , ĊĠĠĠĠĠĠĠ Ġman ue ll Opp g ave Service , ĊĠĠĠĠĠĠĠ Ġhttp Clients . saf Client , ĊĠĠĠĠĠĠĠ Ġhttp Clients . op pg ave Client , ĊĠĠĠĠĠĠĠ Ġhttp Clients . sar Client , ĊĠĠĠĠĠĠĠ Ġhttp Clients . dok Ar k iv Client , ĊĠĠĠĠĠĠĠ Ġhttp Clients . saf Journal post Service , ĊĠĠĠĠĠĠĠ Ġhttp Clients . re gel Client , ĊĠĠĠĠĠĠĠ Ġhttp Clients . p dl Service , ĊĠĠĠĠĠĠĠ Ġhttp Clients . sy k mel der Service , ĊĠĠĠĠĠĠĠ ĠAuthorization Service ( http Clients . sy fo Til gang s Kont roll Client , Ġhttp Clients . ms Graph Client ) ĊĠĠĠ Ġ) ĊĊĠĠĠ ĠApplication Server ( application Engine , Ġapplication State ). start () ĊĊĠĠĠ ĠRen ew Vault Service ( vault Credential Service , Ġapplication State ). start Renew Tasks () ĊĊĠĠĠ Ġapplication State . ready Ġ= Ġtrue ĊĊĠĠĠ ĠGlobal Scope . launch Ġ{ ĊĠĠĠĠĠĠĠ Ġsy k meld ing Job Runner . start Job Runner () ĊĠĠĠĠĠĠĠ Ġlog . info (" Started ĠS yk meld ing Job Runner ") ĊĠĠĠ Ġ} ĊĠĠĠ Ġlaunch Listeners ( ĊĠĠĠĠĠĠĠ Ġapplication State , ĊĠĠĠĠĠĠĠ Ġenv , ĊĠĠĠĠĠĠĠ Ġkafka Consum ers , ĊĠĠĠĠĠĠĠ Ġdatabase , ĊĠĠĠĠĠĠĠ Ġhttp Clients . op pg ave Client ĊĠĠĠ Ġ) Ċ } Ċ Ċ @ Internal API Ċ fun Ġcreate Listener ( application State : ĠApplication State , Ġaction : Ġsuspend ĠCor outine Scope . () Ġ-> ĠUnit ): ĠJob Ġ= ĊĠĠĠ ĠGlobal Scope . launch Ġ{ ĊĠĠĠĠĠĠĠ Ġtry Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġaction () ĊĠĠĠĠĠĠĠ Ġ} Ġcatch Ġ( e : ĠTrack able Exception ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġlog . error ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" En Ġu h Ã¥ nd ter t Ġfe il Ġopp st od , Ġapp lik as jon en Ġrest arter Ġ{}", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠStruct ured Arguments . fields ( e . logging Meta ), Ġe . cause ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠ Ġ} Ġfinally Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġapplication State . alive Ġ= Ġfalse ĊĠĠĠĠĠĠĠĠĠĠĠ Ġapplication State . ready Ġ= Ġfalse ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ċ Ċ @ Internal API Ċ @ K tor Experimental API Ċ fun Ġlaunch Listeners ( ĊĠĠĠ Ġapplication State : ĠApplication State , ĊĠĠĠ Ġenv : ĠEnvironment , ĊĠĠĠ Ġkafka Consum ers : ĠKafka Consum ers , ĊĠĠĠ Ġdatabase : ĠDatabase , ĊĠĠĠ Ġopp g ave Client : ĠO pp g ave Client Ċ ) Ġ{ ĊĠĠĠ Ġcreate Listener ( application State ) Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġkafka Consumer P ap ir Sm Register ing Ġ= Ġkafka Consum ers . kafka Consumer P ap ir Sm Register ing ĊĊĠĠĠĠĠĠĠ Ġkafka Consumer P ap ir Sm Register ing . subscribe ( listOf ( env . sm 2 0 1 3 Sm regist r ering Topic )) ĊĠĠĠĠĠĠĠ Ġblocking Application Logic ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġapplication State , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġkafka Consumer P ap ir Sm Register ing , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġdatabase , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġopp g ave Client ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ} Ċ } Ċ Ċ @ K tor Experimental API Ċ suspend Ġfun Ġblocking Application Logic ( ĊĠĠĠ Ġapplication State : ĠApplication State , ĊĠĠĠ Ġkafka Consumer : ĠKafka Consumer < String , ĠString >, ĊĠĠĠ Ġdatabase : ĠDatabase , ĊĠĠĠ Ġopp g ave Client : ĠO pp g ave Client Ċ ) Ġ{ ĊĠĠĠ Ġwhile Ġ( application State . ready ) Ġ{ ĊĠĠĠĠĠĠĠ Ġkafka Consumer . poll ( Duration . of Millis ( 0 )). forEach Ġ{ Ġconsumer Record Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġreceived P ap ir Sm Register ing : ĠP ap ir Sm Register ing Ġ= ĠobjectMapper . readValue ( consumer Record . value ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġlogging Meta Ġ= ĠLogging Meta ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġmot tak Id Ġ= Ġreceived P ap ir Sm Register ing . sy k meld ing Id , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġdokument Info Id Ġ= Ġreceived P ap ir Sm Register ing . d okument Info Id , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġmsg Id Ġ= Ġreceived P ap ir Sm Register ing . sy k meld ing Id , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġsy k meld ing Id Ġ= Ġreceived P ap ir Sm Register ing . sy k meld ing Id , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġjournal post Id Ġ= Ġreceived P ap ir Sm Register ing . journal post Id ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġhandle Rec ived Message ( received P ap ir Sm Register ing , Ġdatabase , Ġopp g ave Client , Ġlogging Meta ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġdelay ( 1 0 0 ) ĊĠĠĠ Ġ} Ċ } Ċ