{-# LINE 1 "src/Database/EJDB2/HTTP.hsc" #-}
{-# LANGUAGE CPP #-}

module Database.EJDB2.HTTP
        ( Options(..)
        , zero
        , OptionsB
        , options
        , build
        ) where

import           Foreign
import           Foreign.C.String
import           Foreign.C.Types
import           Foreign.Marshal.Utils
import           Data.Int



-- | EJDB HTTP\/Websocket Server options.
data Options = Options { enabled :: !Bool -- ^ If HTTP\/Websocket endpoint enabled. Default: false
                       , port :: !Int32 -- ^ Listen port number, required
                       , bind :: Maybe String -- ^ Listen IP\/host. Default: /localhost/
                       , accessToken :: Maybe String -- ^ Server access token passed in /X-Access-Token/ header. Default: zero
                       , blocking :: !Bool -- ^ Block open thread until http service finished.
-- Otherwise HTTP servee started in background.

                       , readAnon :: !Bool -- ^ Allow anonymous read-only database access
                       , maxBodySize :: !Word64 -- ^ Maximum WS\/HTTP API body size. Default: 64Mb, Min: 512K
                       }

-- | Create default 'Options'
zero :: Options
zero = Options { enabled = False
               , port = 0
               , bind = Nothing
               , accessToken = Nothing
               , blocking = False
               , readAnon = False
               , maxBodySize = 0
               }

-- | Storable version of Options
data OptionsB = OptionsB { options :: Options
                         , bindPtr :: ForeignPtr CChar
                         , accessTokenPtr :: ForeignPtr CChar
                         , accessTokenLen :: CSize
                         }

-- | Create Storable version of Options
build :: Options -> IO OptionsB
build options = do
        bindPtr <-  maybeNew newCString (bind options)
          >>= newForeignPtr finalizerFree
        (accessTokenPtr, accessTokenLen) <- case (accessToken options) of
                                              Nothing -> return (nullPtr, 0)
                                              Just value -> newCStringLen value
        accessTokenFPtr <- newForeignPtr finalizerFree accessTokenPtr
        return OptionsB { options = options
                            , bindPtr = bindPtr
                            , accessTokenPtr = accessTokenFPtr
                            , accessTokenLen = CSize $ fromIntegral accessTokenLen
                            }


instance Storable OptionsB where
        sizeOf _ = (40)
{-# LINE 67 "src/Database/EJDB2/HTTP.hsc" #-}
        alignment _ = 8
{-# LINE 68 "src/Database/EJDB2/HTTP.hsc" #-}
        peek ptr = do
           enabled <- (\hsc_ptr -> peekByteOff hsc_ptr 37) ptr :: IO CInt
{-# LINE 70 "src/Database/EJDB2/HTTP.hsc" #-}
           port <- (\hsc_ptr -> peekByteOff hsc_ptr 32) ptr
{-# LINE 71 "src/Database/EJDB2/HTTP.hsc" #-}
           bindPtr <- (\hsc_ptr -> peekByteOff hsc_ptr 0) ptr
{-# LINE 72 "src/Database/EJDB2/HTTP.hsc" #-}
           bindFPtr <- newForeignPtr finalizerFree nullPtr
           bind <- maybePeek peekCString bindPtr
           access_token <- (\hsc_ptr -> peekByteOff hsc_ptr 8) ptr
{-# LINE 75 "src/Database/EJDB2/HTTP.hsc" #-}
           access_token_len <- (\hsc_ptr -> peekByteOff hsc_ptr 16) ptr
{-# LINE 76 "src/Database/EJDB2/HTTP.hsc" #-}
           accessTokenFPtr <- newForeignPtr finalizerFree nullPtr
           accessToken <- maybePeek (\ptr -> peekCStringLen (ptr, access_token_len)) access_token
           blocking <- (\hsc_ptr -> peekByteOff hsc_ptr 36) ptr :: IO CInt
{-# LINE 79 "src/Database/EJDB2/HTTP.hsc" #-}
           read_anon <- (\hsc_ptr -> peekByteOff hsc_ptr 38) ptr :: IO CInt
{-# LINE 80 "src/Database/EJDB2/HTTP.hsc" #-}
           max_body_size <- (\hsc_ptr -> peekByteOff hsc_ptr 24) ptr
{-# LINE 81 "src/Database/EJDB2/HTTP.hsc" #-}
           return $ OptionsB
                      (Options
                        (toBool enabled)
                        port
                        bind
                        accessToken
                        (toBool blocking)
                        (toBool read_anon)
                         max_body_size)
                      bindFPtr accessTokenFPtr (CSize $ fromIntegral access_token_len)
        poke ptr (OptionsB
                   (Options enabled port _ _ blocking read_anon max_body_size)
                    bindPtr accessTokenPtr accessTokenLen) = do
           (\hsc_ptr -> pokeByteOff hsc_ptr 37) ptr (fromBool enabled :: CInt)
{-# LINE 95 "src/Database/EJDB2/HTTP.hsc" #-}
           (\hsc_ptr -> pokeByteOff hsc_ptr 32) ptr port
{-# LINE 96 "src/Database/EJDB2/HTTP.hsc" #-}
           withForeignPtr bindPtr $ \cBind ->
             (\hsc_ptr -> pokeByteOff hsc_ptr 0) ptr cBind
{-# LINE 98 "src/Database/EJDB2/HTTP.hsc" #-}
           withForeignPtr accessTokenPtr $ \access_token ->
             (\hsc_ptr -> pokeByteOff hsc_ptr 8) ptr access_token
{-# LINE 100 "src/Database/EJDB2/HTTP.hsc" #-}
           (\hsc_ptr -> pokeByteOff hsc_ptr 16) ptr accessTokenLen
{-# LINE 101 "src/Database/EJDB2/HTTP.hsc" #-}
           (\hsc_ptr -> pokeByteOff hsc_ptr 36) ptr (fromBool blocking :: CInt)
{-# LINE 102 "src/Database/EJDB2/HTTP.hsc" #-}
           (\hsc_ptr -> pokeByteOff hsc_ptr 38) ptr (fromBool read_anon :: CInt)
{-# LINE 103 "src/Database/EJDB2/HTTP.hsc" #-}
           (\hsc_ptr -> pokeByteOff hsc_ptr 24) ptr max_body_size
{-# LINE 104 "src/Database/EJDB2/HTTP.hsc" #-}