Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//
// 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
You can’t perform that action at this time.