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 file api cxt. (HasLoad file, HasLogger cxt, HasServer api cxt, HasSwagger api) => String -> file -> Version -> RunSalak (AppMiddleware Simple cxt) -> Proxy api -> RunSalak (ServerT api (AppV cxt IO)) -> IO ()
- start' :: forall api cxt. (HasLogger cxt, HasServer api cxt, HasSwagger api) => LoadSalakT IO () -> Version -> RunSalak (AppMiddleware Simple cxt) -> Proxy api -> RunSalak (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 -> IO HealthResult -> (Context b -> Middleware -> IO HealthResult -> 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 HealthStatus
- data HealthResult = HealthResult {}
- mergeHealth :: IO HealthStatus -> Text -> IO HealthResult -> IO HealthResult
- 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
- data SwaggerTag (name :: Symbol) (desp :: Symbol)
- 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
- logInfo :: (HasCallStack, MonadLogger m) => Text -> m ()
- logError :: (HasCallStack, MonadLogger m) => Text -> m ()
- logWarn :: (HasCallStack, MonadLogger m) => Text -> m ()
- logDebug :: (HasCallStack, MonadLogger m) => Text -> m ()
- newtype RunSalak a = RunSalak {}
How to use this library
import Salak.Yaml import Servant import Yam import Data.Version type API = "hello" :> Get '[PlainText] Text service :: ServerT API AppSimple service = return "world" main = start "app" YAML (makeVersion []) (return emptyAM) (Proxy @API) (return service)
Yam Server
:: (HasLoad file, HasLogger cxt, HasServer api cxt, HasSwagger api) | |
=> String | File config name |
-> file | Config file format |
-> Version | Version |
-> RunSalak (AppMiddleware Simple cxt) | Application Middleware |
-> Proxy api | Application API Proxy |
-> RunSalak (ServerT api (AppV cxt IO)) | Application API Server |
-> IO () |
Standard Starter of Yam.
start' :: forall api cxt. (HasLogger cxt, HasServer api cxt, HasSwagger api) => LoadSalakT IO () -> Version -> RunSalak (AppMiddleware Simple cxt) -> Proxy api -> RunSalak (ServerT api (AppV cxt IO)) -> IO () Source #
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 # | |
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.Middleware 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
Health
data HealthStatus Source #
Instances
Eq HealthStatus Source # | |
Defined in Yam.Server.Health (==) :: HealthStatus -> HealthStatus -> Bool # (/=) :: HealthStatus -> HealthStatus -> Bool # | |
Show HealthStatus Source # | |
Defined in Yam.Server.Health showsPrec :: Int -> HealthStatus -> ShowS # show :: HealthStatus -> String # showList :: [HealthStatus] -> ShowS # | |
Generic HealthStatus Source # | |
Defined in Yam.Server.Health type Rep HealthStatus :: Type -> Type # from :: HealthStatus -> Rep HealthStatus x # to :: Rep HealthStatus x -> HealthStatus # | |
ToJSON HealthStatus Source # | |
Defined in Yam.Server.Health toJSON :: HealthStatus -> Value # toEncoding :: HealthStatus -> Encoding # toJSONList :: [HealthStatus] -> Value # toEncodingList :: [HealthStatus] -> Encoding # | |
ToSchema HealthStatus Source # | |
Defined in Yam.Server.Health declareNamedSchema :: proxy HealthStatus -> Declare (Definitions Schema) NamedSchema # | |
type Rep HealthStatus Source # | |
data HealthResult Source #
Instances
mergeHealth :: IO HealthStatus -> Text -> IO HealthResult -> IO HealthResult Source #
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
data SwaggerTag (name :: Symbol) (desp :: Symbol) Source #
Instances
HasClient m api => HasClient m (SwaggerTag name desp :> api) Source # | |
Defined in Yam.Swagger type Client m (SwaggerTag name desp :> api) :: Type # clientWithRoute :: Proxy m -> Proxy (SwaggerTag name desp :> api) -> Request -> Client m (SwaggerTag name desp :> api) # hoistClientMonad :: Proxy m -> Proxy (SwaggerTag name desp :> api) -> (forall x. mon x -> mon' x) -> Client mon (SwaggerTag name desp :> api) -> Client mon' (SwaggerTag name desp :> api) # | |
(HasSwagger api, KnownSymbol name, KnownSymbol desp) => HasSwagger (SwaggerTag name desp :> api :: Type) Source # | |
Defined in Yam.Swagger | |
HasServer api ctx => HasServer (SwaggerTag name desp :> api :: Type) ctx Source # | |
Defined in Yam.Swagger type ServerT (SwaggerTag name desp :> api) m :: Type # route :: Proxy (SwaggerTag name desp :> api) -> Context ctx -> Delayed env (Server (SwaggerTag name desp :> api)) -> Router env # hoistServerWithContext :: Proxy (SwaggerTag name desp :> api) -> Proxy ctx -> (forall x. m x -> n x) -> ServerT (SwaggerTag name desp :> api) m -> ServerT (SwaggerTag name desp :> api) n # | |
type Client m (SwaggerTag name desp :> api) Source # | |
Defined in Yam.Swagger | |
type ServerT (SwaggerTag name desp :> api :: Type) m Source # | |
Defined in Yam.Swagger |
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
throw :: Exception e => e -> a #
Throw an exception. Exceptions may be thrown from purely
functional code, but may only be caught within the IO
monad.
logInfo :: (HasCallStack, MonadLogger m) => Text -> m () #
See logDebug
Since: monad-logger-0.3.19
logError :: (HasCallStack, MonadLogger m) => Text -> m () #
See logDebug
Since: monad-logger-0.3.19
logWarn :: (HasCallStack, MonadLogger m) => Text -> m () #
See logDebug
Since: monad-logger-0.3.19
logDebug :: (HasCallStack, MonadLogger m) => Text -> m () #
Logs a message with the location provided by
an implicit CallStack
.
Since: monad-logger-0.3.19
Instances
Monad RunSalak Source # | |
Functor RunSalak Source # | |
Applicative RunSalak Source # | |
MonadIO RunSalak Source # | |
Defined in Yam.Prelude | |
MonadUnliftIO RunSalak Source # | |
Defined in Yam.Prelude | |
MonadLogger RunSalak Source # | |
Defined in Yam.Prelude | |
MonadLoggerIO RunSalak Source # | |
Defined in Yam.Prelude | |
HasSourcePack RunSalak Source # | |
Defined in Yam.Prelude |