package Ġapp . api Ċ Ċ import Ġapp . Json Mapper Ċ import Ġapp . dto . to File Response Ċ import Ġcom . fasterxml . jackson . core . PrettyPrinter Ċ import Ġdev . v ish na . watch service . K Watch Event Ċ import Ġio . ktor . application . Application Call Ċ import Ġio . ktor . http . Cache Control Ċ import Ġio . ktor . http . ContentType Ċ import Ġio . ktor . response . cache Control Ċ import Ġio . ktor . response . respond Text Writer Ċ import Ġkotlinx . coroutines . Experimental Cor outines Api Ċ import Ġkotlinx . coroutines . channels . Receive Channel Ċ import Ġkotlinx . coroutines . channels . consume Each Ċ import Ġorg . slf 4 j . LoggerFactory Ċ Ċ private Ġval Ġlog Ġ= ĠLoggerFactory . getLogger (" File Event Responder ") Ċ private Ġval Ġsingle Line Json Writer Ġ= ĠJson Mapper . mapper . writer ( null Ġas ĠPretty Printer ?) Ċ Ċ @ Experimental Cor outines Api Ċ suspend Ġfun ĠApplication Call . respond K Watch Event ( k Watch Events : ĠReceive Channel < K Watch Event >) Ġ{ ĊĊĠĠĠ Ġresponse . cache Control ( Cache Control . No Cache ( null )) ĊĊĠĠĠ Ġrespond Text Writer ( ContentType . Text . Event Stream ) Ġ{ ĊĊĠĠĠĠĠĠĠ Ġk Watch Events . consume Each Ġ{ Ġevent Ġ-> ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġlog . debug (" Cons umed Ġfile Ġevent Ġ${ event . kind } Ġfor Ġ${ event . file . path }") ĊĊĠĠĠĠĠĠĠĠĠĠĠ Ġif Ġ( event . kind Ġ!== ĠK Watch Event . Kind . Initialized ) Ġ{ ĊĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġif Ġ( event . kind Ġ=== ĠK Watch Event . Kind . Deleted ) Ġ{ ĊĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġwrite (" data : Ġ${ event . file . path }\ n ") ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġval Ġfile Response Ġ= Ġevent . file . to File Response () ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġwrite (" data : Ġ${ single Line Json Writer . writeValue AsString ( file Response )}\ n ") ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠĠĠĠĠ Ġwrite (" event : Ġ${ event . kind }\ n ") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġwrite ("\ n ") ĊĠĠĠĠĠĠĠĠĠĠĠ Ġflush () ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ċ } Ċ