Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 在Scotty中在编译时动态添加路由_Haskell_Template Haskell_Scotty - Fatal编程技术网

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

是否可以使用模板Haskell或任何其他方式在编译时通过配置文件动态添加路由

Scotty有一个函数,但我想动态使用它

范例

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]
部分,那么
也可能是个好主意。谢谢。可惜我不能用这个。我在寻找学习的机会。