| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
IHP.ControllerSupport
Synopsis
- type Action' = IO ResponseReceived
- (|>) :: a -> (a -> b) -> b
- getRequestBody :: (?request :: Request) => IO ByteString
- getRequestPath :: (?request :: Request) => ByteString
- getRequestPathAndQuery :: (?request :: Request) => ByteString
- getHeader :: (?request :: Request) => ByteString -> Maybe ByteString
- request :: (?request :: Request) => Request
- requestHeaders :: Request -> RequestHeaders
- getFiles :: (?request :: Request) => [File ByteString]
- class (Show controller, Eq controller) => Controller controller where
- beforeAction :: IO ()
- action :: controller -> IO ResponseReceived
- runAction :: (Controller controller, ?context :: ControllerContext, ?modelContext :: ModelContext, ?respond :: Respond) => controller -> IO ResponseReceived
- type ControllerContext = Request
- class InitControllerContext (application :: k) where
- initContext :: IO ()
- runActionWithNewContext :: forall application controller. (Controller controller, ?request :: Request, ?respond :: Respond, InitControllerContext application, ?application :: application, Typeable application, Typeable controller) => controller -> IO ResponseReceived
- initActionContext :: forall application controller. (Controller controller, ?request :: Request, ?respond :: Respond, InitControllerContext application, ?application :: application, Typeable application, Typeable controller) => controller -> IO ControllerContext
- respondWith :: (?request :: Request, ?respond :: Respond) => Response -> IO ResponseReceived
- respondAndExit :: (?request :: Request, ?respond :: Respond) => Response -> IO a
- earlyReturn :: IO ResponseReceived -> IO a
- jumpToAction :: (Controller action, ?context :: ControllerContext, ?modelContext :: ModelContext, ?respond :: Respond, ?request :: Request) => action -> IO ResponseReceived
- requestBodyJSON :: (?request :: Request, ?respond :: Respond) => IO Value
- startWebSocketApp :: forall webSocketApp application. (?request :: Request, ?respond :: Respond, InitControllerContext application, ?application :: application, Typeable application, WSApp webSocketApp) => webSocketApp -> IO ResponseReceived -> Application
- startWebSocketAppAndFailOnHTTP :: forall webSocketApp application. (?request :: Request, ?respond :: Respond, InitControllerContext application, ?application :: application, Typeable application, WSApp webSocketApp) => webSocketApp -> Application
- setHeader :: (?request :: Request) => Header -> IO ()
- getAppConfig :: forall configParameter context. (?context :: context, ConfigProvider context, Typeable configParameter) => configParameter
- type Respond = Response -> IO ResponseReceived
- data Request
- rlsContextVaultKey :: Key (IORef (Maybe RowLevelSecurityContext))
- initRequestContext :: (InitControllerContext application, ?application :: application, Typeable application) => TypeRep -> Request -> Respond -> IO ControllerContext
- data ResponseReceived
Documentation
type Action' = IO ResponseReceived Source #
getRequestBody :: (?request :: Request) => IO ByteString Source #
getRequestPath :: (?request :: Request) => ByteString Source #
Returns the request path, e.g. /Users or /CreateUser
getRequestPathAndQuery :: (?request :: Request) => ByteString Source #
Returns the request path and the query params, e.g. /ShowUser?userId=9bd6b37b-2e53-40a4-bb7b-fdba67d6af42
getHeader :: (?request :: Request) => ByteString -> Maybe ByteString Source #
Returns a header value for a given header name. Returns Nothing if not found
The header is looked up in a case insensitive way.
>>>getHeader "Content-Type"Just "text/html"
>>>getHeader "X-My-Custom-Header"Nothing
request :: (?request :: Request) => Request Source #
Returns the current HTTP request.
See https://hackage.haskell.org/package/wai-3.2.2.1/docs/Network-Wai.html#t:Request
requestHeaders :: Request -> RequestHeaders #
A list of headers (a pair of key and value) in an HTTP request.
class (Show controller, Eq controller) => Controller controller where Source #
Minimal complete definition
runAction :: (Controller controller, ?context :: ControllerContext, ?modelContext :: ModelContext, ?respond :: Respond) => controller -> IO ResponseReceived Source #
type ControllerContext = Request Source #
The WAI Request threaded through controllers and views as the
?context implicit parameter. All request-scoped state lives in
request.vault (see RequestVault). The type alias is preserved
for source compatibility with existing ?context :: ControllerContext
type signatures.
class InitControllerContext (application :: k) where Source #
Minimal complete definition
Nothing
Methods
initContext :: IO () Source #
Instances
| InitControllerContext () Source # | |
Defined in IHP.ControllerSupport Methods initContext :: IO () Source # | |
runActionWithNewContext :: forall application controller. (Controller controller, ?request :: Request, ?respond :: Respond, InitControllerContext application, ?application :: application, Typeable application, Typeable controller) => controller -> IO ResponseReceived Source #
initActionContext :: forall application controller. (Controller controller, ?request :: Request, ?respond :: Respond, InitControllerContext application, ?application :: application, Typeable application, Typeable controller) => controller -> IO ControllerContext Source #
Bind implicit parameters and run initContext for a controller action.
Used by runActionWithNewContext and WebSocket handlers.
respondWith :: (?request :: Request, ?respond :: Respond) => Response -> IO ResponseReceived Source #
Sends a response to the client. Used by render functions.
This is the normal way to respond - it calls the WAI respond callback directly and returns the ResponseReceived.
respondAndExit :: (?request :: Request, ?respond :: Respond) => Response -> IO a Source #
Sends a response and exits the current action via early return.
Sends the response via respondWith then throws EarlyReturnException
so the action short-circuits.
earlyReturn :: IO ResponseReceived -> IO a #
Exit a request handler early after sending a response.
Use this when you want to conditionally exit a handler:
handler = do
when someCondition do
earlyReturn (sendErrorResponse ...)
-- rest of the handler
sendNormalResponseThe function runs the given IO action (which should send a response),
then throws EarlyReturnException to exit. The middleware catches this
exception so it doesn't propagate further.
jumpToAction :: (Controller action, ?context :: ControllerContext, ?modelContext :: ModelContext, ?respond :: Respond, ?request :: Request) => action -> IO ResponseReceived Source #
startWebSocketApp :: forall webSocketApp application. (?request :: Request, ?respond :: Respond, InitControllerContext application, ?application :: application, Typeable application, WSApp webSocketApp) => webSocketApp -> IO ResponseReceived -> Application Source #
startWebSocketAppAndFailOnHTTP :: forall webSocketApp application. (?request :: Request, ?respond :: Respond, InitControllerContext application, ?application :: application, Typeable application, WSApp webSocketApp) => webSocketApp -> Application Source #
setHeader :: (?request :: Request) => Header -> IO () Source #
Set a header value for a given header name.
>>>setHeader ("Content-Language", "en")
getAppConfig :: forall configParameter context. (?context :: context, ConfigProvider context, Typeable configParameter) => configParameter Source #
Returns a custom config parameter
>>>getAppConfig @StripePublicKeyStripePublicKey "pk_test_..."
Example:
First you need to define a custom config parameter in Config.hs:
-- Config/Config.hs
newtype StripePublicKey = StripePublicKey Text
config :: ConfigBuilder
config = do
-- ...
stripePublicKey <- StripePublicKey <$> env @Text "STRIPE_PUBLIC_KEY"
option stripePublicKeyThen you can access it using getAppConfig:
action MyAction = do
let (StripePublicKey stripePublicKey) = getAppConfig @StripePublicKey
putStrLn ("Stripe public key: " <> stripePublicKey)type Respond = Response -> IO ResponseReceived #
Type alias for WAI respond function
Information on the request sent by the client. This abstracts away the details of the underlying implementation.
Instances
| Show Request | |
| ToApplication Application | |
Defined in Network.Wai.UrlMap Methods | |
| ToApplication UrlMap | |
Defined in Network.Wai.UrlMap Methods toApplication :: UrlMap -> Application | |
| HasField "actionType" Request ActionType Source # | |
Defined in IHP.ActionType Methods getField :: Request -> ActionType # | |
| HasField "frameworkConfig" Request FrameworkConfig Source # | |
Defined in IHP.RequestVault Methods getField :: Request -> FrameworkConfig # | |
| HasField "modelContext" Request ModelContext Source # | |
Defined in IHP.RequestVault.ModelContext Methods getField :: Request -> ModelContext # | |
| HasField "parsedBody" Request RequestBody Source # | |
Defined in IHP.RequestVault.ModelContext Methods getField :: Request -> RequestBody # | |
| HasField "pgListener" Request PGListener Source # | |
Defined in IHP.RequestVault Methods getField :: Request -> PGListener # | |
initRequestContext :: (InitControllerContext application, ?application :: application, Typeable application) => TypeRep -> Request -> Respond -> IO ControllerContext Source #
Bind implicit parameters from a raw WAI request, insert the action type
into the vault, and run initContext. Specialized once per application type.
NOINLINE ensures GHC compiles one copy shared across all controllers.
Exceptions from initContext (including EarlyReturnException) propagate
to the caller, which is expected to catch them.
data ResponseReceived #
A special datatype to indicate that the WAI handler has received the response. This is to avoid the need for Rank2Types in the definition of Application.
It is highly advised that only WAI handlers import and use the data constructor for this data type.
Since: wai-3.0.0
Instances
| ToApplication Application | |
Defined in Network.Wai.UrlMap Methods | |
| ToApplication UrlMap | |
Defined in Network.Wai.UrlMap Methods toApplication :: UrlMap -> Application | |