0

Template Haskell またはその他の方法を使用して、構成ファイルを介してコンパイル時にルートを動的に追加することは可能ですか?

Scotty には関数addRouteがありますが、動的に使用したいと考えています。

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
4

1 に答える 1

2

わかりました、上記のリストを考えると、以下を手で書くのと同等のものを探していると思います:

{-! LANGUAGE OverloadedStrings #-}
import Web.Scotty
import Data.String

main = scotty 3000 $ do
  middleware logStdoutDev
  get (fromString $ '/' : "hello") (text "hello")
  get (fromString $ '/' : "world") (text "world")

良いニュースは、TH マジックを必要とするものは何もないということです!

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)
于 2015-05-18T12:31:56.640 に答える