I've created an REST api in scala using AKKA-HTTP
, spray-json
and Slick
. For Authorization of route, I've used oauth2
.
DAO to retrieve Data(Using Plain SQL):
def getAllNotes: Future[Seq[UserEntity]] = {
implicit val getUserResult = GetResult(r => UserEntity(r.<<, r.<<, r.<<, r.<<, r.<<, r.<<))
query(s"select id, email, password,created_at, updated_at, deleted_at from users", getUserResult)
}
DAO to retrieve Data(Slick Table):
def getAll(): Future[Seq[A]] = {
db.run(tableQ.result)
}
Here's the part of routing:
val route: Route = pathPrefix("auth") {
get {
path("tests") {
complete(userDao.getAll.map(u => u.toList))
} ~
path("test") {
complete(userDao.getAllNotes.map(u => u.toList))
} ~
path("testUsers") {
baseApi(userDao.getAllNotes)
} ~
path("users") {
baseApi(userDao.getAll())
}
}
}
implicit def baseApi(f: ToResponseMarshallable): Route = {
authenticateOAuth2Async[AuthInfo[OauthAccount]]("realm", oauth2Authenticator) { auth =>
pathEndOrSingleSlash {
complete(f)
}
}
}
Functionally, all routes are working as intended but the performance seems to be degrading when OAUTH2 and Slick Tables are used for getting data.
The respective results of above routes:
1. "users" => 10 request per second: OAUTH2: YES, Slick Table: YES
2. "testUsers" => 17 request per second: OAUTH2: YES, Slick Table: NO
3. "tests" => 500 request per second: OAUTH2: NO, Slick Table: YES
4. "test" => 5593 request per second: OAUTH2: NO, Slick Table: NO
My Problem
How can I optimize the REST request using OAUTH2 and Slick Table?
Would it be good practice if I used PLAIN SQL instead of Slick tables and Joins in all cases?