Skip to content

Commit bdac1a5

Browse files
authored
Merge pull request #22 from delphi-hub/newApiImplementation
New api implementation
2 parents 2b1f339 + 40e70ac commit bdac1a5

File tree

13 files changed

+422
-78
lines changed

13 files changed

+422
-78
lines changed

app/EagerLoaderModule.scala

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
// Copyright (C) 2018 The Delphi Team.
2+
// See the LICENCE file distributed with this work for additional
3+
// information regarding copyright ownership.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// 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.
116
package app
217
import com.google.inject.AbstractModule
318
import services.StartUpService
@@ -6,7 +21,7 @@ import services.StartUpService
621
* Run functions during request
722
*/
823
class EagerLoaderModule extends AbstractModule {
9-
override def configure() = {
24+
override def configure() : Unit = {
1025
//startupservice will run during request
1126
bind(classOf[StartUpService]).asEagerSingleton
1227
}

app/controllers/HomeController.scala

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1+
// Copyright (C) 2018 The Delphi Team.
2+
// See the LICENCE file distributed with this work for additional
3+
// information regarding copyright ownership.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// 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.
116
package controllers
217

318
import javax.inject._
419
import play.api.Configuration
520
import play.api.mvc._
621
import utils.BlockingHttpClient
22+
import utils.CommonHelper
723

824
import scala.concurrent.Future
925
import scala.util.{Failure, Success}
@@ -21,7 +37,7 @@ class HomeController @Inject()(configuration: Configuration, cc: ControllerCompo
2137
* will be called when the application receives a `GET` request with
2238
* a path of `/`.
2339
*/
24-
def index = Action {
40+
def index : Action[AnyContent] = Action {
2541
Ok(views.html.index("", "", false))
2642
}
2743

@@ -32,11 +48,11 @@ class HomeController @Inject()(configuration: Configuration, cc: ControllerCompo
3248
*/
3349
def query(query : String) : Action[AnyContent] = Action.async {
3450
implicit request => {
35-
val server = configuration.underlying.getString("webapi.path")
36-
val getRequest = BlockingHttpClient.executeGet("search/"+query, server)
51+
val server = CommonHelper.addHttpProtocolIfNotExist(CommonHelper.configuration.webApiUri)
52+
val getRequest = BlockingHttpClient.executeGet("/search/" + query, server)
3753
getRequest match {
3854
case Success(response) => Future.successful(Ok(views.html.index(response, query, false)))
39-
case Failure(_) => Future.successful(Ok(views.html.index("ERROR: Failed to reach server at "+server, query, true)))
55+
case Failure(_) => Future.successful(Ok(views.html.index("ERROR: Failed to reach server at " + server, query, true)))
4056
}
4157
}
4258
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (C) 2018 The Delphi Team.
2+
// See the LICENCE file distributed with this work for additional
3+
// information regarding copyright ownership.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// 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+
package controllers
17+
18+
import akka.actor.ActorSystem
19+
import de.upb.cs.swt.delphi.webapp.BuildInfo
20+
import javax.inject.Inject
21+
import play.api.mvc.{Action, AnyContent, BaseController, ControllerComponents}
22+
import utils.AppLogging
23+
import scala.concurrent.{ExecutionContext, Future}
24+
25+
class SettingsController @Inject()(val controllerComponents: ControllerComponents) extends BaseController with AppLogging{
26+
implicit val system: ActorSystem = ActorSystem()
27+
implicit val ec : ExecutionContext = system.dispatcher
28+
private val threadSleepTime:Int = 3000 // 3 second
29+
30+
31+
//show the version of webapp service
32+
def version: Action[AnyContent] = Action { implicit request =>
33+
val version = Ok(BuildInfo.version)
34+
version
35+
}
36+
37+
//shutdown hook for webapp shudown
38+
def shutDownHook: Unit = {
39+
log.info("Webapp Stopped Successfully")
40+
}
41+
}

app/services/StartUpService.scala

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
1+
// Copyright (C) 2018 The Delphi Team.
2+
// See the LICENCE file distributed with this work for additional
3+
// information regarding copyright ownership.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// 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.
116
package services
217

318
import java.util.concurrent.TimeUnit
419

520
import javax.inject.{Singleton, _}
621
import play.api.inject.ApplicationLifecycle
7-
import utils.Configuration
22+
import utils.{CommonHelper, Configuration}
823
import utils.instancemanagement.InstanceRegistry
924

1025
import scala.concurrent.duration.Duration
@@ -17,28 +32,27 @@ import scala.util.{Failure, Success}
1732
@Singleton
1833
class StartUpService @Inject()(appLifecycle: ApplicationLifecycle){
1934

20-
private val configuration = new Configuration()
2135

2236
/**
2337
* Will register at the Instance Registry, get an matching WebApi instance and try to connect to it using the
2438
* /version endpoint. If successful, it will post the matching result true to the IR, otherwise false.
2539
*/
2640
def doStartUpChecks(): Unit = {
41+
42+
val configuration = CommonHelper.configuration
43+
2744
InstanceRegistry.getWebApiVersion(configuration) match {
28-
case Success(_) => {
45+
case Success(_) =>
2946
InstanceRegistry.sendWebApiMatchingResult(true, configuration)
30-
}
31-
case Failure(_) => {
47+
case Failure(_) =>
3248
InstanceRegistry.sendWebApiMatchingResult(false, configuration)
33-
//Cannot connect to WebApi on startup, so stop execution
34-
Await.ready(appLifecycle.stop(), Duration(5, TimeUnit.SECONDS))
35-
System.exit(1)
36-
}
49+
InstanceRegistry.handleInstanceFailure(configuration)
50+
//Keep instance running, but webapi won't be reachable
3751
}
3852
}
39-
53+
4054
appLifecycle.addStopHook { () =>
41-
InstanceRegistry.deregister(configuration)
55+
InstanceRegistry.handleInstanceStop(CommonHelper.configuration)
4256
Future.successful(())
4357
}
4458

app/utils/AppLogging.scala

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
1+
// Copyright (C) 2018 The Delphi Team.
2+
// See the LICENCE file distributed with this work for additional
3+
// information regarding copyright ownership.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// 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.
116
package utils
217

318
import akka.actor.{ActorSystem, ExtendedActorSystem}
419
import akka.event.{BusLogging, LoggingAdapter}
520

621
trait AppLogging {
7-
def log(implicit system: ActorSystem): LoggingAdapter = new BusLogging(system.eventStream, this.getClass.getName, this.getClass, system.asInstanceOf[ExtendedActorSystem].logFilter)
22+
def log(implicit system: ActorSystem): LoggingAdapter =
23+
new BusLogging(system.eventStream, this.getClass.getName, this.getClass, system.asInstanceOf[ExtendedActorSystem].logFilter)
824
}

app/utils/BlockingHttpClient.scala

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
// Copyright (C) 2018 The Delphi Team.
2+
// See the LICENCE file distributed with this work for additional
3+
// information regarding copyright ownership.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// 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.
116
package utils
217

318

@@ -7,7 +22,7 @@ import akka.http.scaladsl.model.{HttpEntity, HttpMethods, HttpRequest, HttpRespo
722
import akka.stream.{ActorMaterializer, ActorMaterializerSettings}
823
import akka.util.ByteString
924

10-
import scala.concurrent.{Await, Future}
25+
import scala.concurrent.{Await, ExecutionContext, Future}
1126
import scala.concurrent.duration.Duration
1227
import scala.util.{Failure, Success, Try}
1328
import MediaTypes._
@@ -19,8 +34,8 @@ import MediaTypes._
1934
object BlockingHttpClient {
2035

2136
def doGet(uri : Uri) : Try[String] = {
22-
implicit val system = ActorSystem()
23-
implicit val executionContext = system.dispatcher
37+
implicit val system: ActorSystem = ActorSystem()
38+
implicit val executionContext: ExecutionContext = system.dispatcher
2439
implicit val materializer: ActorMaterializer = ActorMaterializer(ActorMaterializerSettings(system))
2540

2641
try {
@@ -41,9 +56,9 @@ object BlockingHttpClient {
4156
}
4257

4358
// data parameter will be """{"name":"Hello"}"""
44-
def doPost(uri: Uri, data: String) = {
45-
implicit val system = ActorSystem()
46-
implicit val executionContext = system.dispatcher
59+
def doPost(uri: Uri, data: String) : Try[String] = {
60+
implicit val system: ActorSystem = ActorSystem()
61+
implicit val executionContext: ExecutionContext = system.dispatcher
4762
implicit val materializer: ActorMaterializer = ActorMaterializer(ActorMaterializerSettings(system))
4863
val bdata = ByteString(data)
4964
try {

app/utils/CommonHelper.scala

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (C) 2018 The Delphi Team.
2+
// See the LICENCE file distributed with this work for additional
3+
// information regarding copyright ownership.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// 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+
package utils
17+
18+
object CommonHelper {
19+
20+
val configuration: Configuration = new Configuration()
21+
22+
def addHttpProtocolIfNotExist(url: String): String = {
23+
val hasProtocol = url.startsWith("http://") || url.startsWith("https://")
24+
if(! hasProtocol) {
25+
"http://" + url //Default protocol is http
26+
} else {
27+
url
28+
}
29+
}
30+
}

app/utils/Configuration.scala

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
1+
// Copyright (C) 2018 The Delphi Team.
2+
// See the LICENCE file distributed with this work for additional
3+
// information regarding copyright ownership.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// 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.
116
package utils
217

318
import com.typesafe.config.ConfigFactory
4-
import utils.instancemanagement.InstanceEnums.ComponentType
19+
import utils.instancemanagement.InstanceEnums.{ComponentType, InstanceState}
520
import utils.instancemanagement.{Instance, InstanceRegistry}
621

722
import scala.util.{Failure, Success, Try}
@@ -11,7 +26,7 @@ class Configuration(val bindPort: Int = ConfigFactory.load().getInt("app.portWeb
1126
val defaultWebApiPort : Int = ConfigFactory.load().getInt("webapi.port")
1227
val defaultWebApiHost : String = ConfigFactory.load().getString("webapi.host")
1328
val instanceName = "WebAppInstance"
14-
val instanceRegistryUri : String = sys.env.getOrElse("DELPHI_WEBAPI_URI", ConfigFactory.load().getString("instance.registry.path"))
29+
val instanceRegistryUri : String = sys.env.getOrElse("DELPHI_IR_URI", ConfigFactory.load().getString("instance.registry.path"))
1530

1631
lazy val webApiUri:String = webApiInstance.host + ":" + webApiInstance.portNumber
1732

@@ -22,18 +37,19 @@ class Configuration(val bindPort: Int = ConfigFactory.load().getInt("app.portWeb
2237
fallbackWebApiHost,
2338
fallbackWebApiPort,
2439
"Default WebApi instance",
25-
ComponentType.WebApi)
40+
ComponentType.WebApi,
41+
None,
42+
InstanceState.Running,
43+
List.empty[String]
44+
)
2645

2746
}
2847

2948
lazy val usingInstanceRegistry : Boolean = assignedID match {
3049
case Some(_) => true
3150
case None => false
3251
}
33-
lazy val assignedID : Option[Long] = InstanceRegistry.register(this) match {
34-
case Success(id) => Some(id)
35-
case Failure(_) => None
36-
}
52+
lazy val assignedID : Option[Long] = InstanceRegistry.handleInstanceStart(this)
3753

3854
lazy val fallbackWebApiPort : Int = sys.env.get("DELPHI_WEBAPI_URI") match {
3955
case Some(hostString) => if(hostString.count(c => c == ':') == 2){

app/utils/JsonSupport.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
// Copyright (C) 2018 The Delphi Team.
2+
// See the LICENCE file distributed with this work for additional
3+
// information regarding copyright ownership.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// 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.
116
package utils
217

318
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport

0 commit comments

Comments
 (0)