Skip to content

Commit 12b3e08

Browse files
committed
.
1 parent f3b7324 commit 12b3e08

File tree

5 files changed

+99
-63
lines changed

5 files changed

+99
-63
lines changed

project/src/live.server.scala

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -213,46 +213,6 @@ object LiveServer extends IOApp:
213213
.withHandler(minimumLevel = Some(Level.get(lvl).get))
214214
.replace()
215215

216-
val proxyConf2: Resource[IO, Option[Equilibrium]] = proxyTarget
217-
.zip(pathPrefix)
218-
.traverse {
219-
(pt, prfx) =>
220-
IO(
221-
Equilibrium(
222-
ProxyConfig.HttpProxyConfig(
223-
servers = NonEmptyList(
224-
Server(
225-
listen = port,
226-
serverNames = List("localhost"),
227-
locations = List(
228-
ProxyConfig.Location(
229-
matcher = LocationMatcher.Prefix(prfx),
230-
proxyPass = s"http://$$backend"
231-
)
232-
)
233-
),
234-
List()
235-
),
236-
upstreams = List(
237-
ProxyConfig.Upstream(
238-
name = "backend",
239-
servers = NonEmptyList(
240-
ProxyConfig.UpstreamServer(
241-
host = Host.fromString("localhost").get,
242-
port = pt,
243-
weight = 5
244-
),
245-
List()
246-
)
247-
)
248-
)
249-
)
250-
)
251-
)
252-
253-
}
254-
.toResource
255-
256216
val server = for
257217
_ <- logger
258218
.debug(
@@ -295,7 +255,8 @@ object LiveServer extends IOApp:
295255
CliValidationError("path-to-index-html and styles-dir can't be defined at the same time")
296256
)
297257

298-
proxyRoutes <- makeProxyRoutes(client, pathPrefix, proxyConf2)(logger)
258+
proxyConf2 <- proxyConf(proxyTarget, pathPrefix)
259+
proxyRoutes: HttpRoutes[IO] = makeProxyRoutes(client, proxyConf2)(logger)
299260

