servant

Redirections in Servant

不羁岁月 提交于 2019-12-30 04:23:06
问题 What's the appropriate way to make a Servant handler respond with a redirection? I am working in a navigation REST app and I would like to respond to POST requests that create resources with a redirection to the corresponding GET resource list paths. So for instance POST /foos should redirect to GET /foos after creating a foo. I could not find a clear way to do that in the documentation. 回答1: There is one simple (but slightly hacky) answer, and a lead for making the first option obsolete

How to redirect requests from domain.com to www.domain in Wai/Warp app?

徘徊边缘 提交于 2019-12-25 05:09:56
问题 My site is built in Haskell/Servant and Wai/Warp. I need to redirect all requests from my domain.com to www.domain.com with the the 301 or 302 status. I know I can do that with the help of Wai/Warp somehow. How exactly? startApp :: IO () startApp = run 1234 app 回答1: The package wai-util has a convenience function redirect' to create such a Response , so you should be able to do something like app :: Application app req respond = respond =<< redirect' status302 [] uri where Just uri = parseURI

“No instance for” trait that's already implemented

☆樱花仙子☆ 提交于 2019-12-24 09:59:16
问题 I'd like to use Servant's ClientM monad with finally :: MonadBaseControl IO m => m a -> m b -> m a , but am faced with error No instance for (MonadBaseControl IO ClientM) . Oddly, this exact instance appears to be defined already, be it from an internal module. Do I need to explicitly import such instances somehow? 回答1: It works fine if, as Daniel said, you import the module that defines the desired instance: Prelude> import Control.Exception.Lifted Prelude Control.Exception.Lifted> import

Model a serial format in the type system, like Servant

不打扰是莪最后的温柔 提交于 2019-12-24 08:59:55
问题 I'm working on an API integration that ignores the existence of XML or JSON in favor of just appending character data. (The Metro2 format, if interested) I'm simplifying, but imagine that a person needs to be serialized like this: At pos 0, 4 chars: Number of bytes in the message At pos 5: 6 chars: "PERSON" hard coded At pos 11: 20 chars: Name, left-aligned and space-padded At pos 21: 8 chars: Birthday, YYYYMMDD At pos 29: 3 chars: Age, right-aligned and zero-padded Numeric fields are always

Using Licius + Hamlet + Julius in Servant

邮差的信 提交于 2019-12-24 01:19:12
问题 I would like to use Shakespearean Templates (Licius + Hamlet + Julius) from the Yesod.But I have some difficulty with this. The following code from enter link description here works: type TestAPI = "tests" :> Get '[JSON] [Test] :<|> "test" :> Get '[JSON] Test :<|> "TestHTML.html" :> Get '[HTML] Page_TestHTML serverTestAPI :: ServerT TestAPI AppM serverTestAPI = tests :<|> test :<|> testHtml data Page_TestHTML = Page_TestHTML instance ToMarkup Page_TestHTML where toMarkup Page_TestHTML =

Using Servant with Yesod shakespeare (Hamlet, Julius, Lucius)

雨燕双飞 提交于 2019-12-23 03:30:52
问题 How i can use shakespeare (from yesod) for servant webservices APIs? I try: type TestAPI = "tests" :> Get '[JSON] [Test] :<|> "Test.html" :> Get '[HTML] Html serverTestAPI :: ServerT TestAPI AppM serverTestAPI = tests :<|> test :<|> testHtml tests :: AppM [Test] tests = do return [ Test 1 "Test 1" , Test 2 "Test 2" ] testHtml = [hamlet| $doctype 5 ......... |] But I get error! 回答1: As @Carsten points out, in this case what you want is shamlet . The key thing is implementing proper instances

What does an apostrophe in front of a list ( '[Something] ) mean in Haskell?

让人想犯罪 __ 提交于 2019-12-17 16:44:19
问题 I was reading the Servant documentation and came across this line: type UserAPI = "users" :> QueryParam "sortby" SortBy :> Get '[JSON] [User] What is the ' doing to that list? 回答1: This is DataKinds in action, which: lifts values at the type level, and lifts types to the kind level This however causes confusion at the type level. Now, in types, [X] might either be [X] :: * , the list-of- X type, or instead we might have [X] :: [T] due to the lifting -- that is the value [X] (list containing

Haskell Servant and streaming

血红的双手。 提交于 2019-12-08 19:08:59
问题 I am trying to add a functionality to my servant server that would get a file from Amazon S3 and stream it back to the user. Because files can be big I don't want to download them locally and then serve them to clients, I'd rather prefer to stream them directly from S3 to clients. I use Amazonka for what I do with S3 and I can get a stream for an S3 file as a Conduit sink. But now I don't know how to get from Sink to EitherT ServantErr IO a . Can anyone explain me how to do it or show me some

Use of `get` and `toSqlKey` in persistent

孤街醉人 提交于 2019-12-08 11:10:44
问题 I'm trying to use persistent-postgresql with servant. I have a User model. I want to have an endpoint that takes an id and returns the user with that id. According to other answers I can use toSqlKey to turn an Int64 into a Key to feed to get . My function looks like: oneUser :: Int64 -> App (Entity User) oneUser userId = do maybeUser <- runDb $ get $ toSqlKey userId case maybeUser of Nothing -> throwError err404 Just user -> return user When I try to compile I get the error Couldn't match

Issues using pattern matching with servant-client

北城余情 提交于 2019-12-07 21:03:15
问题 In the Servant docs, we have the following api: type API = "position" :> Capture "x" Int :> Capture "y" Int :> Get '[JSON] Position :<|> "hello" :> QueryParam "name" String :> Get '[JSON] HelloMessage :<|> "marketing" :> ReqBody '[JSON] ClientInfo :> Post '[JSON] Email and we can define client functions like so: api :: Proxy API api = Proxy position :<|> hello :<|> marketing = client api If our api type instead looked like: type API = QueryParam "test" Int :> ( "position" :> Capture "x" Int :