Skip to content

Commit

Permalink
Prototype typesafe config
Browse files Browse the repository at this point in the history
  • Loading branch information
peri4n committed Jul 4, 2022
1 parent 13e577d commit 488cb4b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
5 changes: 5 additions & 0 deletions server/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,22 @@ lazy val server = (project in file("."))
scalaVersion := "3.1.0",
assembly / mainClass := Some("org.openschool.server.Main"),
libraryDependencies ++= Seq(
"com.typesafe" % "config" % "1.4.2",

"org.http4s" %% "http4s-ember-server" % Http4sVersion,
"org.http4s" %% "http4s-ember-client" % Http4sVersion,
"org.http4s" %% "http4s-circe" % Http4sVersion,
"io.circe" %% "circe-generic" % "0.14.1",

"org.http4s" %% "http4s-dsl" % Http4sVersion,
"com.softwaremill.sttp.tapir" %% "tapir-core" % TapirVersion,
"com.softwaremill.sttp.tapir" %% "tapir-json-circe" % TapirVersion,
"com.softwaremill.sttp.tapir" %% "tapir-http4s-server" % TapirVersion,
"com.softwaremill.sttp.tapir" %% "tapir-swagger-ui-bundle" % TapirVersion,

"org.scalameta" %% "munit" % MunitVersion % Test,
"org.typelevel" %% "munit-cats-effect-3" % MunitCatsEffectVersion % Test,

"ch.qos.logback" % "logback-classic" % LogbackVersion
),
testFrameworks += new TestFramework("munit.Framework")
Expand Down
1 change: 1 addition & 0 deletions server/src/main/resources/application.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
port = 8080
14 changes: 11 additions & 3 deletions server/src/main/scala/org/openschool/server/BackendServer.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.openschool.server

import cats.Applicative
import com.typesafe.config.Config
import com.typesafe.config.ConfigFactory
import cats.effect.{Async, Resource}
import cats.syntax.all._
import com.comcast.ip4s._
Expand All @@ -14,8 +17,13 @@ import org.http4s.Uri

object BackendServer:

def config[F[_]: Applicative]: Resource[F, Config] =
Resource.pure(ConfigFactory.load())

def stream[F[_]: Async]: Stream[F, Nothing] = {
for {
config <- Stream.resource(config[F])
port = config.getInt("port")
client <- Stream.resource(EmberClientBuilder.default[F].build)
helloWorldAlg = HelloWorld.impl[F]
systemInfoAlg = SystemInfo.impl[F]
Expand All @@ -25,8 +33,8 @@ object BackendServer:
corsOriginSettings = CORS.policy
.withAllowOriginHost(
Set(
Origin.Host(Uri.Scheme.https, Uri.RegName("localhost"), 8000.some),
Origin.Host(Uri.Scheme.http, Uri.RegName("localhost"), 8000.some)
Origin.Host(Uri.Scheme.https, Uri.RegName("localhost"), port.some),
Origin.Host(Uri.Scheme.http, Uri.RegName("localhost"), port.some)
)
)
.withAllowCredentials(false)
Expand All @@ -50,7 +58,7 @@ object BackendServer:
EmberServerBuilder
.default[F]
.withHost(ipv4"0.0.0.0")
.withPort(port"8080")
.withPort(Port.fromInt(port).get)
.withHttpApp(finalHttpApp)
.build >>
Resource.eval(Async[F].never)
Expand Down

0 comments on commit 488cb4b

Please sign in to comment.