300261
_ <- buildRunner(
301262
buildTool,

project/src/proxy.http.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ object HttpProxy:
2929

3030
val routes: HttpRoutes[F] = HttpRoutes.of {
3131
case (req: Request[F]) =>
32+
println(req)
3233
val pathRendered = pathPrefix + req.uri.path.renderString
3334
val host = req.headers.get[Host].map(_.host).getOrElse("") // Host set otherwise empty string
3435
val newServers = servers.filter(_.serverNames.contains(host))

project/src/proxy.routes.scala

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,78 @@ import cats.effect.kernel.Resource
88
import cats.effect.std.Random
99

1010
import ProxyConfig.Equilibrium
11+
import org.http4s.server.Router
12+
import org.http4s.server.middleware.Logger
13+
import com.comcast.ip4s.Port
14+
import cats.data.NonEmptyList
15+
import ProxyConfig.Server
16+
import ProxyConfig.LocationMatcher
17+
import com.comcast.ip4s.Host
18+
import cats.syntax.all.*
1119

1220
def makeProxyRoutes(
1321
client: Client[IO],
14-
pathPrefix: Option[String],
15-
proxyConfig: Resource[IO, Option[Equilibrium]]
16-
)(logger: Scribe[IO]): Resource[IO, HttpRoutes[IO]] =
17-
proxyConfig.flatMap {
18-
case Some(pc) =>
19-
{
20-
given R: Random[IO] = Random.javaUtilConcurrentThreadLocalRandom[IO]
21-
logger.debug("setup proxy server") >>
22-
IO(HttpProxy.servers[IO](pc, client, pathPrefix.getOrElse(???)).head._2)
23-
}.toResource
22+
proxyConfig: Option[(Equilibrium, String)]
23+
)(logger: Scribe[IO]): HttpRoutes[IO] =
24+
proxyConfig match
25+
case Some((pc, pathPrefix)) =>
26+
given R: Random[IO] = Random.javaUtilConcurrentThreadLocalRandom[IO]
27+
Logger.httpRoutes[IO](
28+
logHeaders = true,
29+
logBody = true,
30+
redactHeadersWhen = _ => false,
31+
logAction = Some((msg: String) => logger.trace(msg))
32+
)(
33+
Router(
34+
pathPrefix -> HttpProxy.servers[IO](pc, client, pathPrefix).head._2
35+
)
36+
)
2437

2538
case None =>
26-
(
27-
logger.debug("no proxy set") >>
28-
IO(HttpRoutes.empty[IO])
29-
).toResource
30-
}
39+
HttpRoutes.empty[IO]
3140

3241
end makeProxyRoutes
42+
43+
def proxyConf(proxyTarget: Option[Port], pathPrefix: Option[String]): Resource[IO, Option[(Equilibrium, String)]] =
44+
proxyTarget
45+
.zip(pathPrefix)
46+
.traverse {
47+
(pt, prfx) =>
48+
IO(
49+
(
50+
Equilibrium(
51+
ProxyConfig.HttpProxyConfig(
52+
servers = NonEmptyList(
53+
Server(
54+
listen = pt,
55+
serverNames = List("localhost"),
56+
locations = List(
57+
ProxyConfig.Location(
58+
matcher = LocationMatcher.Prefix(prfx),
59+
proxyPass = s"http://$$backend"
60+
)
61+
)
62+
),
63+
List()
64+
),
65+
upstreams = List(
66+
ProxyConfig.Upstream(
67+
name = "backend",
68+
servers = NonEmptyList(
69+
ProxyConfig.UpstreamServer(
70+
host = Host.fromString("localhost").get,
71+
port = pt,
72+
weight = 5
73+
),
74+
List()
75+
)
76+
)
77+
)
78+
)
79+
),
80+
prfx
81+
)
82+
)
83+
84+
}
85+
.toResource

project/src/routes.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,9 @@ def routes[F[_]: Files: MonadThrow](
209209
val app = logMiddler(
210210
refreshRoutes
211211
.combineK(linkedAppWithCaching)
212+
.combineK(proxyRoutes)
212213
.combineK(clientSpaRoutes)
213214
.combineK(staticAssetRoutes)
214-
.combineK(proxyRoutes)
215215
)
216216

217217
clientRoutingPrefix.fold(IO.unit)(s => logger.trace(s"client spa at : $s")).toResource >>

project/test/src/liveServer.test.scala

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,24 @@ trait PlaywrightTest extends munit.FunSuite:
8484
os.remove.all(tempDir)
8585
)
8686

87+
val externalHtmlStyles = FunFixture[(os.Path, os.Path)](
88+
setup = test =>
89+
// create a temp folder
90+
val tempDir = os.temp.dir()
91+
val staticDir = tempDir / "assets"
92+
os.makeDir(staticDir)
93+
// create a file in the folder
94+
os.write.over(tempDir / "hello.scala", helloWorldCode("Hello"))
95+
os.write.over(staticDir / "index.less", "h1{color:red}")
96+
os.write.over(staticDir / "index.html", vanillaTemplate(true).render)
97+
os.proc("scala-cli", "compile", tempDir.toString).call(cwd = tempDir)
98+
(tempDir, staticDir)
99+
,
100+
teardown = tempDir =>
101+
// Always gets called, even if test failed.
102+
os.remove.all(tempDir._1)
103+
)
104+
87105
files.test("incremental") {
88106
testDir =>
89107
val thisTestPort = basePort + 1
@@ -147,7 +165,7 @@ trait PlaywrightTest extends munit.FunSuite:
147165
assertEquals(out.statusCode, 404)
148166
}
149167

150-
files.test("proxy server".only) {
168+
files.test("proxy server") {
151169
testDir =>
152170
val backendPort = 8089
153171
val thisTestPort = basePort + 3
@@ -196,7 +214,7 @@ trait PlaywrightTest extends munit.FunSuite:
196214
assertEquals(outFail.statusCode, 404)
197215
}
198216

199-
files.test("proxy server and SPA client apps") {
217+
externalHtmlStyles.test("proxy server and SPA client apps") {
200218
testDir =>
201219
val backendPort = 8090
202220
val thisTestPort = basePort + 3
@@ -222,17 +240,19 @@ trait PlaywrightTest extends munit.FunSuite:
222240
"--build-tool",
223241
"scala-cli",
224242
"--project-dir",
225-
testDir.toString,
226-
"--styles-dir",
227-
styleDir(testDir).toString,
243+
testDir._1.toString,
244+
"--path-to-index-html",
245+
testDir._2.toString,
228246
"--client-routes-prefix",
229247
"/app",
230248
"--port",
231249
thisTestPort.toString,
232250
"--proxy-target-port",
233251
backendPort.toString,
234252
"--proxy-prefix-path",
235-
"/api"
253+
"/api",
254+
"--log-level",
255+
"trace"
236256
)
237257
)
238258
.unsafeToFuture()
@@ -248,6 +268,7 @@ trait PlaywrightTest extends munit.FunSuite:
248268

249269
val canGetHtml = requests.get(s"http://localhost:$thisTestPort", check = false)
250270
assertEquals(canGetHtml.statusCode, 200)
271+
251272
}
252273

253274
files.test("no styles") {

0 commit comments

Comments
 (0)