Haskell 在Scotty中在编译时动态添加路由
是否可以使用模板Haskell或任何其他方式在编译时通过配置文件动态添加路由 Scotty有一个函数,但我想动态使用它 范例Haskell 在Scotty中在编译时动态添加路由,haskell,template-haskell,scotty,Haskell,Template Haskell,Scotty,是否可以使用模板Haskell或任何其他方式在编译时通过配置文件动态添加路由 Scotty有一个函数,但我想动态使用它 范例 import qualified Data.Text.Lazy as LTB sampleRoutes :: [(String, LTB.Text)] sampleRoutes = [("hello", LTB.pack "hello"), ("world", LTB.pack "world")] 我想迭代sampleRoutes数组,并在编译时定义路由和响应 imp
import qualified Data.Text.Lazy as LTB
sampleRoutes :: [(String, LTB.Text)]
sampleRoutes = [("hello", LTB.pack "hello"), ("world", LTB.pack "world")]
我想迭代sampleRoutes数组,并在编译时定义路由和响应
import Web.Scotty
main = scotty 3000 $ do
middleware logStdoutDev
someFunc sampleRoutes
好的,鉴于上面的列表,我假设您正在寻找与手写以下内容相当的内容:
{-! LANGUAGE OverloadedStrings #-}
import Web.Scotty
import Data.String
main = scotty 3000 $ do
middleware logStdoutDev
get (fromString $ '/' : "hello") (text "hello")
get (fromString $ '/' : "world") (text "world")
好消息是,里面没有任何东西需要魔法
请记住,addroute
/get
只是返回ScottyM()
值的常规函数。如果我有
r1 = get (fromString $ '/' : "hello") (text "hello")
r2 = get (fromString $ '/' : "world") (text "world")
那么前面的main
函数与
main = do
middleware logStdoutDev
r1
r2
这一点以及r1
和r2
的共同结构建议了以下解决方案:
import Control.Monad (forM_)
main = do
middleware logStdoutDev
forM_ sampleRoutes $ \(name, response) ->
get (fromString $ '/':name) (text response)
没有使用TH,我已经能够做
do{中间件logStdoutDev;((get(“/hello”))(text(LTB.pack(“hello”));((get(/world”)(text(LTB.pack(“world”)))}
如何根据一些列表在ScottyM()
monad中动态组合动作?小提示:可以删除(大部分,如果不是全部的话)如果你只需要使用{-#LANGUAGE OverloadedStrings}
(而Text
代替String
,如果你真的不需要String=[Char]
部分,那么也可能是个好主意。谢谢。可惜我不能用这个。我在寻找学习的机会。