Skip to content

New api implementation #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Nov 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
3230819
Added heder information and settings controller
sami-cseseu Sep 26, 2018
ee98b61
added shutdown hook
sami-cseseu Oct 1, 2018
5d2279c
shutting down webapp with a response
sami-cseseu Oct 2, 2018
3a3a920
fixed multiple header file issue
sami-cseseu Oct 2, 2018
cc61e91
Added updates to according to the new registry
sami-cseseu Oct 2, 2018
20cb169
Instance registry problem fixed
sami-cseseu Oct 11, 2018
f59ebbf
Code refectoring and startup service modification
sami-cseseu Oct 11, 2018
aceaff1
Fixed the issue with the failure call
sami-cseseu Oct 13, 2018
21d80b2
Naming problem of the variable solved
sami-cseseu Oct 16, 2018
277ad36
Webapi instance connection problem fixed
sami-cseseu Oct 18, 2018
094a7db
Fixed wrong env var name for IR uri.
Oct 25, 2018
1689662
Fixed a bug that made webapp register twice
Oct 25, 2018
759a5d9
Webapp registry issue without docker fixed
sami-cseseu Oct 25, 2018
e1b43b9
Webapp registry issue without docker fixed
sami-cseseu Oct 25, 2018
b025bce
Test file for SettingController
sami-cseseu Oct 25, 2018
de978ce
Removed unnecessary library file
sami-cseseu Oct 25, 2018
72f92d8
Removing test file as it is not working yet and should be developed i…
Nov 4, 2018
f8647d4
Changed codestyle config to require the same header as for the other …
Nov 4, 2018
cb8249e
Codestyle: Fixed issues with file header (hopefully...)
Nov 4, 2018
a49908d
Fixed minor mistakes (extra comma, protocol check not considering https)
Nov 8, 2018
8598cf6
Implemented new matching interface
Nov 8, 2018
91a49db
Removed the stop endpoint.
Nov 8, 2018
40e70ac
Fixed protocol check using 'contains' instead of 'startsWith'.
Nov 10, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion app/EagerLoaderModule.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
// Copyright (C) 2018 The Delphi Team.
// See the LICENCE file distributed with this work for additional
// information regarding copyright ownership.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package app
import com.google.inject.AbstractModule
import services.StartUpService
Expand All @@ -6,7 +21,7 @@ import services.StartUpService
* Run functions during request
*/
class EagerLoaderModule extends AbstractModule {
override def configure() = {
override def configure() : Unit = {
//startupservice will run during request
bind(classOf[StartUpService]).asEagerSingleton
}
Expand Down
24 changes: 20 additions & 4 deletions app/controllers/HomeController.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
// Copyright (C) 2018 The Delphi Team.
// See the LICENCE file distributed with this work for additional
// information regarding copyright ownership.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package controllers

import javax.inject._
import play.api.Configuration
import play.api.mvc._
import utils.BlockingHttpClient
import utils.CommonHelper

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

Expand All @@ -32,11 +48,11 @@ class HomeController @Inject()(configuration: Configuration, cc: ControllerCompo
*/
def query(query : String) : Action[AnyContent] = Action.async {
implicit request => {
val server = configuration.underlying.getString("webapi.path")
val getRequest = BlockingHttpClient.executeGet("search/"+query, server)
val server = CommonHelper.addHttpProtocolIfNotExist(CommonHelper.configuration.webApiUri)
val getRequest = BlockingHttpClient.executeGet("/search/" + query, server)
getRequest match {
case Success(response) => Future.successful(Ok(views.html.index(response, query, false)))
case Failure(_) => Future.successful(Ok(views.html.index("ERROR: Failed to reach server at "+server, query, true)))
case Failure(_) => Future.successful(Ok(views.html.index("ERROR: Failed to reach server at " + server, query, true)))
}
}
}
Expand Down
41 changes: 41 additions & 0 deletions app/controllers/SettingsController.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (C) 2018 The Delphi Team.
// See the LICENCE file distributed with this work for additional
// information regarding copyright ownership.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package controllers

import akka.actor.ActorSystem
import de.upb.cs.swt.delphi.webapp.BuildInfo
import javax.inject.Inject
import play.api.mvc.{Action, AnyContent, BaseController, ControllerComponents}
import utils.AppLogging
import scala.concurrent.{ExecutionContext, Future}

class SettingsController @Inject()(val controllerComponents: ControllerComponents) extends BaseController with AppLogging{
implicit val system: ActorSystem = ActorSystem()
implicit val ec : ExecutionContext = system.dispatcher
private val threadSleepTime:Int = 3000 // 3 second


//show the version of webapp service
def version: Action[AnyContent] = Action { implicit request =>
val version = Ok(BuildInfo.version)
version
}

//shutdown hook for webapp shudown
def shutDownHook: Unit = {
log.info("Webapp Stopped Successfully")
}
}
36 changes: 25 additions & 11 deletions app/services/StartUpService.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
// Copyright (C) 2018 The Delphi Team.
// See the LICENCE file distributed with this work for additional
// information regarding copyright ownership.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package services

import java.util.concurrent.TimeUnit

import javax.inject.{Singleton, _}
import play.api.inject.ApplicationLifecycle
import utils.Configuration
import utils.{CommonHelper, Configuration}
import utils.instancemanagement.InstanceRegistry

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

private val configuration = new Configuration()

/**
* Will register at the Instance Registry, get an matching WebApi instance and try to connect to it using the
* /version endpoint. If successful, it will post the matching result true to the IR, otherwise false.
*/
def doStartUpChecks(): Unit = {

val configuration = CommonHelper.configuration

InstanceRegistry.getWebApiVersion(configuration) match {
case Success(_) => {
case Success(_) =>
InstanceRegistry.sendWebApiMatchingResult(true, configuration)
}
case Failure(_) => {
case Failure(_) =>
InstanceRegistry.sendWebApiMatchingResult(false, configuration)
//Cannot connect to WebApi on startup, so stop execution
Await.ready(appLifecycle.stop(), Duration(5, TimeUnit.SECONDS))
System.exit(1)
}
InstanceRegistry.handleInstanceFailure(configuration)
//Keep instance running, but webapi won't be reachable
}
}

appLifecycle.addStopHook { () =>
InstanceRegistry.deregister(configuration)
InstanceRegistry.handleInstanceStop(CommonHelper.configuration)
Future.successful(())
}

Expand Down
18 changes: 17 additions & 1 deletion app/utils/AppLogging.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
// Copyright (C) 2018 The Delphi Team.
// See the LICENCE file distributed with this work for additional
// information regarding copyright ownership.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package utils

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

trait AppLogging {
def log(implicit system: ActorSystem): LoggingAdapter = new BusLogging(system.eventStream, this.getClass.getName, this.getClass, system.asInstanceOf[ExtendedActorSystem].logFilter)
def log(implicit system: ActorSystem): LoggingAdapter =
new BusLogging(system.eventStream, this.getClass.getName, this.getClass, system.asInstanceOf[ExtendedActorSystem].logFilter)
}
27 changes: 21 additions & 6 deletions app/utils/BlockingHttpClient.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
// Copyright (C) 2018 The Delphi Team.
// See the LICENCE file distributed with this work for additional
// information regarding copyright ownership.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package utils


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

import scala.concurrent.{Await, Future}
import scala.concurrent.{Await, ExecutionContext, Future}
import scala.concurrent.duration.Duration
import scala.util.{Failure, Success, Try}
import MediaTypes._
Expand All @@ -19,8 +34,8 @@ import MediaTypes._
object BlockingHttpClient {

def doGet(uri : Uri) : Try[String] = {
implicit val system = ActorSystem()
implicit val executionContext = system.dispatcher
implicit val system: ActorSystem = ActorSystem()
implicit val executionContext: ExecutionContext = system.dispatcher
implicit val materializer: ActorMaterializer = ActorMaterializer(ActorMaterializerSettings(system))

try {
Expand All @@ -41,9 +56,9 @@ object BlockingHttpClient {
}

// data parameter will be """{"name":"Hello"}"""
def doPost(uri: Uri, data: String) = {
implicit val system = ActorSystem()
implicit val executionContext = system.dispatcher
def doPost(uri: Uri, data: String) : Try[String] = {
implicit val system: ActorSystem = ActorSystem()
implicit val executionContext: ExecutionContext = system.dispatcher
implicit val materializer: ActorMaterializer = ActorMaterializer(ActorMaterializerSettings(system))
val bdata = ByteString(data)
try {
Expand Down
30 changes: 30 additions & 0 deletions app/utils/CommonHelper.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (C) 2018 The Delphi Team.
// See the LICENCE file distributed with this work for additional
// information regarding copyright ownership.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package utils

object CommonHelper {

val configuration: Configuration = new Configuration()

def addHttpProtocolIfNotExist(url: String): String = {
val hasProtocol = url.startsWith("http://") || url.startsWith("https://")
if(! hasProtocol) {
"http://" + url //Default protocol is http
} else {
url
}
}
}
30 changes: 23 additions & 7 deletions app/utils/Configuration.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
// Copyright (C) 2018 The Delphi Team.
// See the LICENCE file distributed with this work for additional
// information regarding copyright ownership.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package utils

import com.typesafe.config.ConfigFactory
import utils.instancemanagement.InstanceEnums.ComponentType
import utils.instancemanagement.InstanceEnums.{ComponentType, InstanceState}
import utils.instancemanagement.{Instance, InstanceRegistry}

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

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

Expand All @@ -22,18 +37,19 @@ class Configuration(val bindPort: Int = ConfigFactory.load().getInt("app.portWeb
fallbackWebApiHost,
fallbackWebApiPort,
"Default WebApi instance",
ComponentType.WebApi)
ComponentType.WebApi,
None,
InstanceState.Running,
List.empty[String]
)

}

lazy val usingInstanceRegistry : Boolean = assignedID match {
case Some(_) => true
case None => false
}
lazy val assignedID : Option[Long] = InstanceRegistry.register(this) match {
case Success(id) => Some(id)
case Failure(_) => None
}
lazy val assignedID : Option[Long] = InstanceRegistry.handleInstanceStart(this)

lazy val fallbackWebApiPort : Int = sys.env.get("DELPHI_WEBAPI_URI") match {
case Some(hostString) => if(hostString.count(c => c == ':') == 2){
Expand Down
15 changes: 15 additions & 0 deletions app/utils/JsonSupport.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
// Copyright (C) 2018 The Delphi Team.
// See the LICENCE file distributed with this work for additional
// information regarding copyright ownership.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package utils

import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
Expand Down
Loading