Skip to content

Commit

Permalink
Override http & https port from the remote write url (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantinos Sideris committed Jun 14, 2021
1 parent 5dde976 commit 227a170
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/Haka/Handlers/Heartbeats.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import Katip
import qualified Network.HTTP.Req as R
import qualified Relude.Unsafe as Unsafe
import Servant
import Text.URI (mkURI)
import Text.URI (URI, authPort, mkURI, uriAuthority)

data User = User
{ name :: Text,
Expand Down Expand Up @@ -209,15 +209,23 @@ remoteWriteHeartbeats (Just conf) machineHeader heartbeats = do

case (R.useHttpsURI remoteUrl, R.useHttpURI remoteUrl) of
(Nothing, Nothing) -> logFM ErrorS ("Malformed remote write URL was given: " <> show remoteUrl)
(Just (httpsUrl, _), _) -> mkReq httpsUrl >> pure ()
(_, Just (httpUrl, _)) -> mkReq httpUrl >> pure ()
(Just (httpsUrl, _), _) -> mkReq remoteUrl httpsUrl >> pure ()
(_, Just (httpUrl, _)) -> mkReq remoteUrl httpUrl >> pure ()
where
encodedToken = encode (encodeUtf8 (token conf))

mkReq :: (R.MonadHttp m) => R.Url scheme -> m R.IgnoreResponse
mkReq url = R.req R.POST url (R.ReqBodyJson heartbeats) R.ignoreResponse mkHeader
mkPort :: Text.URI.URI -> R.Option scheme
mkPort originalUri = case Text.URI.uriAuthority originalUri of
Left _ -> mempty
Right u -> case Text.URI.authPort u of
Just p -> R.port (fromIntegral p)
Nothing -> mempty

mkHeader :: R.Option scheme
mkHeader =
mkReq :: (R.MonadHttp m) => Text.URI.URI -> R.Url scheme -> m R.IgnoreResponse
mkReq originalUri url = R.req R.POST url (R.ReqBodyJson heartbeats) R.ignoreResponse (mkHeader originalUri)

mkHeader :: Text.URI.URI -> R.Option scheme
mkHeader originalUri =
R.header "Authorization" ("Basic " <> encodedToken)
<> maybe mempty (R.header "X-Machine-Name" . encodeUtf8) machineHeader
<> mkPort originalUri

0 comments on commit 227a170

Please sign in to comment.