{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# OPTIONS_HADDOCK prune #-}

{- |
Many programs present their interface in the form of a webservice, be it
internet-facing, for internal use, or even just as a machine-local daemon. The
Haskell language has numerous frameworks for building webservices and a number
of high-quality HTTP server implementations. This module provides support for
the Web Application Interface from the __wai__ package and the __warp__
webserver.

Given an 'Network.Wai.Application' type (the definition of your web service) and a
'Network.Wai.Middleware' (which is just @Application -> Application@), run a the
"Core.Program"'s 'Core.Program.Execute.Program' monad. Metrics values (aka web
server logs) will be sent as key/value pairs via "Core.Telemetry".

= Usage

First set up your program and initialize the telemetry subsystem.

@
import "Core.Program"
import "Core.Telemetry"
import "Core.Webservice.Warp"

main :: 'IO' ()
main = do
    context <- 'Core.Program.Execute.configure' \"1.0\" 'Core.Program.Execute.None' ('Core.Program.Arguments.simpleConfig' [])
    context' <- 'initializeTelemetry' ['Core.Telemetry.Console.consoleExporter', 'Core.Telemetry.Structured.structuredExporter', 'Core.Telemetry.Honeycomb.honeycombExporter'] context
    'Core.Program.Execute.executeWith' context' \$ do
        'Core.Program.Logging.info' \"Starting...\"
        'launchWebserver' 8080 application
@

You can then describe your webservice 'Application', for example

@
application :: 'Application'
application = request sendResponse =
    sendResponse ('Network.Wai.responseLBS' 'Network.HTTP.Types.status200' [] \"Hello World\")
@

performs the heroic duty of replying to you with the given string. In
practice, if you're using something like __servant__ to define the shape of
your webservice its 'Servant.serve' function will give you the 'Application'
you're trying to run.

Logging output is sent to the telemtry channel. If you run your program with
the console exporter, and hit something like
<http://localhost:8080/hello?question=answer> will see something like this:

@
\$ __hello-service --telemetry=console__
03:16:01Z (00.002) Starting...
03:16:04Z (00.259)                                             <-- this is the request duration, 259 ms
/hello:                                                        <-- the base of the context path aka \"endpoint\"
  request.method = \"GET\"
  request.path = "/hello?question=answer"                      <-- the full context path with query string
  response.status_code = "200"
@

This is useful for debugging during development but for production you are
recommended to use the structured logging output or to send the traces to an
observability service; this will be the root span of a trace.
-}
module Core.Webserver.Warp
    ( Port
    , launchWebserver
    , launchWebserverTLS
    , requestContextKey
    , contextFromRequest
    , ContextNotFoundInRequest (..)
    ) where

--
-- We follow the convention used elsewhere in this collection of libraries of
-- using a qualified name for the imports of significant libraries. It's a bit
-- cumbersome, but makes it easier to disambiguate what's going on when
-- comparing to almost identical code in sibling modules covering other
-- webserver frameworks.
--

import Control.Exception.Safe qualified as Safe (catch)
import Core.Program.Context
import Core.Program.Logging
import Core.System.Base
import Core.Telemetry.Identifiers
import Core.Telemetry.Observability
import Core.Text.Rope
import Data.List qualified as List
import Data.Vault.Lazy qualified as Vault
import Network.HTTP.Types
    ( Status
    , hContentType
    , status400
    , status500
    , statusCode
    )
import Network.Wai
import Network.Wai.Handler.Warp (InvalidRequest, Port)
import Network.Wai.Handler.Warp qualified as Warp
import Network.Wai.Handler.WarpTLS (TLSSettings)
import Network.Wai.Handler.WarpTLS qualified as Warp

{- |
Given a WAI 'Application', run a Warp webserver on the specified port from
within the 'Program' monad.

@
    'launchWebserver' 80 application
@

(this wraps the __warp__ package)
-}
launchWebserver :: Port -> Application -> Program τ ()
launchWebserver :: forall τ. Port -> Application -> Program τ ()
launchWebserver Port
port Application
application = do
    Context τ
context <- forall τ. Program τ (Context τ)
getContext
    let settings :: Settings
settings =
            (Maybe Request -> SomeException -> IO ()) -> Settings -> Settings
Warp.setOnException
                (forall τ. Context τ -> Maybe Request -> SomeException -> IO ()
onExceptionHandler Context τ
context)
                forall b c a. (b -> c) -> (a -> b) -> a -> c
. Port -> Settings -> Settings
Warp.setPort Port
port
                forall a b. (a -> b) -> a -> b
$ Settings
Warp.defaultSettings
    forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
        Settings -> Application -> IO ()
Warp.runSettings
            Settings
settings
            ( forall τ. Context τ -> Application -> Application
loggingMiddleware
                Context τ
context
                Application
application
            )

{- |
Given a WAI 'Application', run a Warp webserver on the specified port from
within the 'Program' monad. This variant of 'launchWebserver' runs the server
with a TLS connection.

For the common case of supplying a certificate and private key, you can do:

@
    let crypto = 'tlsSettings' \"\/path\/to\/certificate.crt\" \"\/path\/to\/private.key\"
    'launchWebserverTLS' crypto 443 application
@

(this wraps the __warp-tls__ package; for more complex certificate management
requirements see the documentation for the 'TLSSettings' type there)

@since 0.2.1
-}
launchWebserverTLS :: TLSSettings -> Port -> Application -> Program τ ()
launchWebserverTLS :: forall τ. TLSSettings -> Port -> Application -> Program τ ()
launchWebserverTLS TLSSettings
crypto Port
port Application
application = do
    Context τ
context <- forall τ. Program τ (Context τ)
getContext
    let settings :: Settings
settings =
            (Maybe Request -> SomeException -> IO ()) -> Settings -> Settings
Warp.setOnException
                (forall τ. Context τ -> Maybe Request -> SomeException -> IO ()
onExceptionHandler Context τ
context)
                forall b c a. (b -> c) -> (a -> b) -> a -> c
. Port -> Settings -> Settings
Warp.setPort Port
port
                forall a b. (a -> b) -> a -> b
$ Settings
Warp.defaultSettings
    forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
        TLSSettings -> Settings -> Application -> IO ()
Warp.runTLS
            TLSSettings
crypto
            Settings
settings
            ( forall τ. Context τ -> Application -> Application
loggingMiddleware
                Context τ
context
                Application
application
            )

requestContextKey :: forall t. Vault.Key (Context t)
requestContextKey :: forall t. Key (Context t)
requestContextKey = forall a. IO a -> a
unsafePerformIO forall a. IO (Key a)
Vault.newKey
{-# NOINLINE requestContextKey #-}

contextFromRequest :: forall t. Request -> Maybe (Context t)
contextFromRequest :: forall t. Request -> Maybe (Context t)
contextFromRequest Request
request = forall a. Key a -> Vault -> Maybe a
Vault.lookup forall t. Key (Context t)
requestContextKey (Request -> Vault
vault Request
request)

data ContextNotFoundInRequest = ContextNotFoundInRequest deriving (Port -> ContextNotFoundInRequest -> ShowS
[ContextNotFoundInRequest] -> ShowS
ContextNotFoundInRequest -> String
forall a.
(Port -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ContextNotFoundInRequest] -> ShowS
$cshowList :: [ContextNotFoundInRequest] -> ShowS
show :: ContextNotFoundInRequest -> String
$cshow :: ContextNotFoundInRequest -> String
showsPrec :: Port -> ContextNotFoundInRequest -> ShowS
$cshowsPrec :: Port -> ContextNotFoundInRequest -> ShowS
Show)

instance Exception ContextNotFoundInRequest where
    displayException :: ContextNotFoundInRequest -> String
displayException ContextNotFoundInRequest
_ = String
"Context was not found in request. This is a serious error."

-- which is IO
loggingMiddleware :: Context τ -> Application -> Application
loggingMiddleware :: forall τ. Context τ -> Application -> Application
loggingMiddleware (Context τ
context0 :: Context τ) Application
application Request
request Response -> IO ResponseReceived
sendResponse = do
    let path :: Rope
path = forall α. Textual α => α -> Rope
intoRope (Request -> ByteString
rawPathInfo Request
request)
        query :: Rope
query = forall α. Textual α => α -> Rope
intoRope (Request -> ByteString
rawQueryString Request
request)
        method :: Rope
method = forall α. Textual α => α -> Rope
intoRope (Request -> ByteString
requestMethod Request
request)

    forall τ α. Context τ -> Program τ α -> IO α
subProgram Context τ
context0 forall a b. (a -> b) -> a -> b
$ do
        forall z a. Request -> Program z a -> Program z a
resumeTraceIf Request
request forall a b. (a -> b) -> a -> b
$ do
            forall z a. Rope -> Program z a -> Program z a
encloseSpan Rope
path forall a b. (a -> b) -> a -> b
$ do
                Context τ
context1 <- forall τ. Program τ (Context τ)
getContext

                -- we could call `telemetry` here with the request values, but
                -- since we call into nested actions which could clear the
                -- state without starting a new span, we duplicate adding them
                -- below to ensure they get passed through.

                forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
                    -- The below wires the context in the request's `vault`. As the type of
                    -- `Context` is polymorphic to support user data, we have to use a type
                    -- application to make sure that consumers can later fetch the appropriate
                    -- `Context t`.
                    let vault' :: Vault
vault' = forall a. Key a -> a -> Vault -> Vault
Vault.insert (forall t. Key (Context t)
requestContextKey @τ) Context τ
context1 (Request -> Vault
vault Request
request)
                        request' :: Request
request' = Request
request {vault :: Vault
vault = Vault
vault'}
                    forall (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
m a -> (e -> m a) -> m a
Safe.catch
                        ( Application
application Request
request' forall a b. (a -> b) -> a -> b
$ \Response
response -> do
                            -- accumulate the details for logging
                            let code :: Port
code = Status -> Port
statusCode (Response -> Status
responseStatus Response
response)

                            forall τ α. Context τ -> Program τ α -> IO α
subProgram Context τ
context1 forall a b. (a -> b) -> a -> b
$ do
                                forall τ. [MetricValue] -> Program τ ()
telemetry
                                    [ forall σ. Telemetry σ => Rope -> σ -> MetricValue
metric Rope
"request.method" Rope
method
                                    , forall σ. Telemetry σ => Rope -> σ -> MetricValue
metric Rope
"request.path" Rope
path
                                    , if Rope -> Bool
nullRope Rope
query then forall σ. Telemetry σ => Rope -> σ -> MetricValue
metric Rope
"request.query" () else forall σ. Telemetry σ => Rope -> σ -> MetricValue
metric Rope
"request.query" Rope
query
                                    , forall σ. Telemetry σ => Rope -> σ -> MetricValue
metric Rope
"response.status_code" Port
code
                                    ]

                            -- actually handle the request
                            Response -> IO ResponseReceived
sendResponse Response
response
                        )
                        ( \(SomeException
e :: SomeException) -> do
                            -- set the magic `error` field with the exception text.
                            let text :: Rope
text = forall α. Textual α => α -> Rope
intoRope (forall e. Exception e => e -> String
displayException SomeException
e)
                                (Status
status, Rope
detail) = SomeException -> (Status, Rope)
assignException SomeException
e
                                code :: Port
code = Status -> Port
statusCode Status
status

                            forall τ α. Context τ -> Program τ α -> IO α
subProgram Context τ
context1 forall a b. (a -> b) -> a -> b
$ do
                                forall τ. Rope -> Program τ ()
warn Rope
"Trapped internal exception"
                                forall τ. Rope -> Rope -> Program τ ()
debug Rope
"e" Rope
text
                                forall τ. [MetricValue] -> Program τ ()
telemetry
                                    [ forall σ. Telemetry σ => Rope -> σ -> MetricValue
metric Rope
"request.method" Rope
method
                                    , forall σ. Telemetry σ => Rope -> σ -> MetricValue
metric Rope
"request.path" Rope
path
                                    , if Rope -> Bool
nullRope Rope
query then forall σ. Telemetry σ => Rope -> σ -> MetricValue
metric Rope
"request.query" () else forall σ. Telemetry σ => Rope -> σ -> MetricValue
metric Rope
"request.query" Rope
query
                                    , forall σ. Telemetry σ => Rope -> σ -> MetricValue
metric Rope
"response.status_code" Port
code
                                    , forall σ. Telemetry σ => Rope -> σ -> MetricValue
metric Rope
"error" Rope
text
                                    ]

                            Response -> IO ResponseReceived
sendResponse
                                ( Status -> ResponseHeaders -> ByteString -> Response
responseLBS
                                    Status
status
                                    [(HeaderName
hContentType, ByteString
"text/plain; charset=utf-8")]
                                    (forall α. Textual α => Rope -> α
fromRope Rope
detail)
                                )
                        )

assignException :: SomeException -> (Status, Rope)
assignException :: SomeException -> (Status, Rope)
assignException SomeException
e
    | Just (InvalidRequest
_ :: InvalidRequest) <-
        forall e. Exception e => SomeException -> Maybe e
fromException SomeException
e =
        (Status
status400, forall α. Textual α => α -> Rope
intoRope (forall e. Exception e => e -> String
displayException SomeException
e))
    | Bool
otherwise =
        (Status
status500, Rope
"Internal Server Error")

--
-- Ideally this would be a catch-all and not be hit; our application wrapper
-- should have caught this beforehand. However, it turns out "Bad Request"
-- type protocol problems that don't even result in a coherent request -
-- which, sadly, are somewhat to be expected on the wild and wooly internet.
-- So we note them briefly and move on.
--
-- Much more interesting are exceptions which occur within the request path,
-- which means that we can annotate the current span with an `error` field and
-- send it down the telemetry channel.
--
onExceptionHandler :: Context τ -> Maybe Request -> SomeException -> IO ()
onExceptionHandler :: forall τ. Context τ -> Maybe Request -> SomeException -> IO ()
onExceptionHandler Context τ
context Maybe Request
possibleRequest SomeException
e = do
    forall τ α. Context τ -> Program τ α -> IO α
subProgram Context τ
context forall a b. (a -> b) -> a -> b
$ do
        forall τ. Rope -> Program τ ()
critical Rope
"Exception escaped webserver"
        forall α τ. Show α => Rope -> α -> Program τ ()
debugS Rope
"e" SomeException
e
        case Maybe Request
possibleRequest of
            Maybe Request
Nothing -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
            Just Request
request ->
                let line :: Rope
line = forall α. Textual α => α -> Rope
intoRope (Request -> ByteString
requestMethod Request
request) forall a. Semigroup a => a -> a -> a
<> Rope
" " forall a. Semigroup a => a -> a -> a
<> forall α. Textual α => α -> Rope
intoRope (Request -> ByteString
rawPathInfo Request
request) forall a. Semigroup a => a -> a -> a
<> forall α. Textual α => α -> Rope
intoRope (Request -> ByteString
rawQueryString Request
request)
                in  forall τ. Rope -> Rope -> Program τ ()
debug Rope
"request" Rope
line

{- |
Pull the Trace identifier and parent Span identifier out of the request
headers, if present. Resume using those values, otherwise start a new trace.
-}
resumeTraceIf :: Request -> Program z a -> Program z a
resumeTraceIf :: forall z a. Request -> Program z a -> Program z a
resumeTraceIf Request
request Program z a
action =
    case Request -> Maybe (Trace, Span)
extractTraceParent Request
request of
        Maybe (Trace, Span)
Nothing -> do
            forall τ α. Program τ α -> Program τ α
beginTrace Program z a
action
        Just (Trace
trace, Span
parent) -> do
            forall τ α. Trace -> Span -> Program τ α -> Program τ α
usingTrace Trace
trace Span
parent Program z a
action

--
-- This is wildly inefficient. Surely warp must provide a better way to search
-- header values?!?
--
extractTraceParent :: Request -> Maybe (Trace, Span)
extractTraceParent :: Request -> Maybe (Trace, Span)
extractTraceParent Request
request =
    let headers :: ResponseHeaders
headers = Request -> ResponseHeaders
requestHeaders Request
request
        possibleValue :: Maybe ByteString
possibleValue = forall a b. Eq a => a -> [(a, b)] -> Maybe b
List.lookup HeaderName
"traceparent" ResponseHeaders
headers
    in  case Maybe ByteString
possibleValue of
            Maybe ByteString
Nothing -> forall a. Maybe a
Nothing
            Just ByteString
value -> Rope -> Maybe (Trace, Span)
parseTraceParentHeader (forall α. Textual α => α -> Rope
intoRope ByteString
value)