Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prototype typesafe config #8

Merged
merged 1 commit into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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