Copyright | (c) 2019 Daniel YU |
---|---|
License | BSD3 |
Maintainer | leptonyu@gmail.com |
Stability | experimental |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
A out-of-the-box wrapper of servant,
providing configuration loader salak and flexible extension with AppMiddleware
.
Synopsis
- start :: forall api cxt. (HasServer api cxt, HasSwagger api) => AppConfig -> SwaggerConfig -> Version -> IO LogConfig -> (Span -> AppV cxt IO ()) -> AppMiddleware Simple cxt -> (AppConfig -> Application -> IO ()) -> Proxy api -> ServerT api (AppV cxt IO) -> IO ()
- serveWarp :: AppConfig -> Application -> IO ()
- data AppConfig = AppConfig {}
- data AppT cxt m a
- type AppV cxt = AppT (VaultHolder ': cxt)
- type AppIO cxt = AppT cxt IO
- type AppSimple = AppV Simple IO
- type Simple = '[LogFuncHolder]
- runAppT :: Context cxt -> AppT cxt m a -> m a
- runVault :: MonadIO m => Context cxt -> Vault -> AppV cxt IO a -> m a
- throwS :: (HasCallStack, MonadIO m, MonadLogger m) => ServerError -> Text -> m a
- newtype AppMiddleware a b = AppMiddleware {
- runAM :: Context a -> Middleware -> (Context b -> Middleware -> LoggingT IO ()) -> LoggingT IO ()
- emptyAM :: AppMiddleware cxt cxt
- simpleContext :: a -> AppMiddleware cxt (a ': cxt)
- simpleConfig :: (HasSalak cxt, FromProp a) => Text -> AppMiddleware cxt (a ': cxt)
- simpleConfig' :: (HasSalak cxt, FromProp a) => Text -> (a -> AppT cxt (LoggingT IO) b) -> AppMiddleware cxt (b ': cxt)
- simpleMiddleware :: Middleware -> AppMiddleware cxt cxt
- data LogConfig = LogConfig {
- bufferSize :: Word16
- file :: FilePath
- maxSize :: Word32
- rotateHistory :: Word16
- level :: LogLevel
- type HasLogger cxt = (HasContextEntry cxt LogFuncHolder, TryContextEntry cxt VaultHolder)
- data LogFuncHolder
- data VaultHolder
- data Context (contextTypes :: [Type]) where
- class HasContextEntry (context :: [Type]) val where
- getContextEntry :: Context context -> val
- class TryContextEntry (cxt :: [*]) (entry :: *) where
- tryContextEntry :: Context cxt -> Maybe entry
- getEntry :: (HasContextEntry cxt entry, Monad m) => AppT cxt m entry
- tryEntry :: (TryContextEntry cxt entry, Monad m) => AppT cxt m (Maybe entry)
- data SwaggerConfig = SwaggerConfig {}
- serveWithContextAndSwagger :: forall api context. (HasSwagger api, HasServer api context) => SwaggerConfig -> (Swagger -> Swagger) -> Proxy api -> Context context -> ServerT api Handler -> Application
- baseInfo :: String -> Text -> Version -> Int -> Swagger -> Swagger
- spanNoNotifier :: Span -> AppV cxt IO ()
- data Span = Span {
- spanId :: ByteString
- name :: Text
- startTime :: Int
- finishTime :: Maybe Int
- tags :: HashMap Text SpanTag
- logs :: HashMap Text Text
- context :: SpanContext
- references :: [SpanReference]
- data SpanContext = SpanContext {}
- data SpanTag
- data SpanReference = SpanReference {
- referenceType :: SpanReferenceType
- parentId :: ByteString
- showText :: Show a => a -> Text
- randomString :: IO ByteString
- randomCode :: Vector Char -> Int -> IO String
- decodeUtf8 :: ByteString -> Text
- encodeUtf8 :: Text -> ByteString
- pack :: String -> Text
- liftIO :: MonadIO m => IO a -> m a
- fromMaybe :: a -> Maybe a -> a
- throw :: Exception e => e -> a
How to use this library
import Salak import Salak.Yaml import Servant import Yam import qualified Control.Category as C import Data.Version type API = "hello" :> Get '[PlainText] Text service :: ServerT API AppSimple service = return "world" main = runSalakWith "app" YAML $ do al <- require "yam.application" sw <- require "yam.swagger" lc <- requireD "yam.logging" start al sw (makeVersion []) lc spanNoNotifier emptyAM serveWarp (Proxy @API) service
Yam Server
:: (HasServer api cxt, HasSwagger api) | |
=> AppConfig | Application Config |
-> SwaggerConfig | SwaggerConfig |
-> Version | Application Version |
-> IO LogConfig | Logger Config |
-> (Span -> AppV cxt IO ()) | Opentracing notifier |
-> AppMiddleware Simple cxt | Application Middleware |
-> (AppConfig -> Application -> IO ()) | Run Application |
-> Proxy api | Application API Proxy |
-> ServerT api (AppV cxt IO) | Application API Server |
-> IO () |
Standard Starter of Yam.
Application Configuration
Application Configuration.
Application Context
Application Context Monad.
Instances
MonadTrans (AppT cxt) Source # | |
Monad m => MonadReader (Context cxt) (AppT cxt m) Source # | |
Monad m => Monad (AppT cxt m) Source # | |
Functor m => Functor (AppT cxt m) Source # | |
Applicative m => Applicative (AppT cxt m) Source # | |
MonadIO m => MonadIO (AppT cxt m) Source # | |
MonadUnliftIO m => MonadUnliftIO (AppT cxt m) Source # | |
(HasLogger cxt, MonadIO m) => HasValid (AppT cxt m) Source # | |
(HasLogger cxt, MonadIO m) => MonadLogger (AppT cxt m) Source # | |
(HasLogger cxt, MonadIO m) => MonadLoggerIO (AppT cxt m) Source # | |
(HasContextEntry cxt SourcePack, Monad m) => HasSourcePack (AppT cxt m) Source # | |
Defined in Yam.App askSourcePack :: AppT cxt m SourcePack # |
type Simple = '[LogFuncHolder] Source #
Simple Application context
runVault :: MonadIO m => Context cxt -> Vault -> AppV cxt IO a -> m a Source #
Run Application with Vault
.
:: (HasCallStack, MonadIO m, MonadLogger m) | |
=> ServerError | Server error |
-> Text | message |
-> m a |
throw ServerError
with message
Application Middleware
newtype AppMiddleware a b Source #
Application Middleware.
AppMiddleware | |
|
Instances
Category AppMiddleware Source # | |
Defined in Yam id :: AppMiddleware a a # (.) :: AppMiddleware b c -> AppMiddleware a b -> AppMiddleware a c # |
emptyAM :: AppMiddleware cxt cxt Source #
Empty Application Middleware.
simpleContext :: a -> AppMiddleware cxt (a ': cxt) Source #
Simple Application Middleware, just provide a config to context.
simpleConfig :: (HasSalak cxt, FromProp a) => Text -> AppMiddleware cxt (a ': cxt) Source #
Simple Application Middleware, just provide a config to context.
simpleConfig' :: (HasSalak cxt, FromProp a) => Text -> (a -> AppT cxt (LoggingT IO) b) -> AppMiddleware cxt (b ': cxt) Source #
Simple Application Middleware, just provide a config to context.
simpleMiddleware :: Middleware -> AppMiddleware cxt cxt Source #
Simple Application Middleware, promote a Middleware
to AppMiddleware
Modules
Logger
Logger config
LogConfig | |
|
type HasLogger cxt = (HasContextEntry cxt LogFuncHolder, TryContextEntry cxt VaultHolder) Source #
Context with logger.
data LogFuncHolder Source #
Holder for LogFunc
data VaultHolder Source #
Holder for Vault
Context
data Context (contextTypes :: [Type]) where #
Context
s are used to pass values to combinators. (They are not meant
to be used to pass parameters to your handlers, i.e. they should not replace
any custom ReaderT
-monad-stack that you're using
with hoistServer
.) If you don't use combinators that
require any context entries, you can just use serve
as always.
If you are using combinators that require a non-empty Context
you have to
use serveWithContext
and pass it a Context
that contains all
the values your combinators need. A Context
is essentially a heterogenous
list and accessing the elements is being done by type (see getContextEntry
).
The parameter of the type Context
is a type-level list reflecting the types
of the contained context entries. To create a Context
with entries, use the
operator (
::.
)
>>>
:type True :. () :. EmptyContext
True :. () :. EmptyContext :: Context '[Bool, ()]
EmptyContext :: forall (contextTypes :: [Type]). Context ([] :: [Type]) | |
(:.) :: forall (contextTypes :: [Type]) x (xs :: [Type]). x -> Context xs -> Context (x ': xs) infixr 5 |
class HasContextEntry (context :: [Type]) val where #
This class is used to access context entries in Context
s. getContextEntry
returns the first value where the type matches:
>>>
getContextEntry (True :. False :. EmptyContext) :: Bool
True
If the Context
does not contain an entry of the requested type, you'll get
an error:
>>>
getContextEntry (True :. False :. EmptyContext) :: String
... ...No instance for (HasContextEntry '[] [Char]) ...
getContextEntry :: Context context -> val #
Instances
HasContextEntry xs val => HasContextEntry (notIt ': xs) val | |
Defined in Servant.Server.Internal.Context getContextEntry :: Context (notIt ': xs) -> val # | |
HasContextEntry (val ': xs) val | |
Defined in Servant.Server.Internal.Context getContextEntry :: Context (val ': xs) -> val # |
class TryContextEntry (cxt :: [*]) (entry :: *) where Source #
This class provide a optional supports for get entry from Context
.
tryContextEntry :: Context cxt -> Maybe entry Source #
Instances
TryContextEntry a entry Source # | |
Defined in Yam.Prelude tryContextEntry :: Context a -> Maybe entry Source # | |
TryContextEntry (entry ': as) entry Source # | |
Defined in Yam.Prelude tryContextEntry :: Context (entry ': as) -> Maybe entry Source # | |
TryContextEntry as entry => TryContextEntry (a ': as) entry Source # | |
Defined in Yam.Prelude tryContextEntry :: Context (a ': as) -> Maybe entry Source # |
tryEntry :: (TryContextEntry cxt entry, Monad m) => AppT cxt m (Maybe entry) Source #
Try get entry from AppT
Swagger
data SwaggerConfig Source #
Swagger Configuration
Instances
Eq SwaggerConfig Source # | |
Defined in Yam.Swagger (==) :: SwaggerConfig -> SwaggerConfig -> Bool # (/=) :: SwaggerConfig -> SwaggerConfig -> Bool # | |
Show SwaggerConfig Source # | |
Defined in Yam.Swagger showsPrec :: Int -> SwaggerConfig -> ShowS # show :: SwaggerConfig -> String # showList :: [SwaggerConfig] -> ShowS # | |
FromProp SwaggerConfig Source # | |
Defined in Yam.Swagger |
serveWithContextAndSwagger Source #
:: (HasSwagger api, HasServer api context) | |
=> SwaggerConfig | Swagger configuration. |
-> (Swagger -> Swagger) | Swagger modification. |
-> Proxy api | Application API Proxy. |
-> Context context | Application context. |
-> ServerT api Handler | Application API Server |
-> Application |
Serve with swagger.
:: String | Hostname |
-> Text | Server Name |
-> Version | Server version |
-> Int | Port |
-> Swagger | Old swagger |
-> Swagger |
Swagger modification
Reexport
Span | |
|
data SpanContext Source #
Instances
Eq SpanContext Source # | |
Defined in Data.Opentracing.Types (==) :: SpanContext -> SpanContext -> Bool # (/=) :: SpanContext -> SpanContext -> Bool # | |
Show SpanContext Source # | |
Defined in Data.Opentracing.Types showsPrec :: Int -> SpanContext -> ShowS # show :: SpanContext -> String # showList :: [SpanContext] -> ShowS # |
data SpanReference Source #
SpanReference | |
|
Instances
Eq SpanReference Source # | |
Defined in Data.Opentracing.Types (==) :: SpanReference -> SpanReference -> Bool # (/=) :: SpanReference -> SpanReference -> Bool # | |
Show SpanReference Source # | |
Defined in Data.Opentracing.Types showsPrec :: Int -> SpanReference -> ShowS # show :: SpanReference -> String # showList :: [SpanReference] -> ShowS # |
randomString :: IO ByteString Source #
Utility
decodeUtf8 :: ByteString -> Text #
Decode a ByteString
containing UTF-8 encoded text that is known
to be valid.
If the input contains any invalid UTF-8 data, an exception will be
thrown that cannot be caught in pure code. For more control over
the handling of invalid data, use decodeUtf8'
or
decodeUtf8With
.
encodeUtf8 :: Text -> ByteString #
Encode text using UTF-8 encoding.
fromMaybe :: a -> Maybe a -> a #
The fromMaybe
function takes a default value and and Maybe
value. If the Maybe
is Nothing
, it returns the default values;
otherwise, it returns the value contained in the Maybe
.
Examples
Basic usage:
>>>
fromMaybe "" (Just "Hello, World!")
"Hello, World!"
>>>
fromMaybe "" Nothing
""
Read an integer from a string using readMaybe
. If we fail to
parse an integer, we want to return 0
by default:
>>>
import Text.Read ( readMaybe )
>>>
fromMaybe 0 (readMaybe "5")
5>>>
fromMaybe 0 (readMaybe "")
0