Compare commits

..

1 commit

Author SHA1 Message Date
3eff953def Add Pagination 2022-07-17 16:43:47 -04:00
3 changed files with 19 additions and 5 deletions

View file

@ -24,6 +24,7 @@ dependencies:
- containers
- envy
- lens
- network-uri
- openapi3
- parsec
- parsec-extra

View file

@ -25,6 +25,7 @@ import qualified Data.Text as T
import Data.Typeable (Typeable)
import Data.Version (showVersion)
import GHC.Generics (Generic)
import Network.URI
import qualified Paths_tutor as PT
import Servant
import Servant.OpenApi (HasOpenApi (toOpenApi))
@ -229,9 +230,12 @@ tutorServer config = status config :<|> searchCards (tutorDatabase config)
prev Pagination.Page {Pagination.previous = Just p} = addHeader $ link "prev" $ uri (Just $ Pagination.limit p) (Just $ Pagination.offset p)
prev _ = noHeader
uri :: Maybe Int -> Maybe Int -> URI
uri limit' offset' = linkURI $ safeLink api (Proxy :: Proxy SearchCards) q sortBy inCollection limit' offset'
uri limit' offset' =
-- TODO: Apply the generated relative URI to the requested absolute URI
relativeTo
(linkURI $ safeLink api (Proxy :: Proxy SearchCards) q sortBy inCollection limit' offset')
(configuredBaseURI config)
link :: T.Text -> URI -> T.Text
-- TODO: Apply the generated relative URI to the requested absolute URI
link rel uri = T.concat ["<", T.pack $ show uri, ">; rel=\"", rel, "\""]
docsServer :: Server DocsAPI

View file

@ -1,14 +1,16 @@
{-# LANGUAGE DeriveGeneric #-}
module Tutor.Config (Config (..), defaultConfig) where
module Tutor.Config (Config (..), defaultConfig, configuredBaseURI) where
import GHC.Generics
import Network.URI
import System.Envy
data Config = Config
{ tutorPort :: Int,
tutorDatabase :: FilePath,
tutorStatic :: FilePath
tutorStatic :: FilePath,
tutorBaseUrl :: Maybe String
}
deriving (Generic, Show)
@ -16,7 +18,14 @@ defaultConfig =
Config
{ tutorPort = 8000,
tutorDatabase = "tutor.db",
tutorStatic = "www/public"
tutorStatic = "www/public",
tutorBaseUrl = Nothing
}
configuredBaseURI :: Config -> URI
configuredBaseURI config =
case tutorBaseUrl config >>= parseURI of
Just uri -> uri
Nothing -> URI "http:" (Just $ URIAuth "" "localhost" (":" ++ show (tutorPort config))) "/" "" ""
instance FromEnv Config