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'
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in new issue