Safe Haskell | None |
---|---|
Language | Haskell2010 |
Extensions |
|
Okapi is a micro web framework.
Synopsis
- type MonadOkapi m = (Functor m, Applicative m, Alternative m, Monad m, MonadPlus m, MonadError Failure m, MonadState State m)
- newtype OkapiT m a = OkapiT {}
- data Failure
- data State = State {
- stateRequest :: Request
- stateVault :: Vault
- data Request
- type Method = Maybe ByteString
- type Path = [Text]
- type Query = [QueryItem]
- type QueryItem = (Text, QueryValue)
- data QueryValue
- type Body = ByteString
- type Headers = [Header]
- type Header = (HeaderName, ByteString)
- type HeaderName = HeaderName
- type Cookie = [Crumb]
- type Crumb = (ByteString, ByteString)
- request :: MonadOkapi m => m Request
- requestEnd :: MonadOkapi m => m ()
- method :: MonadOkapi m => m Method
- methodGET :: MonadOkapi m => m ()
- methodPOST :: MonadOkapi m => m ()
- methodHEAD :: MonadOkapi m => m ()
- methodPUT :: MonadOkapi m => m ()
- methodPATCH :: MonadOkapi m => m ()
- methodDELETE :: MonadOkapi m => m ()
- methodOPTIONS :: MonadOkapi m => m ()
- methodTRACE :: MonadOkapi m => m ()
- methodCONNECT :: MonadOkapi m => m ()
- methodEnd :: MonadOkapi m => m ()
- path :: MonadOkapi m => m [Text]
- pathParam :: (FromHttpApiData a, MonadOkapi m) => m a
- pathEnd :: MonadOkapi m => m ()
- query :: MonadOkapi m => m Query
- queryValue :: MonadOkapi m => Text -> m QueryValue
- queryFlag :: MonadOkapi m => Text -> m ()
- queryParam :: (FromHttpApiData a, MonadOkapi m) => Text -> m a
- queryList :: (FromHttpApiData a, MonadOkapi m) => Text -> m (NonEmpty a)
- queryEnd :: MonadOkapi m => m ()
- body :: MonadOkapi m => m Body
- bodyJSON :: (FromJSON a, MonadOkapi m) => m a
- bodyForm :: (FromForm a, MonadOkapi m) => m a
- bodyEnd :: MonadOkapi m => m ()
- headers :: MonadOkapi m => m Headers
- header :: MonadOkapi m => HeaderName -> m ByteString
- basicAuth :: MonadOkapi m => m (Text, Text)
- headersEnd :: MonadOkapi m => m ()
- cookie :: MonadOkapi m => m Cookie
- cookieCrumb :: MonadOkapi m => ByteString -> m ByteString
- cookieEnd :: MonadOkapi m => m ()
- vaultLookup :: MonadOkapi m => Key a -> m a
- vaultInsert :: MonadOkapi m => Key a -> a -> m ()
- vaultDelete :: MonadOkapi m => Key a -> m ()
- vaultAdjust :: MonadOkapi m => (a -> a) -> Key a -> m ()
- vaultWipe :: MonadOkapi m => m ()
- is :: (Eq a, MonadOkapi m) => m a -> a -> m ()
- satisfies :: (Eq a, MonadOkapi m) => m a -> (a -> Bool) -> m ()
- look :: MonadOkapi m => m a -> m a
- next :: MonadOkapi m => m a
- throw :: MonadOkapi m => Response -> m a
- (<!>) :: MonadOkapi m => m a -> m a -> m a
- guardThrow :: MonadOkapi m => Response -> Bool -> m ()
- type Handler m = m Response
- data Response = Response {}
- type Status = Natural
- data ResponseBody
- ok :: Response
- notFound :: Response
- redirect :: Status -> Text -> Response
- forbidden :: Response
- internalServerError :: Response
- setStatus :: Status -> Response -> Response
- setHeaders :: Headers -> Response -> Response
- setHeader :: Header -> Response -> Response
- addHeader :: Header -> Response -> Response
- addSetCookie :: (ByteString, ByteString) -> Response -> Response
- setBody :: ResponseBody -> Response -> Response
- setBodyRaw :: ByteString -> Response -> Response
- setBodyFile :: FilePath -> Response -> Response
- setBodyEventSource :: EventSource -> Response -> Response
- setPlaintext :: Text -> Response -> Response
- setHTML :: ByteString -> Response -> Response
- setJSON :: ToJSON a => a -> Response -> Response
- static :: MonadOkapi m => Handler m
- type Middleware m = Handler m -> Handler m
- applyMiddlewares :: MonadOkapi m => [Middleware m] -> Middleware m
- scope :: MonadOkapi m => Path -> [Middleware m] -> Middleware m
- clearHeadersMiddleware :: MonadOkapi m => Middleware m
- prefixPathMiddleware :: MonadOkapi m => Path -> Middleware m
- type Router m a = m a -> (a -> Handler m) -> Handler m
- route :: MonadOkapi m => Router m a
- pattern PathParam :: (ToHttpApiData a, FromHttpApiData a) => a -> Text
- pattern GET :: Method
- pattern POST :: Method
- pattern DELETE :: Method
- pattern PUT :: Method
- pattern PATCH :: Method
- pattern IsQueryParam :: (ToHttpApiData a, FromHttpApiData a) => a -> QueryValue
- pattern HasQueryFlag :: Maybe QueryValue
- viewQuery :: Text -> Query -> (Maybe QueryValue, Query)
- viewQueryParam :: FromHttpApiData a => Text -> Query -> (Maybe a, Query)
- data RelURL = RelURL Path Query
- renderRelURL :: RelURL -> Text
- renderPath :: Path -> Text
- renderQuery :: Query -> Text
- parseRelURL :: Text -> Maybe RelURL
- testParser :: Monad m => OkapiT m Response -> Request -> m (Either Failure Response, State)
- testParserPure :: OkapiT Identity Response -> Request -> Identity (Either Failure Response, State)
- testParserIO :: OkapiT IO Response -> Request -> IO (Either Failure Response, State)
- assert :: ((Either Failure Response, State) -> Bool) -> (Either Failure Response, State) -> Bool
- assert200 :: (Either Failure Response, State) -> Bool
- assert404 :: (Either Failure Response, State) -> Bool
- assert500 :: (Either Failure Response, State) -> Bool
- run :: Monad m => (forall a. m a -> IO a) -> OkapiT m Response -> IO ()
- serve :: Monad m => Int -> Response -> (forall a. m a -> IO a) -> OkapiT m Response -> IO ()
- serveTLS :: Monad m => TLSSettings -> Settings -> Response -> (forall a. m a -> IO a) -> OkapiT m Response -> IO ()
- serveWebsockets :: Monad m => ConnectionOptions -> ServerApp -> Int -> Response -> (forall a. m a -> IO a) -> OkapiT m Response -> IO ()
- serveWebsocketsTLS :: Monad m => TLSSettings -> Settings -> ConnectionOptions -> ServerApp -> Response -> (forall a. m a -> IO a) -> OkapiT m Response -> IO ()
- app :: Monad m => Response -> (forall a. m a -> IO a) -> OkapiT m Response -> Application
- websocketsApp :: Monad m => ConnectionOptions -> ServerApp -> Response -> (forall a. m a -> IO a) -> OkapiT m Response -> Application
- testRunSession :: Monad m => Session a -> (forall a. m a -> IO a) -> OkapiT m Response -> IO a
- testWithSession :: Monad m => (forall a. m a -> IO a) -> OkapiT m Response -> Session a -> IO a
- testRequest :: Request -> Session SResponse
- data Event
- = Event { }
- | CommentEvent ByteString
- | CloseEvent
- class ToSSE a where
- type EventSource = Chan Event
- newEventSource :: IO EventSource
- sendValue :: ToSSE a => EventSource -> a -> IO ()
- sendEvent :: EventSource -> Event -> IO ()
- type Session = Map ByteString ByteString
- class Monad m => HasSession m where
- sessionSecret :: m ByteString
- getSession :: m (Maybe Session)
- putSession :: Session -> m ()
- session :: (MonadOkapi m, HasSession m) => m Session
- sessionLookup :: HasSession m => MonadOkapi m => ByteString -> m ByteString
- sessionInsert :: HasSession m => MonadOkapi m => ByteString -> ByteString -> m ()
- sessionDelete :: HasSession m => MonadOkapi m => ByteString -> m ()
- sessionClear :: HasSession m => m ()
- withSession :: (MonadOkapi m, HasSession m) => Middleware m
Parsing
These are the parsers that you'll use to build you own app.
type MonadOkapi m = (Functor m, Applicative m, Alternative m, Monad m, MonadPlus m, MonadError Failure m, MonadState State m) Source #
A type constraint representing monads that have the ability to parse an HTTP request.
A concrete implementation of the MonadOkapi
type constraint.
Instances
MonadTrans OkapiT Source # | |
Monad m => MonadState State (OkapiT m) Source # | |
MonadReader r m => MonadReader r (OkapiT m) Source # | |
Monad m => MonadError Failure (OkapiT m) Source # | |
Monad m => Monad (OkapiT m) Source # | |
Functor m => Functor (OkapiT m) Source # | |
Monad m => Applicative (OkapiT m) Source # | |
MonadIO m => MonadIO (OkapiT m) Source # | |
Monad m => Alternative (OkapiT m) Source # | |
Monad m => MonadPlus (OkapiT m) Source # | |
MFunctor OkapiT Source # | |
Represents the two variants of failure that can occur when parsing a HTTP request.
Represents the state of a parser. Set on every request to the Okapi server.
State | |
|
Represents the HTTP request being parsed.
type Method = Maybe ByteString Source #
type QueryItem = (Text, QueryValue) Source #
data QueryValue Source #
Instances
Eq QueryValue Source # | |
Defined in Okapi (==) :: QueryValue -> QueryValue -> Bool # (/=) :: QueryValue -> QueryValue -> Bool # | |
Show QueryValue Source # | |
Defined in Okapi showsPrec :: Int -> QueryValue -> ShowS # show :: QueryValue -> String # showList :: [QueryValue] -> ShowS # |
type Body = ByteString Source #
type Header = (HeaderName, ByteString) Source #
type HeaderName = HeaderName Source #
type Crumb = (ByteString, ByteString) Source #
Request Parsers
request :: MonadOkapi m => m Request Source #
Parses the entire request.
requestEnd :: MonadOkapi m => m () Source #
Method Parsers
method :: MonadOkapi m => m Method Source #
methodGET :: MonadOkapi m => m () Source #
methodPOST :: MonadOkapi m => m () Source #
methodHEAD :: MonadOkapi m => m () Source #
methodPUT :: MonadOkapi m => m () Source #
methodPATCH :: MonadOkapi m => m () Source #
methodDELETE :: MonadOkapi m => m () Source #
methodOPTIONS :: MonadOkapi m => m () Source #
methodTRACE :: MonadOkapi m => m () Source #
methodCONNECT :: MonadOkapi m => m () Source #
methodEnd :: MonadOkapi m => m () Source #
Path Parsers
These are the path parsers.
path :: MonadOkapi m => m [Text] Source #
Parses and discards mutiple path segments matching the values and order of the given [Text]
value
pathParam :: (FromHttpApiData a, MonadOkapi m) => m a Source #
Parses and discards a single path segment matching the given Text
value
pathEnd :: MonadOkapi m => m () Source #
Similar to end
function in https://github.com/purescript-contrib/purescript-routing/blob/main/GUIDE.md
Query Parsers
These are the query parsers.
query :: MonadOkapi m => m Query Source #
queryValue :: MonadOkapi m => Text -> m QueryValue Source #
queryFlag :: MonadOkapi m => Text -> m () Source #
Test for the existance of a query flag
queryParam :: (FromHttpApiData a, MonadOkapi m) => Text -> m a Source #
Parses the value of a query parameter with the given type and name
queryEnd :: MonadOkapi m => m () Source #
Body Parsers
body :: MonadOkapi m => m Body Source #
bodyJSON :: (FromJSON a, MonadOkapi m) => m a Source #
bodyForm :: (FromForm a, MonadOkapi m) => m a Source #
bodyEnd :: MonadOkapi m => m () Source #
Header Parsers
These are header parsers.
headers :: MonadOkapi m => m Headers Source #
header :: MonadOkapi m => HeaderName -> m ByteString Source #
headersEnd :: MonadOkapi m => m () Source #
cookie :: MonadOkapi m => m Cookie Source #
cookieCrumb :: MonadOkapi m => ByteString -> m ByteString Source #
cookieEnd :: MonadOkapi m => m () Source #
Vault Parsers
vaultLookup :: MonadOkapi m => Key a -> m a Source #
vaultInsert :: MonadOkapi m => Key a -> a -> m () Source #
vaultDelete :: MonadOkapi m => Key a -> m () Source #
vaultAdjust :: MonadOkapi m => (a -> a) -> Key a -> m () Source #
vaultWipe :: MonadOkapi m => m () Source #
Combinators
is :: (Eq a, MonadOkapi m) => m a -> a -> m () Source #
look :: MonadOkapi m => m a -> m a Source #
Parses without modifying the state, even if it succeeds.
Failure
next :: MonadOkapi m => m a Source #
throw :: MonadOkapi m => Response -> m a Source #
(<!>) :: MonadOkapi m => m a -> m a -> m a Source #
guardThrow :: MonadOkapi m => Response -> Bool -> m () Source #
Responding
Represents HTTP responses that can be returned by a parser.
data ResponseBody Source #
Represents the body of an HTTP response.
Values
Setters
addSetCookie :: (ByteString, ByteString) -> Response -> Response Source #
setBodyRaw :: ByteString -> Response -> Response Source #
setBodyEventSource :: EventSource -> Response -> Response Source #
Special
static :: MonadOkapi m => Handler m Source #
Middleware
Middlewares allow you to modify the behavior of Okapi handlers.
Middlewares are functions that take a handler and return another handler.
Middlewares can be composed with the fish operator >=>
.
clearHeadersMiddleware >=> pathPrefix ["jello"] :: forall m. Middleware m
type Middleware m = Handler m -> Handler m Source #
A middleware takes an action that returns a Response
and can modify the action in various ways
applyMiddlewares :: MonadOkapi m => [Middleware m] -> Middleware m Source #
scope :: MonadOkapi m => Path -> [Middleware m] -> Middleware m Source #
clearHeadersMiddleware :: MonadOkapi m => Middleware m Source #
prefixPathMiddleware :: MonadOkapi m => Path -> Middleware m Source #
Routing
Okapi implements routes and type-safe relative URLs using bidirectional pattern synonyms and view patterns.
Routing can be extended to dispatch on any property of the request, including method, path, query, headers, and even body.
By default, Okapi provides a route
function for dispatching on the path of the request.
route :: MonadOkapi m => Router m a Source #
pattern IsQueryParam :: (ToHttpApiData a, FromHttpApiData a) => a -> QueryValue Source #
pattern HasQueryFlag :: Maybe QueryValue Source #
Relative URLs
Relative URLs are useful when we want to refer to other locations within our app. Thanks to bidirectional patterns, we can use the same pattern to deconstruct an incoming request AND construct the relative URL that leads to itself.
renderRelURL :: RelURL -> Text Source #
renderPath :: Path -> Text Source #
renderQuery :: Query -> Text Source #
Testing
There are two ways to test in Okapi.
testParser :: Monad m => OkapiT m Response -> Request -> m (Either Failure Response, State) Source #
testParserPure :: OkapiT Identity Response -> Request -> Identity (Either Failure Response, State) Source #
assert :: ((Either Failure Response, State) -> Bool) -> (Either Failure Response, State) -> Bool Source #
WAI
These functions are for interfacing with WAI (Web Application Interface).
serveTLS :: Monad m => TLSSettings -> Settings -> Response -> (forall a. m a -> IO a) -> OkapiT m Response -> IO () Source #
serveWebsockets :: Monad m => ConnectionOptions -> ServerApp -> Int -> Response -> (forall a. m a -> IO a) -> OkapiT m Response -> IO () Source #
serveWebsocketsTLS :: Monad m => TLSSettings -> Settings -> ConnectionOptions -> ServerApp -> Response -> (forall a. m a -> IO a) -> OkapiT m Response -> IO () Source #
:: Monad m | |
=> Response | The default response to pure if parser fails |
-> (forall a. m a -> IO a) | Function for "unlifting" monad inside |
-> OkapiT m Response | The parser used to equals the request |
-> Application |
Turns a parser into a WAI application
:: Monad m | |
=> ConnectionOptions | Connection options configuration for the WebSocket server |
-> ServerApp | The server to use for handling WebSocket connections |
-> Response | |
-> (forall a. m a -> IO a) | |
-> OkapiT m Response | |
-> Application |
Turns a parsers into a WAI application with WebSocket functionality See __ for information on how to create a WebSocket server
testRunSession :: Monad m => Session a -> (forall a. m a -> IO a) -> OkapiT m Response -> IO a Source #
testWithSession :: Monad m => (forall a. m a -> IO a) -> OkapiT m Response -> Session a -> IO a Source #
testRequest :: Request -> Session SResponse Source #
Utilities
Server Sent Events
type EventSource = Chan Event Source #
Sessions
type Session = Map ByteString ByteString Source #
class Monad m => HasSession m where Source #
sessionSecret :: m ByteString Source #
getSession :: m (Maybe Session) Source #
putSession :: Session -> m () Source #
session :: (MonadOkapi m, HasSession m) => m Session Source #
sessionLookup :: HasSession m => MonadOkapi m => ByteString -> m ByteString Source #
sessionInsert :: HasSession m => MonadOkapi m => ByteString -> ByteString -> m () Source #
sessionDelete :: HasSession m => MonadOkapi m => ByteString -> m () Source #
sessionClear :: HasSession m => m () Source #
withSession :: (MonadOkapi m, HasSession m) => Middleware m Source #