Commit 520c7701 authored by Alexandre Delanoë's avatar Alexandre Delanoë

Merge remote-tracking branch 'origin/adinapoli/issue-374' into dev

parents ec845b10 9378fb7e
......@@ -37,7 +37,6 @@ import Gargantext.API.Admin.Settings.MicroServices
import Gargantext.API.Admin.Types
import Gargantext.API.Routes.Named.Private
import Gargantext.API.ThrowAll (throwAllRoutes)
import Gargantext.API.Types (HTML)
import Gargantext.Core.Config (gc_frame_write_url)
import Gargantext.Database.Prelude (hasConfig)
import Gargantext.Prelude hiding (Handler)
......@@ -53,7 +52,6 @@ import Servant.Client.Core.BaseUrl
import Servant.Server.Generic
import Text.RE.Replace hiding (Capture)
import Text.RE.TDFA.ByteString
import Text.RawString.QQ (r)
-- See https://github.com/haskell-servant/servant/issues/1601#issue-1338013029
instance {-# OVERLAPPING #-}
......@@ -124,7 +122,7 @@ data NotesProxy mode = NotesProxy
-- | The config file which contains the server settings for the websocket connection
-- that we have to overwrite with our settings.
, configFile :: mode :- "config" :> Get '[HTML] T.Text
, configFile :: mode :- "config" :> Raw
-- | Once the connection has been established, this is the websocket endpoint to
-- poll edits.
......@@ -164,29 +162,46 @@ server env = ReverseProxyAPI {
, proxyPassAll = proxyPassServer ST_notes env
}
-- | A customised configuration file that the \"notes\" service would otherwise send us, that
-- | Customise the configuration file that the \"notes\" service would otherwise send us, that
-- overrides the 'urlpath' to contain the proper service path, so that the websocket connection
-- can be started correctly. If we do not override the 'urlpath', due to the way things work
-- internally, the Javascript of CodiMD would otherwise take the first slice of the URL path
-- (something like `/notes/<frameId>`) and use /that/ as the <frameId>, which would be wrong
-- as it would try to establish a connection to `noteId=notes`.
configJS :: BaseUrl -> ServiceType -> T.Text
configJS bu st = T.pack $ [r|
window.domain = '|] <> (baseUrlHost bu) <> [r|'
window.urlpath = '|] <> renderServiceType st <> [r|'
window.debug = false
window.version = '1.2.0'
customiseConfigJS :: BaseUrl -> ServiceType -> ConduitT B.ByteString (Flush Builder) IO ()
customiseConfigJS bu st = CC.map flushReplace
where
-- Replaces the relative links in the proxied page content with proper urls.
flushReplace :: B.ByteString -> Flush Builder
flushReplace = Chunk . byteString . replaceWindowDomain . replaceUrlPath
replaceWindowDomain :: B.ByteString -> B.ByteString
replaceWindowDomain htmlBlob =
replaceAllCaptures ALL makeAbsolute $ htmlBlob *=~
[re|window.domain.*=*'.*'$|]
where
makeAbsolute _ _loc cap = case capturedText cap of
_ -> Just $ C8.pack $ "window.domain = '" <> (baseUrlHost bu) <> "'"
window.allowedUploadMimeTypes = ["image/jpeg","image/png","image/jpg","image/gif","image/svg+xml"]
replaceUrlPath :: B.ByteString -> B.ByteString
replaceUrlPath htmlBlob =
replaceAllCaptures ALL makeAbsolute $ htmlBlob *=~
[re|window.urlpath.*=*'.*'$|]
where
makeAbsolute _ _loc cap = case capturedText cap of
_ -> Just $ C8.pack $ "window.urlpath = '" <> renderServiceType st <> "'"
window.DROPBOX_APP_KEY = ''
|]
configFileSettings :: Env -> ServiceType -> WaiProxySettings
configFileSettings env sty =
defaultWaiProxySettings
{ wpsProcessBody = \_req _res -> Just $ customiseConfigJS (proxyUrl env) sty
}
notesProxyImplementation :: Env -> NotesProxy AsServer
notesProxyImplementation env = NotesProxy {
slideEp = \frameId -> slideProxyServer env frameId
, publishEp = \frameId -> publishProxyServer env frameId
, configFile = pure $ configJS (proxyUrl env) sty
, configFile = defaultForwardServerWithSettings sty id env (configFileSettings env sty)
, notesSocket = socketIOProxyImplementation sty env
, meEndpoint = proxyPassServer sty env
, notesEp = \_frameId -> defaultForwardServer sty id env
......@@ -250,12 +265,12 @@ removeFromReferer pth originalRequest =
proxyUrl :: Env -> BaseUrl
proxyUrl env = mkProxyUrl (env ^. hasConfig) (env ^. env_settings . microservicesSettings)
defaultForwardServer :: ServiceType
defaultForwardServerWithSettings :: ServiceType
-> (Request -> Request)
-> Env
-> WaiProxySettings
-> ServerT Raw m
defaultForwardServer sty presendModifyRequest env =
defaultForwardServerWithSettings sty presendModifyRequest env proxySettings =
Tagged $ waiProxyToSettings forwardRequest (proxySettings) (env ^. env_manager)
where
......@@ -265,17 +280,6 @@ defaultForwardServer sty presendModifyRequest env =
proxyUrlStr :: String
proxyUrlStr = showBaseUrl (proxyUrl env)
proxySettings :: WaiProxySettings
proxySettings =
defaultWaiProxySettings {
wpsProcessBody = \_req _res -> Just $ replaceRelativeLinks microURL proxyPath
, wpsModifyResponseHeaders = \_req _res -> tweakResponseHeaders
, wpsRedirectCounts = 5
}
where
microURL = proxyDestination
proxyPath = C8.pack $ proxyUrlStr <> serviceTypeToProxyPath sty
setHost :: ProxyDestination -> RequestHeaders -> RequestHeaders
setHost hst hdrs = (hHost, fwdHost hst) : filter ((/=) hHost . fst) hdrs
......@@ -292,6 +296,27 @@ defaultForwardServer sty presendModifyRequest env =
}
pure $ WPRModifiedRequest proxiedReq (ProxyDest (fwdHost proxyDestination) (fwdPort proxyDestination))
defaultForwardServer :: ServiceType
-> (Request -> Request)
-> Env
-> ServerT Raw m
defaultForwardServer sty presendModifyRequest env =
defaultForwardServerWithSettings sty presendModifyRequest env $
defaultWaiProxySettings {
wpsProcessBody = \_req _res -> Just $ replaceRelativeLinks proxyDestination proxyPath
, wpsModifyResponseHeaders = \_req _res -> tweakResponseHeaders
, wpsRedirectCounts = 5
}
where
proxyPath = C8.pack $ proxyUrlStr <> serviceTypeToProxyPath sty
proxyDestination :: ProxyDestination
proxyDestination = mkProxyDestination env
proxyUrlStr :: String
proxyUrlStr = showBaseUrl (proxyUrl env)
--
-- Utility functions
--
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment