Comments (3)
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.
I put together a blog post that describes my solution:
https://thma.github.io/posts/2022-07-04-polysemy-and-warp.html
from polysemycleanarchitecture.
Hi again Steve,
I came up with the following solution:
-
Define a new ExternalInterfaces.AppServer effect
-
Provide a Warp based implementation ExternalInterfaces.WarpAppServer
-
write a main function that runs the whole Polysemy application
here is the code: -
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
- 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
- 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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from polysemycleanarchitecture.