Skip to content

Commit

Permalink
configurable welcome mesage
Browse files Browse the repository at this point in the history
Summary: Pure refactoring to move the welcome message to Config. I want to emit Scuba links in the terminal when the server starts, but this is Facebook specific so it needs to be done in config.

Reviewed By: malanka

Differential Revision: D55995228

fbshipit-source-id: 21e934c48d6c001e31baf586fad8d8db20d90212
  • Loading branch information
Pepe Iborra authored and facebook-github-bot committed Apr 11, 2024
1 parent c3838b9 commit 6eab781
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
12 changes: 11 additions & 1 deletion glean/glass/Glean/Glass/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ module Glean.Glass.Config
defaultPort,
defaultServiceName,
defaultRefreshFreq,
defaultListDatabasesRetry
defaultListDatabasesRetry,
defaultWelcomeMessage
) where

import Data.Text (Text)
import Glean.Util.Time
import Glean.Glass.Env
import Util.Text (textShow)

defaultPort :: Int
defaultPort = 26073
Expand All @@ -32,3 +35,10 @@ defaultRefreshFreq = minutes 5

defaultListDatabasesRetry :: Int
defaultListDatabasesRetry = 5

defaultWelcomeMessage :: Config -> Text
defaultWelcomeMessage Config{..} = mconcat
[ "glass"
, ": port " <> textShow listenPort
, ", config " <> configKey
]
10 changes: 10 additions & 0 deletions glean/glass/Glean/Glass/Env.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module Glean.Glass.Env
setSnapshotBackend,
setSourceControl,
setTracer,
updateWelcomeMessage,

-- * Session resources
Env(..),
Expand Down Expand Up @@ -54,6 +55,7 @@ data Config = Config
, snapshotBackend :: EventBaseDataplane -> Some SnapshotBackend
, sourceControl :: EventBaseDataplane -> Some SourceControl
, tracer :: GlassTracer
, welcomeMessage :: EventBaseDataplane -> Config -> IO Text
}

setSnapshotBackend
Expand All @@ -70,6 +72,14 @@ setSourceControl sourceControl config =
setTracer :: GlassTracer -> Config -> Config
setTracer tracer config = config{ tracer = tracer }

updateWelcomeMessage
:: ( (EventBaseDataplane -> Config -> IO Text)
-> EventBaseDataplane -> Config -> IO Text)
-> Config
-> Config
updateWelcomeMessage f config =
config{ welcomeMessage = f (welcomeMessage config)}

-- | Read-only, scoped, dynamic resources.
data Env = Env
{ evp :: EventBaseDataplane
Expand Down
14 changes: 2 additions & 12 deletions glean/glass/Glean/Glass/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ import qualified Thrift.Server.HTTP as Thrift
#endif
import Util.EventBase ( withEventBaseDataplane )
import Util.Log.Text ( logInfo )
import Util.Text ( textShow )
import Logger.IO (withLogger)

import Control.Exception (SomeException, fromException)
import Data.Text (Text)
import qualified Data.Text as Text
import Data.Text.Encoding (encodeUtf8)
import Options.Applicative (Parser)

Expand Down Expand Up @@ -117,9 +115,9 @@ indexBackend b = Glass.IndexBackend $ case backendKind b of

-- | Kick off the server
runGlass :: Server
runGlass res@Glass.Env{fb303} conf@Glass.Config{..} = do
runGlass res@Glass.Env{fb303, evp} conf@Glass.Config{..} = do

logInfo =<< welcomeMessage conf
logInfo =<< welcomeMessage evp conf

let options = Thrift.defaultOptions
{ Thrift.desiredPort = Just listenPort
Expand All @@ -140,14 +138,6 @@ assignHeaders _ (Left e) | isRevisionNotAvailableException e =
isExactRevisionNotAvailable _ = False
assignHeaders _ _ = []

welcomeMessage :: Glass.Config -> IO Text
welcomeMessage Glass.Config{..} = do
return $ Text.concat
[ "glass"
, ": port " <> textShow listenPort
, ", config " <> configKey
]

-- | Perform an operation with the latest RepoMapping
withCurrentRepoMapping :: Glass.Env -> (Glass.Env -> IO a) -> IO a
withCurrentRepoMapping env0 fn = do
Expand Down
2 changes: 2 additions & 0 deletions glean/glass/Glean/Glass/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import qualified Glean.Glass.Env as Glass
import qualified Glean.Glass.Config as Glass
import Glean.Glass.SnapshotBackend
import Glean.Glass.SourceControl
import Glean.Glass.Config (defaultWelcomeMessage)

options :: Parser (Glass.Config -> Glass.Config) -> ParserInfo Glass.Config
options mod = info (helper <*> mod <*> configParser) fullDesc
Expand All @@ -39,6 +40,7 @@ configParser = do
snapshotBackend <- pure (const $ Some NilSnapshotBackend)
sourceControl <- pure (const (Some NilSourceControl))
tracer <- pure mempty
welcomeMessage <- pure (pure (pure . defaultWelcomeMessage))
return Glass.Config{configKey = Glass.defaultConfigKey, ..}

portParser :: Parser Int
Expand Down

0 comments on commit 6eab781

Please sign in to comment.