2

I'm building a Scotty-based application and I'm trying to import and add a list of dynamic middlewares contained in a directory.

I don't want to hard-code my list of middleware - but as of now I'm using a Index.hs which expose all the directory middlewares.

Let's say I have a Main.hs

import Controllers.Index (endpoints)
...

main :: IO ()
main = do
  port <- read <$> getEnv "PORT"
  scotty port $ do
         middleware logStdoutDev
         endpoints

Then in Controllers/Index.hs:

module Controllers.Index
( endpoints ) where

import Controllers.Order (order)
import Controllers.User (user)
...
import Web.Scotty (ScottyM)

endpoints :: ScottyM ()
endpoints = order >> user >> ...

Each Controllers/*.hs contains a middleware.

What would be the best way to get rid of Controllers/Index.hs? Is there a way of importing all the modules from a directory and getting a list I can work with?

framp
  • 813
  • 11
  • 22
  • 2
    You can use cabal to generate the Controllers.Index module at configure time, probably. – user2407038 Mar 06 '15 at 00:23
  • 2
    You mean generating the `Index.hs` file from `Setup.hs`? That's pretty clever. I suppose I am right in assuming there is nothing in Haskell which will let me import modules dynamically, then? (this ticket actually seems to confirm this: https://ghc.haskell.org/trac/ghc/ticket/1475) – framp Mar 06 '15 at 05:27
  • 1
    Yes, you cannot import modules dynamically. I think the priority on that ticket is quite low, so you shouldn't expect anything like that any time soon. – user2407038 Mar 06 '15 at 05:39

1 Answers1

0

A little bit late, but this package helps to generate the needed imports from the setup, as suggested in the comments:

https://hackage.haskell.org/package/imports

framp
  • 813
  • 11
  • 22