mise en place structure globale et exemple de fonctionnement

production
Override-6 2 years ago
parent 86c1de71cd
commit caef1e2364

@ -1,11 +1,5 @@
group 'org.tbasket.api' group "org.tbasket.api"
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies { dependencies {
implementation 'com.typesafe.akka:akka-http_3:10.5.0-M1' implementation 'com.typesafe.akka:akka-http_2.13:10.5.0-M1'
} }

@ -0,0 +1,38 @@
package org.tbasket.api
import akka.actor.typed.ActorSystem
import akka.actor.typed.javadsl.Behaviors
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{ContentTypes, HttpEntity, HttpResponse}
import akka.http.scaladsl.server.Directives._
import scala.concurrent.ExecutionContextExecutor
class Endpoint(url: String, port: Int) {
implicit val system: ActorSystem[_] = ActorSystem(Behaviors.empty, "main")
implicit val ec : ExecutionContextExecutor = system.executionContext
private var count = 0
def countEntity = HttpEntity(ContentTypes.`application/json`, s"value: $count")
private val server = Http().newServerAt(url, port)
server.bind(concat(
path("counter") {
get {
println("got get request")
complete(countEntity)
}
},
path("counter") {
post {
println("got request")
count += 1
complete(HttpResponse(entity = countEntity))
}
}
))
}

@ -0,0 +1,7 @@
package org.tbasket.api.event
trait RequestHandler {
def handle()
}

@ -1,12 +1,5 @@
group "org.tbasket.api"
group 'org.tbasket.db'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies { dependencies {
implementation 'io.getquill:quill_2.12:3.2.0' implementation 'io.getquill:quill_2.12:3.2.0'
} }

@ -4,35 +4,48 @@ plugins {
id 'application' id 'application'
} }
println("Running build on java version ${System.getProperty("java.version")}.") final var scalaVersion = "2.13"
mainClassName = 'org.tbasket.Main' mainClassName = 'org.tbasket.Main'
group 'org.tbasket' group 'org.tbasket'
version '1.0-SNAPSHOT'
println("Running gradle on java version ${System.getProperty("java.version")}.")
repositories { repositories {
mavenCentral() mavenCentral()
} }
dependencies {
implementation project(':API')
implementation project(':DB')
implementation "io.circe:circe-core_$scalaVersion:0.15.0-M1"
}
test {
useJUnitPlatform()
}
run {
standardInput = System.in
}
allprojects { allprojects {
apply plugin: 'scala' apply plugin: 'scala'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies { dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1' testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
implementation 'org.scala-lang:scala-library:2.13.10' implementation "org.scala-lang:scala-library:$scalaVersion.10"
implementation 'com.typesafe.akka:akka-actor_3:2.7.0' implementation "com.typesafe.akka:akka-actor-typed_$scalaVersion:2.7.0"
} implementation "com.typesafe.akka:akka-stream-typed_$scalaVersion:2.7.0"
}
dependencies { }
implementation project(':API')
implementation project(':DB')
}
test {
useJUnitPlatform()
} }

@ -0,0 +1 @@
endpoint.url=localhost:48485

@ -0,0 +1,32 @@
package org.tbasket
import org.tbasket.api.Endpoint
import java.util.Properties
object EndpointSetup {
final val EndpointUrl = "endpoint.url"
final val EndpointUrlDefault = s"localhost:48485"
def setupEndpoint(): Endpoint = {
println("Initializing API endpoint...")
val endpoint = createEndpoint()
endpoint
}
private def createEndpoint(): Endpoint = {
val properties = new Properties()
val in = getClass.getClassLoader.getResourceAsStream("server.properties")
properties.load(in)
val (url, port) = properties
.getProperty(EndpointUrl, EndpointUrlDefault) match {
case s"$ip:$port/$endpointPath" => (ip + "/" + endpointPath, port.toInt)
case s"$ip:$port" => (ip, port.toInt)
case v => throw new InternalBasketServerException(s"$EndpointUrl property value is wrong: $v must be <ip>:<port>/[endpointPath]")
}
new Endpoint(url, port)
}
}

@ -0,0 +1,8 @@
package org.tbasket
/**
* exception thrown when an error occurs due to an external fault from the server.
* */
class ExternalBasketServerException(msg: String, cause: Throwable = null) extends Exception(msg, cause) {
}

@ -0,0 +1,8 @@
package org.tbasket
/**
* exception thrown when an error occurs due to an internal fault of the server or its maintainers.
* */
class InternalBasketServerException(msg: String, cause: Throwable = null) extends Exception(msg, cause) {
}

@ -1,9 +1,15 @@
package org.tbasket package org.tbasket
import scala.io.StdIn
object Main { object Main {
def main(args: Array[String]): Unit = { def main(args: Array[String]): Unit = {
println("Hello Server !") val endpoint = EndpointSetup.setupEndpoint()
println("enter to exit")
StdIn.readLine()
} }
} }