Skip to content

Instantly share code, notes, and snippets.

@ndchandar
Last active September 20, 2019 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndchandar/15a81f904fbf21b2149346c6d120fab7 to your computer and use it in GitHub Desktop.
Save ndchandar/15a81f904fbf21b2149346c6d120fab7 to your computer and use it in GitHub Desktop.
//
// 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