Code Monkey home page Code Monkey logo

Comments (3)

thma avatar thma commented on July 29, 2024 1

Hi Steve,

That's a cool idea!

There is https://hackage.haskell.org/package/servant-polysemy which allows to run Servant as a polysemy effect. here is a very simple example:

https://github.com/AJChapman/servant-polysemy/blob/master/example/Server.hs

At the moment I don't have the time to do it. But all Pull requests are welcome!

from polysemycleanarchitecture.

thma avatar thma commented on July 29, 2024 1

I put together a blog post that describes my solution:
https://thma.github.io/posts/2022-07-04-polysemy-and-warp.html

from polysemycleanarchitecture.

thma avatar thma commented on July 29, 2024

Hi again Steve,

I came up with the following solution:

  1. Define a new ExternalInterfaces.AppServer effect

  2. Provide a Warp based implementation ExternalInterfaces.WarpAppServer

  3. write a main function that runs the whole Polysemy application
    here is the code:

  4. AppServer.hs:

{-# LANGUAGE TemplateHaskell #-}
module ExternalInterfaces.AppServer where

import Polysemy ( makeSem )
import Network.Wai (Application)
import InterfaceAdapters.Config


data AppServer m a where
  ServeApp           :: Int -> Application -> AppServer m ()
  ServeAppFromConfig :: Config -> AppServer m ()

makeSem ''AppServer
  1. WarpAppServer.hs:
module ExternalInterfaces.WarpAppServer where

import           ExternalInterfaces.AppServer
import           Polysemy (Embed, Sem, Member, embed, interpret, runM )
import qualified Network.Wai.Handler.Warp as Warp (run)
import           InterfaceAdapters.Config                 (Config(..))
import           ExternalInterfaces.ApplicationAssembly (createApp, loadConfig)

    
-- | Warp Based implementation of AppServer
runWarpAppServer :: (Member (Embed IO) r) => Sem (AppServer : r) a -> Sem r a
runWarpAppServer = interpret $ \case
  ServeApp port app         -> embed $ Warp.run port app
  ServeAppFromConfig config -> embed $ 
        let p   = port config
            app = createApp config
        in  Warp.run p app
  1. Main.hs:
-- | in this example the AppServer effect is interpreted by runAppServerOnWarp
warpAsEffectMain :: IO ()
warpAsEffectMain = do
  config <- loadConfig
  serveAppFromConfig config
    & runWarpAppServer    -- use Warp to run rest application
    & runM

What I like about this approach is that the polysemy effect interpretation now remains the outermost layer of the application.
Everything else, including the Warp server is now just an effect.
That makes the whole architecture still a bit cleaner!

Does this approach match your idea?

from polysemycleanarchitecture.

Related Issues (2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.