Safe Haskell | None |
---|
Synopsis
- type Action' = IO ResponseReceived
- (|>) :: t1 -> (t1 -> t2) -> t2
- getRequestBody :: (?context :: ControllerContext) => IO LByteString
- getRequestPath :: (?context :: ControllerContext) => ByteString
- getRequestPathAndQuery :: (?context :: ControllerContext) => ByteString
- getHeader :: (?context :: ControllerContext) => ByteString -> Maybe ByteString
- data RequestContext = RequestContext Request Respond RequestBody FrameworkConfig
- request :: (?context :: ControllerContext) => Request
- requestHeaders :: Request -> RequestHeaders
- getFiles :: (?context :: ControllerContext) => [File ByteString]
- class (Show controller, Eq controller) => Controller controller where
- beforeAction :: IO ()
- action :: controller -> IO ()
- runAction :: (Controller controller, ?context :: ControllerContext, ?modelContext :: ModelContext, ?applicationContext :: ApplicationContext, ?requestContext :: RequestContext) => controller -> IO ResponseReceived
- createRequestContext :: ApplicationContext -> Request -> Respond -> IO RequestContext
- data ControllerContext
- class InitControllerContext (application :: k) where
- initContext :: IO ()
- runActionWithNewContext :: forall application controller. (Controller controller, ?applicationContext :: ApplicationContext, ?context :: RequestContext, InitControllerContext application, ?application :: application, Typeable application, Typeable controller) => controller -> IO ResponseReceived
- newContextForAction :: forall application controller. (Controller controller, ?applicationContext :: ApplicationContext, ?context :: RequestContext, InitControllerContext application, ?application :: application, Typeable application, Typeable controller) => controller -> IO (Either (IO ResponseReceived) ControllerContext)
- respondAndExit :: (?context :: ControllerContext) => Response -> IO ()
- jumpToAction :: (Controller action, ?context :: ControllerContext, ?modelContext :: ModelContext) => action -> IO ()
- requestBodyJSON :: (?context :: ControllerContext) => Value
- startWebSocketApp :: forall webSocketApp application. (?applicationContext :: ApplicationContext, ?context :: RequestContext, InitControllerContext application, ?application :: application, Typeable application, WSApp webSocketApp) => IO ResponseReceived -> Application
- startWebSocketAppAndFailOnHTTP :: forall webSocketApp application. (?applicationContext :: ApplicationContext, ?context :: RequestContext, InitControllerContext application, ?application :: application, Typeable application, WSApp webSocketApp) => Application
- setHeader :: (?context :: ControllerContext) => Header -> IO ()
- getAppConfig :: forall configParameter context. (?context :: context, ConfigProvider context, Typeable configParameter) => configParameter
Documentation
getRequestBody :: (?context :: ControllerContext) => IO LByteString Source #
getRequestPath :: (?context :: ControllerContext) => ByteString Source #
Returns the request path, e.g. /Users
or /CreateUser
getRequestPathAndQuery :: (?context :: ControllerContext) => ByteString Source #
Returns the request path and the query params, e.g. /ShowUser?userId=9bd6b37b-2e53-40a4-bb7b-fdba67d6af42
getHeader :: (?context :: ControllerContext) => 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
data RequestContext Source #
request :: (?context :: ControllerContext) => 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 #
getFiles :: (?context :: ControllerContext) => [File ByteString] Source #
class (Show controller, Eq controller) => Controller controller where Source #
Instances
(ToField (PrimaryKey (GetTableName CurrentUserRecord)), Show (PrimaryKey (GetTableName CurrentUserRecord)), HasNewSessionUrl CurrentUserRecord, Typeable CurrentUserRecord, HasField "id" CurrentUserRecord (Id' (GetTableName CurrentUserRecord))) => Controller ApiController Source # | |
Defined in IHP.DataSync.REST.Controller beforeAction :: IO () Source # action :: ApiController -> IO () Source # | |
Controller WelcomeController Source # | |
Defined in IHP.Welcome.Controller beforeAction :: IO () Source # action :: WelcomeController -> IO () Source # | |
(JobsDashboard jobs, AuthenticationMethod authType) => Controller (JobsDashboardController authType jobs) Source # | |
Defined in IHP.Job.Dashboard beforeAction :: IO () Source # action :: JobsDashboardController authType jobs -> IO () Source # |
runAction :: (Controller controller, ?context :: ControllerContext, ?modelContext :: ModelContext, ?applicationContext :: ApplicationContext, ?requestContext :: RequestContext) => controller -> IO ResponseReceived Source #
createRequestContext :: ApplicationContext -> Request -> Respond -> IO RequestContext Source #
data ControllerContext Source #
A container storing useful data along the request lifecycle, such as the request, the current user, set current view layout, flash messages, ...
The controller context is usually accessed via the ?context
variable. It's availble inside the action and the view. Think of it as a key-value-map where the key is the type of the value.
You can store information inside the context using putContext
:
>>>
newtype CurrentLayout = CurrentLayout Html
>>>
>>>
?context <- newControllerContext
>>>
putContext (CurrentLayout layout)
Inside an action you can access the values using fromContext
:
>>>
(CurrentLayout layout) <- fromContext
You can freeze the context and then access values without being inside an IO context (like inside views which are pure):
Call freeze
inside an IO part:
>>>
?context <- freeze ?context
(freeze
is automatically called by IHP before rendering a view, so usually you don't need to call it manually)
Then use the frozen context from your pure code like this:
>>>
let (CurrentLayout layout) = fromFrozenContext in ...
The context is initially created before a action is going to be executed. Its life cycle looks like this:
newControllerContext
: The new controller context is created- The
runActionWithNewContext
fills in a few default values: The current?application
and also the Flash Messages to be rendered in the to-be-generated response. initContext
: The initContext function of theInitControllerContext WebApplication
(inside your FrontController.hs) is called. There application-specific context can be provided. Usually this is the current user and the default layout.beforeAction
: Here the context could also be modified. E.g. the layout could be overriden here for the whole controller.action ..
: The action itself.- Freezing: Before rendering the response, the container is frozen. Frozen means that all previously mutable fields become immutable.
- View Rendering: The frozen container is now used inside the view and layout to display information such as the current user or flash messages
Instances
HasField "frameworkConfig" ControllerContext FrameworkConfig Source # | |
Defined in IHP.Controller.Context | |
HasField "logger" ControllerContext Logger Source # | |
Defined in IHP.Controller.Context getField :: ControllerContext -> Logger # |
class InitControllerContext (application :: k) where Source #
Nothing
initContext :: IO () Source #
Instances
InitControllerContext () Source # | |
Defined in IHP.ControllerSupport initContext :: IO () Source # |
runActionWithNewContext :: forall application controller. (Controller controller, ?applicationContext :: ApplicationContext, ?context :: RequestContext, InitControllerContext application, ?application :: application, Typeable application, Typeable controller) => controller -> IO ResponseReceived Source #
newContextForAction :: forall application controller. (Controller controller, ?applicationContext :: ApplicationContext, ?context :: RequestContext, InitControllerContext application, ?application :: application, Typeable application, Typeable controller) => controller -> IO (Either (IO ResponseReceived) ControllerContext) Source #
respondAndExit :: (?context :: ControllerContext) => Response -> IO () Source #
jumpToAction :: (Controller action, ?context :: ControllerContext, ?modelContext :: ModelContext) => action -> IO () Source #
requestBodyJSON :: (?context :: ControllerContext) => Value Source #
startWebSocketApp :: forall webSocketApp application. (?applicationContext :: ApplicationContext, ?context :: RequestContext, InitControllerContext application, ?application :: application, Typeable application, WSApp webSocketApp) => IO ResponseReceived -> Application Source #
startWebSocketAppAndFailOnHTTP :: forall webSocketApp application. (?applicationContext :: ApplicationContext, ?context :: RequestContext, InitControllerContext application, ?application :: application, Typeable application, WSApp webSocketApp) => Application Source #
setHeader :: (?context :: ControllerContext) => 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 @StripePublicKey
StripePublicKey "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 stripePublicKey
Then you can access it using getAppConfig
:
action MyAction = do let (StripePublicKey stripePublicKey) = getAppConfig @StripePublicKey putStrLn ("Stripe public key: " <> stripePublicKey)