/tools/src/main/scala/org/apache/predictionio/tools/admin/AdminAPI.scala

https://github.com/apache/incubator-predictionio · Scala · 129 lines · 95 code · 17 blank · 17 comment · 0 complexity · 01e0d5cb463bdb903de439799e82e6a1 MD5 · raw file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.predictionio.tools.admin
  18. import java.util.concurrent.TimeUnit
  19. import akka.http.scaladsl.server._
  20. import org.apache.predictionio.data.storage._
  21. import scala.concurrent.duration.Duration
  22. import scala.concurrent.{Await, ExecutionContext}
  23. import akka.actor.ActorSystem
  24. import akka.stream.ActorMaterializer
  25. import akka.http.scaladsl.Http
  26. import akka.http.scaladsl.model._
  27. import akka.http.scaladsl.server.Directives._
  28. import akka.util.Timeout
  29. import org.apache.predictionio.akkahttpjson4s.Json4sSupport._
  30. import org.json4s.{DefaultFormats, Formats}
  31. object Json4sProtocol {
  32. implicit val serialization = org.json4s.jackson.Serialization
  33. implicit def json4sFormats: Formats = DefaultFormats
  34. }
  35. case class AdminServerConfig(
  36. ip: String = "localhost",
  37. port: Int = 7071
  38. )
  39. object AdminServer {
  40. import Json4sProtocol._
  41. private implicit val timeout: Timeout = Timeout(5, TimeUnit.SECONDS)
  42. // for better message response
  43. private val rejectionHandler = RejectionHandler.newBuilder().handle {
  44. case MalformedRequestContentRejection(msg, _) =>
  45. complete(StatusCodes.BadRequest, Map("message" -> msg))
  46. case MissingQueryParamRejection(msg) =>
  47. complete(StatusCodes.NotFound,
  48. Map("message" -> s"missing required query parameter ${msg}."))
  49. case AuthenticationFailedRejection(cause, challengeHeaders) =>
  50. complete(StatusCodes.Unauthorized, challengeHeaders,
  51. Map("message" -> s"Invalid accessKey."))
  52. }.result()
  53. def createRoute()(implicit executionContext: ExecutionContext): Route = {
  54. val commandClient = new CommandClient(
  55. appClient = Storage.getMetaDataApps,
  56. accessKeyClient = Storage.getMetaDataAccessKeys,
  57. eventClient = Storage.getLEvents()
  58. )
  59. val route =
  60. pathSingleSlash {
  61. get {
  62. complete(Map("status" -> "alive"))
  63. }
  64. } ~
  65. path("cmd" / "app" / Segment / "data") {
  66. appName => {
  67. delete {
  68. complete(commandClient.futureAppDataDelete(appName))
  69. }
  70. }
  71. } ~
  72. path("cmd" / "app" / Segment) {
  73. appName => {
  74. delete {
  75. complete(commandClient.futureAppDelete(appName))
  76. }
  77. }
  78. } ~
  79. path("cmd" / "app") {
  80. get {
  81. complete(commandClient.futureAppList())
  82. } ~
  83. post {
  84. entity(as[AppRequest]) {
  85. appArgs =>
  86. onSuccess(commandClient.futureAppNew(appArgs)){
  87. case res: GeneralResponse => complete(res)
  88. case res: AppNewResponse => complete(res)
  89. }
  90. }
  91. }
  92. }
  93. route
  94. }
  95. def createAdminServer(config: AdminServerConfig): ActorSystem = {
  96. implicit val system = ActorSystem("AdminServerSystem")
  97. implicit val materializer = ActorMaterializer()
  98. implicit val executionContext = system.dispatcher
  99. val route = createRoute()
  100. Http().bindAndHandle(route, config.ip, config.port)
  101. system
  102. }
  103. }
  104. object AdminRun {
  105. def main (args: Array[String]) : Unit = {
  106. val f = AdminServer.createAdminServer(AdminServerConfig(
  107. ip = "localhost",
  108. port = 7071))
  109. .whenTerminated
  110. Await.ready(f, Duration.Inf)
  111. }
  112. }