-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add proxy functionality. Re-enable local conf.
- Loading branch information
Showing
5 changed files
with
81 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
modules/web/server/src/main/scala/navigate/web/server/http4s/ProxyBuilder.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) 2016-2023 Association of Universities for Research in Astronomy, Inc. (AURA) | ||
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause | ||
|
||
package navigate.web.server.http4s | ||
|
||
import cats.data.OptionT | ||
import cats.effect.Async | ||
import cats.effect.kernel.Resource | ||
import cats.syntax.all.* | ||
import fs2.io.net.Network | ||
import org.http4s.HttpRoutes | ||
import org.http4s.Uri | ||
import org.http4s.ember.client.EmberClientBuilder | ||
import org.http4s.headers._ | ||
|
||
object ProxyBuilder: | ||
def buildService[F[_]: Async: Network]( | ||
baseUri: Uri, | ||
localPath: Uri.Path | ||
): Resource[F, HttpRoutes[F]] = | ||
val remoteBaseHost: String = baseUri.host.map(_.toString).orEmpty | ||
val localPathElements: Int = localPath.segments.length | ||
|
||
EmberClientBuilder | ||
.default[F] | ||
.build | ||
.map( | ||
_.toHttpApp | ||
.mapK(OptionT.liftK) // Turns HttpApp into HttpRoutes | ||
.local: req => | ||
req | ||
.withUri( | ||
// Drop the local path (eg: "/proxy") | ||
baseUri.resolve(req.uri.withPath(req.uri.path.splitAt(localPathElements)._2)) | ||
) | ||
.putHeaders(Host(remoteBaseHost)) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters