【发布时间】:2016-04-02 11:51:10
【问题描述】:
我有一个简单的 hello world Servant 应用程序。我需要向它添加一些静态或动态的 html 页面。我怎样才能做到这一点?在文档中没有提到它。注意我不想在 Haskell 代码中创建 html 布局,我希望 Haskell 显示已经创建的 html 页面。
更新:
我该如何结合:
type MyApi = "/" :> Raw
server :: Server MyApi
server = serveDirectory "static/" -- index.html, about.html
我已经拥有的:
type API =
"api" :> "items" :> Get '[JSON] [MyData] :<|>
"api" :> "items" :> Capture "id" Int :> Get '[JSON] MyData
app :: Application
app = serve api server
api :: Proxy API
api = Proxy
server :: Server API
server = getItems :<|> getItem
startApp :: IO ()
startApp = run 1234 app
更新2:
工作:
type API =
"api" :> "items" :> Get '[JSON] [MyData] :<|>
"api" :> "items" :> Capture "id" Int :> Get '[JSON] MyData :<|>
Raw
不工作,完全没有反应:
type API =
"api" :> "items" :> Get '[JSON] [MyData] :<|>
"api" :> "items" :> Capture "id" Int :> Get '[JSON] MyData :<|>
"/" :> Raw
-- or
type API =
"api" :> "items" :> Get '[JSON] [MyData] :<|>
"api" :> "items" :> Capture "id" Int :> Get '[JSON] MyData :<|>
"" :> Raw
不知道为什么?
【问题讨论】:
-
如果您的静态页面结构正确,您可以使用
serveDirectory。 -
@zakyggaps,已更新。
-
Servant API 将按从上到下的顺序匹配。你可以把它放在最后一行。
-
@zakyggaps,在第一个“服务器”中,我有 serveDirectory “static/”,在第二个中,我有“getItems : getItem”。我该如何组合它们?
-
你不能这样做:
:<|>是右关联的,如果"/" :> Raw是最后一种情况,你必须定义你的 API,其中包含所有三种情况,如case1 :<|> case2 :<|> case3。