如何提升为Servant Server型?
How to lift to Servant Server type?
我在使用以下代码时遇到问题:
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
module API where
import Data.Text
import Servant.API
import Servant
import Repository
import FileOperation
import Util
import Config
import qualified Data.Map as M
import Control.Monad.State.Strict (liftIO)
import Network.Wai.Handler.Warp
type RepositoryAPI = "repository" :> "all" :> Get '[JSON] [Repository]
:<|> "all" :> "repository" :> Get '[JSON] [Repository]
server :: Server RepositoryAPI
server = do
repositoriesMap <- liftIO $ loadFromFile repositoryMapFile
let repositories = M.elems repositoriesMap in do
return repositories
repositoryAPI :: Proxy RepositoryAPI
repositoryAPI = Proxy
app :: Application
app = serve repositoryAPI server
main :: IO ()
main = run 8081 app
我得到的错误是:
API.hs:22:3: error:
• Couldn't match type ‘[a0]’ with ‘Handler [Repository]’
Expected type: Server RepositoryAPI
Actual type: Handler [Repository] :<|> [a0]
• In a stmt of a 'do' block:
repositoriesMap <- liftIO $ loadFromFile repositoryMapFile
In the expression:
do repositoriesMap <- liftIO $ loadFromFile repositoryMapFile
let repositories = ... in do return repositories
In an equation for ‘server’:
server
= do repositoriesMap <- liftIO $ loadFromFile repositoryMapFile
let ... in do ...
|
22 | repositoriesMap <- liftIO $ loadFromFile repositoryMapFile
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
问题是我怎样才能正确的解除returns Servant Server类型?
问题与提升无关。真正的问题是您只指定了一个 Handler
,但您的服务器需要两个 Handler
,一个用于 "repository" :> "all" :> Get '[JSON] [Repository]
,一个用于 "all" :> "repository" :> Get '[JSON] [Repository]
。这是你的第二个骨架(用这个替换你现有的 server
):
getRepositoryAll :: Handler [Repository]
getRepositoryAll = do
repositoriesMap <- liftIO $ loadFromFile repositoryMapFile
let repositories = M.elems repositoriesMap in do
return repositories
getAllRepository :: Handler [Repository]
getAllRepository = undefined -- fill this in
server :: Server RepositoryAPI
server = getRepositoryAll :<|> getAllRepository
我在使用以下代码时遇到问题:
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
module API where
import Data.Text
import Servant.API
import Servant
import Repository
import FileOperation
import Util
import Config
import qualified Data.Map as M
import Control.Monad.State.Strict (liftIO)
import Network.Wai.Handler.Warp
type RepositoryAPI = "repository" :> "all" :> Get '[JSON] [Repository]
:<|> "all" :> "repository" :> Get '[JSON] [Repository]
server :: Server RepositoryAPI
server = do
repositoriesMap <- liftIO $ loadFromFile repositoryMapFile
let repositories = M.elems repositoriesMap in do
return repositories
repositoryAPI :: Proxy RepositoryAPI
repositoryAPI = Proxy
app :: Application
app = serve repositoryAPI server
main :: IO ()
main = run 8081 app
我得到的错误是:
API.hs:22:3: error:
• Couldn't match type ‘[a0]’ with ‘Handler [Repository]’
Expected type: Server RepositoryAPI
Actual type: Handler [Repository] :<|> [a0]
• In a stmt of a 'do' block:
repositoriesMap <- liftIO $ loadFromFile repositoryMapFile
In the expression:
do repositoriesMap <- liftIO $ loadFromFile repositoryMapFile
let repositories = ... in do return repositories
In an equation for ‘server’:
server
= do repositoriesMap <- liftIO $ loadFromFile repositoryMapFile
let ... in do ...
|
22 | repositoriesMap <- liftIO $ loadFromFile repositoryMapFile
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
问题是我怎样才能正确的解除returns Servant Server类型?
问题与提升无关。真正的问题是您只指定了一个 Handler
,但您的服务器需要两个 Handler
,一个用于 "repository" :> "all" :> Get '[JSON] [Repository]
,一个用于 "all" :> "repository" :> Get '[JSON] [Repository]
。这是你的第二个骨架(用这个替换你现有的 server
):
getRepositoryAll :: Handler [Repository]
getRepositoryAll = do
repositoriesMap <- liftIO $ loadFromFile repositoryMapFile
let repositories = M.elems repositoriesMap in do
return repositories
getAllRepository :: Handler [Repository]
getAllRepository = undefined -- fill this in
server :: Server RepositoryAPI
server = getRepositoryAll :<|> getAllRepository