gpt4 book ai didi

haskell - 如何提升到Servant服务器类型?

转载 作者:行者123 更新时间:2023-12-04 07:36:08 25 4
gpt4 key购买 nike

我在使用以下代码时遇到问题:

{-# 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
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
问题是我如何正确提升,以便提升返回仆人服务器类型?

最佳答案

问题与举重无关。真正的问题是你只指定了一个 Handler ,但您的服务器需要两个 Handler s,一个为"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

关于haskell - 如何提升到Servant服务器类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67731453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com