diff options
| author | nasr <nsrddyn@gmail.com> | 2025-12-01 18:35:51 +0100 |
|---|---|---|
| committer | nasr <nsrddyn@gmail.com> | 2025-12-01 18:35:51 +0100 |
| commit | 2ec866b4787da9ad055b3582760764cc55918564 (patch) | |
| tree | 226a426fdd9a6fdb67e4cd07955778689227573b /src/main | |
| parent | 460ecda3f1a1ca47ff99a8b196b836ddc3112ad4 (diff) | |
feature: implement HTTP handler and update resource methods for API routes
Diffstat (limited to 'src/main')
| -rw-r--r-- | src/main/scala/main/Main.scala | 7 | ||||
| -rw-r--r-- | src/main/scala/main/infrastructure/Http.scala | 11 | ||||
| -rw-r--r-- | src/main/scala/main/infrastructure/Resources.scala | 2 | ||||
| -rw-r--r-- | src/main/scala/main/infrastructure/Routes.scala | 28 | ||||
| -rw-r--r-- | src/main/scala/main/infrastructure/routes/Routes.scala | 21 |
5 files changed, 36 insertions, 33 deletions
diff --git a/src/main/scala/main/Main.scala b/src/main/scala/main/Main.scala index b22e0f7..388daa9 100644 --- a/src/main/scala/main/Main.scala +++ b/src/main/scala/main/Main.scala @@ -25,6 +25,7 @@ object Torque extends ZIOAppDefault with Runner { val v: View = new View v.serveView + httpHandler } @@ -33,10 +34,4 @@ object Torque extends ZIOAppDefault with Runner { * Which allows us to post the gathered data to the API * */ - def runHttp: Unit = { - - - - } - } diff --git a/src/main/scala/main/infrastructure/Http.scala b/src/main/scala/main/infrastructure/Http.scala index 01103d7..0317655 100644 --- a/src/main/scala/main/infrastructure/Http.scala +++ b/src/main/scala/main/infrastructure/Http.scala @@ -12,10 +12,11 @@ import main.domain._ * * source: https://ziohttp.com */ - def httpHandler(cpu: CpuInfo, ram: RamInfo, platform: PlatformInfo): Unit = { - - Server.serve(routes) - .provide(Server.default) - .exitCode + def httpHandler: ZIO[Any, Throwable, Unit] = { + ZIO.attempt { + Server.serve(routes) + .provide(Server.default) + .exitCode + } } diff --git a/src/main/scala/main/infrastructure/Resources.scala b/src/main/scala/main/infrastructure/Resources.scala index c32d9e3..fc3b08f 100644 --- a/src/main/scala/main/infrastructure/Resources.scala +++ b/src/main/scala/main/infrastructure/Resources.scala @@ -20,7 +20,7 @@ class Resources { /** * Platform Mehtods * */ - def getPlatform: Task[PlatformInfo] = { + def getPlatformInfo: Task[PlatformInfo] = { ZIO.attempt { /** * Pass the OS version to the DTO to later transfer it over the API diff --git a/src/main/scala/main/infrastructure/Routes.scala b/src/main/scala/main/infrastructure/Routes.scala new file mode 100644 index 0000000..74055ce --- /dev/null +++ b/src/main/scala/main/infrastructure/Routes.scala @@ -0,0 +1,28 @@ +package main.infrastructure + +import zio._ +import zio.http._ +import main.domain._ + +/** + * + * API routes to show the results of a stress test + * + * */ +val routes = Routes ( + + // the zio catch was needed for some reason + Method.GET / "cpu" -> handler{ + val resources = new Resources() + resources.getCpuInfo.map(info => Response.text(info.toString)) + .catchAll(err => ZIO.succeed(Response.internalServerError("cpu route failed")))}, + Method.GET / "ram" -> handler{ + val resources = new Resources() + resources.getRamInfo.map(info => Response.text(info.toString)) + .catchAll(err => ZIO.succeed(Response.internalServerError("ram route failed")))}, + Method.GET / "platform" -> handler{ + val resources = new Resources() + resources.getPlatformInfo.map(info => Response.text(info.toString)) + .catchAll(err => ZIO.succeed(Response.internalServerError("platform route failed")))}, +) + diff --git a/src/main/scala/main/infrastructure/routes/Routes.scala b/src/main/scala/main/infrastructure/routes/Routes.scala deleted file mode 100644 index a021ffd..0000000 --- a/src/main/scala/main/infrastructure/routes/Routes.scala +++ /dev/null @@ -1,21 +0,0 @@ -package main.infrastructure - -import zio._ -import zio.http._ -import main.domain._ - -/** - * - * API routes to show the results of a stress test - * - * */ - -val routes = Routes ( - - Method.GET / Root -> handler(Response.text("Greetings at your services")), - Method.GET / "cpu" -> handler(Response.text("Greetings at your services")), - Method.GET / "ram" -> handler(Response.text("Greetings at your services")), - Method.GET / "platform" -> handler(Response.text("Greetings at your services")) - -) - |
