From 17a6e7aa48c6561bddc0f105ec2cb835dfa6d8df Mon Sep 17 00:00:00 2001 From: Override-6 Date: Sun, 4 Dec 2022 05:48:52 +0100 Subject: [PATCH] endpoint modifications --- .drone.yml | 31 ------------------ .../main/scala/org/tbasket/api/Endpoint.scala | 4 +-- build.gradle | 2 +- drone/.drone.yml | 29 +++++++++++++++++ src/main/resources/server.properties | 2 +- .../scala/org/tbasket/EndpointSetup.scala | 18 +++++------ src/main/scala/org/tbasket/Main.scala | 32 ++++++++++++++++--- 7 files changed, 68 insertions(+), 50 deletions(-) delete mode 100644 .drone.yml create mode 100644 drone/.drone.yml diff --git a/.drone.yml b/.drone.yml deleted file mode 100644 index 80d7887..0000000 --- a/.drone.yml +++ /dev/null @@ -1,31 +0,0 @@ -kind: pipeline -type: docker -name: Deployment - -trigger: - branch: - - production - - dev - -steps: - - name: 'Unit Tests' - image: ubuntu:latest - commands: - - apt update && apt install openjdk-11-jdk -y - - ./gradlew :test - - - name: deploy to server - image: ubuntu:latest - depends_on: - - 'Unit Tests' - environment: - SSH_PRIVATE_KEY: - from_secret: ??? - SSH_PUBLIC_KEY: - from_secret: ??? - USER: - from_secret: ??? - IP: - from_secret: ??? - commands: - - drone/deliver.sh $DRONE_BRANCH diff --git a/API/src/main/scala/org/tbasket/api/Endpoint.scala b/API/src/main/scala/org/tbasket/api/Endpoint.scala index be27204..ef99857 100644 --- a/API/src/main/scala/org/tbasket/api/Endpoint.scala +++ b/API/src/main/scala/org/tbasket/api/Endpoint.scala @@ -11,7 +11,7 @@ import zio.http.model.Status import scala.collection.mutable -class Endpoint(hostname: String, port: Int) extends ZIOAppDefault { +class Endpoint(port: Int) extends ZIOAppDefault { private val handlers = mutable.HashMap.empty[String, APIRequestHandler] @@ -46,7 +46,7 @@ class Endpoint(hostname: String, port: Int) extends ZIOAppDefault { val configLayer = ServerConfig.live(config) Server.install(app).flatMap { port => - LOG.info(s"Listening API entries on $hostname:$port") + LOG.info(s"Listening API entries on $port") ZIO.never }.provide(configLayer, Server.live) } diff --git a/build.gradle b/build.gradle index d9bbb9b..0e2c3bb 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { id 'scala' id 'application' //for 'run' task id 'com.adarshr.test-logger' version '3.2.0' //fancy prints during tests - id 'com.github.johnrengelman.shadow' version '7.1.2' //for optimised jar + id 'com.github.johnrengelman.shadow' version '7.1.2' //for building optimised jar } final var scalaVersion = "2.13" diff --git a/drone/.drone.yml b/drone/.drone.yml new file mode 100644 index 0000000..768a49f --- /dev/null +++ b/drone/.drone.yml @@ -0,0 +1,29 @@ +kind: pipeline +type: docker +name: Deployment + +trigger: + branch: + - production + - dev + - drone-setup + +steps: + - name: Unit tests + image: amazoncorretto:11 + commands: + - echo + - ./gradlew :test + + - name: Deploy + image: amazoncorretto:11 + depends_on: + - 'Unit tests' + environment: + SSH_PRIVATE_KEY: + from_secret: SSH_PRIVATE + SSH_PUBLIC_KEY: + from_secret: SSH_PUBLIC + commands: + - chmod 777 drone/deliver.sh + - drone/deliver.sh $DRONE_BRANCH diff --git a/src/main/resources/server.properties b/src/main/resources/server.properties index 824af47..52a2da6 100644 --- a/src/main/resources/server.properties +++ b/src/main/resources/server.properties @@ -1 +1 @@ -endpoint.url=localhost:48485 \ No newline at end of file +endpoint.url=48485 \ No newline at end of file diff --git a/src/main/scala/org/tbasket/EndpointSetup.scala b/src/main/scala/org/tbasket/EndpointSetup.scala index 578cd5d..78ce397 100644 --- a/src/main/scala/org/tbasket/EndpointSetup.scala +++ b/src/main/scala/org/tbasket/EndpointSetup.scala @@ -10,23 +10,21 @@ object EndpointSetup { final val EndpointUrl = "endpoint.url" final val EndpointUrlDefault = s"localhost:48485" - def setupEndpoint(): Endpoint = { + def setupEndpoint(config: Properties): Endpoint = { Main.LOG.debug("Initializing API endpoint...") - val endpoint = createEndpoint() + val endpoint = createEndpoint(config) endpoint.bind("counter")(IncrementHandler) endpoint } - private def createEndpoint(): Endpoint = { - val properties = new Properties() - val in = getClass.getClassLoader.getResourceAsStream("server.properties") - properties.load(in) - val (hostname, port) = properties + private def createEndpoint(config: Properties): Endpoint = { + val port = config .getProperty(EndpointUrl, EndpointUrlDefault) match { - case s"$ip:$port" => (ip, port.toInt) - case v => throw new InternalBasketServerException(s"$EndpointUrl property value is wrong: $v must be :/[endpointPath]") + case s"$port" if port.toIntOption.isDefined => port.toInt + case v => + throw new InternalBasketServerException(s"$EndpointUrl property value is wrong: $v must be integer") } - new Endpoint(hostname, port) + new Endpoint(port) } } diff --git a/src/main/scala/org/tbasket/Main.scala b/src/main/scala/org/tbasket/Main.scala index 6977068..2486558 100644 --- a/src/main/scala/org/tbasket/Main.scala +++ b/src/main/scala/org/tbasket/Main.scala @@ -5,6 +5,8 @@ import org.apache.logging.log4j.LogManager import zio._ import java.lang +import java.nio.file.{Files, Path} +import java.util.Properties import scala.io.StdIn import scala.util.control.NonFatal @@ -13,8 +15,19 @@ object Main { def main(args: Array[String]): Unit = { LOG.info("Starting server") - val endpoint = EndpointSetup.setupEndpoint() - val runtime = Runtime.default + val config = retrieveConfig + db(config) + api(config) + LOG.info("Server successfully started") + } + + private def db(config: Properties): Unit = new Thread({ () => + }, "Database").start() + //TODO + + private def api(config: Properties): Unit = new Thread({ () => + val endpoint = EndpointSetup.setupEndpoint(config) + val runtime = Runtime.default Unsafe.unsafe { implicit u => runtime.unsafe.run(endpoint.run).catchSome { case NonFatal(e) => @@ -22,9 +35,18 @@ object Main { throw e } } - LOG.info("Server successfully started") - println("enter to exit") - StdIn.readLine() + }: Runnable, "API").start() + + private def retrieveConfig: Properties = { + val configFile = Path.of("server.properties") + if (Files.notExists(configFile)) { + val in = getClass.getResourceAsStream("/server.properties") + Files.writeString(configFile, new String(in.readAllBytes())) + } + val in = Files.newInputStream(configFile) + val properties = new Properties() + properties.load(in) + properties } //add a shutdown hook to log when the server is about to get killed