-
-
Save ndchandar/15a81f904fbf21b2149346c6d120fab7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Sample code snipppet to describe lack of dryness in my current approach | |
// Goal is to make `correlationId` available as a context entry for all the routes | |
// | |
// uuid generator | |
object IdGenerator { | |
def newId: String = UUID.randomUUID.toString | |
} | |
// create correlationKey | |
object CorrelationIdCodec { | |
val correlationKey = Context.key[String]("correlationId", null) | |
} | |
class MyService { | |
// entity 1 | |
def createEntity(js: Json): Future[Option[Js] = ??? | |
def getEntity(id: String): Future[Option[Js] = ??? | |
// entity 2 | |
def createEntity2(js: Json): Future[Option[Js] = ??? | |
def getEntity2(id: String): Future[Option[Js] = ??? | |
} | |
object MyService { | |
def myService = new MyService() | |
} | |
import MyService.myService | |
// sample routes | |
val someCrudApi: Route = | |
(pathPrefix("path1" / "path2")) { | |
(post & entity(as[Json]) ) { js => | |
Kamon.runWithContextEntry(CorrelationIdCodec.correlationKey, IdGenerator.newId) { | |
onSuccess(myService.createEntity(js)) | |
} | |
} ~ | |
(get & path(Segment)) { attribute1 => | |
Kamon.runWithContextEntry(CorrelationIdCodec.correlationKey, IdGenerator.newId) { | |
onSuccess(myService.getEntity(attribute1)) | |
} | |
} // end `pathPrefix` | |
val someCrudApi2: Route = | |
(pathPrefix("path3" / "path4") & myCustomAuthDirective) { | |
(post & entity(as[Json]) ) { js => | |
Kamon.runWithContextEntry(CorrelationIdCodec.correlationKey, IdGenerator.newId) { | |
onSuccess(myService.createEntity2(js)) | |
} | |
} ~ | |
(get & path(Segment)) { attribute1 => | |
Kamon.runWithContextEntry(CorrelationIdCodec.correlationKey, IdGenerator.newId) { | |
onSuccess(myService.getEntity2(attribute1)) | |
} | |
} // end `pathPrefix` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment