Skip to content

Commit

Permalink
Format code
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-k authored and Anton Kholomiov committed Aug 22, 2023
1 parent 1b7f6fb commit d102e31
Show file tree
Hide file tree
Showing 39 changed files with 1,290 additions and 1,165 deletions.
1 change: 1 addition & 0 deletions Setup.hs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import Distribution.Simple

main = defaultMain
38 changes: 22 additions & 16 deletions examples/mig-example-apps/Counter/Main.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# Language UndecidableInstances #-}
{-# LANGUAGE UndecidableInstances #-}

-- | Example shows how to use custom monad with server.
-- We use Json response server with our Monad App which is a newtype over ReaderT-IO.
-- As we can run only @Server IO@ we need to convert the @Server App@ to IO-based server.
Expand All @@ -8,14 +9,15 @@
-- extension as our monad is a newtype wrapper over ReaderT-IO and for that monad server can be derived.
--
-- Also we can derive instance for the newtypes over @ReaderT env (ExceptT err IO)@
module Main
( main
) where
module Main (
main,
) where

-- import Json based server
import Mig.Json

import Control.Monad.Reader
import Data.IORef
import Mig.Json

main :: IO ()
main = do
Expand All @@ -32,14 +34,16 @@ counter =
-------------------------------------------------------------------------------------
-- server types

-- | Custom type for application monad which is based on Reader-IO pattern.
-- Note the HasServer instance. It allows us to render server to IO-based one
-- which we can run as warp + WAI server
{-| Custom type for application monad which is based on Reader-IO pattern.
Note the HasServer instance. It allows us to render server to IO-based one
which we can run as warp + WAI server
-}
newtype App a = App (ReaderT Env IO a)
deriving newtype (Functor, Applicative, Monad, MonadReader Env, MonadIO, HasServer)

-- | Common shared state
-- We can put more shared state if we need. Like logger state or some interfaces.
{-| Common shared state
We can put more shared state if we need. Like logger state or some interfaces.
-}
data Env = Env
{ current :: IORef Int
}
Expand All @@ -51,14 +55,16 @@ initEnv = Env <$> newIORef 0
-------------------------------------------------------------------------------------
-- server definition

-- | Server has two routes:
--
-- * get - to querry current state
-- * put - to add some integer to the state
{-| Server has two routes:
* get - to querry current state
* put - to add some integer to the state
-}
server :: Server App
server =
"counter" /. "api" /.
mconcat
"counter"
/. "api"
/. mconcat
[ "get" /. handleGet
, "put" /. handlePut
]
Expand Down
21 changes: 10 additions & 11 deletions examples/mig-example-apps/HelloClient/Main.hs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module Main
( main
) where
module Main (
main,
) where

import Data.Text (Text)
import Mig.Client
import Mig.Server
import Data.Text (Text)
import Network.HTTP.Client

type Hello m = Capture "who" Text -> Capture "suffix" Text -> Get Json m Text
Expand All @@ -18,14 +18,13 @@ main = do

hello :: Hello Client
bye :: Bye Client

(hello, bye) = toClient server

server :: Server Client
server =
"api" /. "v1" /. mconcat
[ "hello" /. "*" /. "*" /. route hello
, "bye" /. route bye
]


"api"
/. "v1"
/. mconcat
[ "hello" /. "*" /. "*" /. route hello
, "bye" /. route bye
]
66 changes: 36 additions & 30 deletions examples/mig-example-apps/HelloWorld/Main.hs
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
-- | Most basic Hello world server.
-- It has only one route which outputs the greeting to the user
module Main
( main
, hello'
, bye'
) where
{-| Most basic Hello world server.
It has only one route which outputs the greeting to the user
-}
module Main (
main,
hello',
bye',
) where

-- import Mig.Json.IO

import Control.Lens ((&), (.~), (?~))
import Data.Aeson.Encode.Pretty
import Data.ByteString.Lazy.Char8 qualified as BL
import Data.Text (Text)
import Mig.Server
import Mig.Internal.Api (toNormalApi)
import Text.Show.Pretty
import Mig.OpenApi
import Data.Aeson.Encode.Pretty
import Data.ByteString.Lazy.Char8 qualified as BL
import Mig.Server
import Mig.Swagger.Ui
import Control.Lens ((.~), (?~), (&))
import Text.Show.Pretty

import Mig.Client
import Data.OpenApi qualified as OA
import Mig.Client

-- | We can render the server and run it on port 8085.
-- It uses wai and warp.
{-| We can render the server and run it on port 8085.
It uses wai and warp.
-}
main :: IO ()
main = do
putStrLn ("The hello world server listens on port: " <> show port)
Expand All @@ -31,14 +34,18 @@ main = do
where
port = 8085

-- | Init simple hello world server which
-- replies on a single route
{-| Init simple hello world server which
replies on a single route
-}
server :: Server IO
server = withSwagger swaggerConfig $
"api" /. "v1" /. mconcat
[ setDescription "Greeting action" $ "hello" /. "*" /. "*" /. route hello
, "bye" /. route bye
]
server =
withSwagger swaggerConfig $
"api"
/. "v1"
/. mconcat
[ setDescription "Greeting action" $ "hello" /. "*" /. "*" /. route hello
, "bye" /. route bye
]
where
swaggerConfig =
SwaggerConfig
Expand All @@ -48,12 +55,12 @@ server = withSwagger swaggerConfig $
}

addInfo =
OA.info .~
(mempty
& OA.title .~ "Hello world app"
& OA.description ?~ "Demo application"
& OA.version .~ "1.0"
)
OA.info
.~ ( mempty
& OA.title .~ "Hello world app"
& OA.description ?~ "Demo application"
& OA.version .~ "1.0"
)

type Hello m = Capture "who" Text -> Capture "suffix" Text -> Get Json m Text

Expand All @@ -71,6 +78,5 @@ bye (Optional mWho) = Send $ pure $ mappend "Hello " $ maybe "World" id mWho

hello' :: Hello Client
bye' :: Bye Client

hello'
:| bye' = toClient server
:| bye' = toClient server
11 changes: 6 additions & 5 deletions examples/mig-example-apps/Html/Content.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
-- | random content for blog posts
module Content
( poems
, quotes
) where
module Content (
poems,
quotes,
) where

import Data.Text (Text)
import Data.Text qualified as Text
Expand Down Expand Up @@ -74,7 +74,8 @@ dream =
]

pie :: Text
pie = Text.unlines
pie =
Text.unlines
[ "Cottleston Cottleston Cottleston Pie,"
, "A fly can't bird, but a bird can fly."
, "Ask me a riddle and I reply"
Expand Down
43 changes: 22 additions & 21 deletions examples/mig-example-apps/Html/Init.hs
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
module Init
( initSite
) where
module Init (
initSite,
) where

import Data.Text (Text)
import Data.Text.IO qualified as Text
import Data.IORef
import System.Random
import Data.List qualified as List
import Data.Text (Text)
import Data.Text.IO qualified as Text
import Data.Time
import System.Random

import Content
import Types
import Interface
import Internal.State
import Types

-- | Initialise the logic for our website.
-- we read the posts from some poems and fill the site with them.
--
-- Also we init all actions. Note how we hide the mutable state Env with interface for Site.
{-| Initialise the logic for our website.
we read the posts from some poems and fill the site with them.
Also we init all actions. Note how we hide the mutable state Env with interface for Site.
-}
initSite :: IO Site
initSite = do
env <- initEnv
pure $ Site
{ readBlogPost = mockRead env
, writeBlogPost = mockWriteBlogPost env
, listBlogPosts = readIORef env.blogPosts
, readQuote = Quote <$> randomQuote
, logInfo = Text.putStrLn . mappend "[INFO]: "
}
pure $
Site
{ readBlogPost = mockRead env
, writeBlogPost = mockWriteBlogPost env
, listBlogPosts = readIORef env.blogPosts
, readQuote = Quote <$> randomQuote
, logInfo = Text.putStrLn . mappend "[INFO]: "
}

-------------------------------------------------------------------------------------
-- implementation of the site interfaces.
Expand All @@ -46,10 +48,9 @@ mockWriteBlogPost env title content = do
pid <- randomBlogPostId
time <- getCurrentTime
-- unsafe in concurrent, it is here just for example (use TVar or atomicModifyIORef)
modifyIORef' env.blogPosts (BlogPost pid title time content : )
modifyIORef' env.blogPosts (BlogPost pid title time content :)
pure pid


randomQuote :: IO Text
randomQuote = oneOf quotes

Expand All @@ -58,6 +59,6 @@ randomQuote = oneOf quotes

-- pick random element from a list
oneOf :: [a] -> IO a
oneOf as = (as !! ) . (`mod` len) <$> randomIO
oneOf as = (as !!) . (`mod` len) <$> randomIO
where
len = length as
16 changes: 9 additions & 7 deletions examples/mig-example-apps/Html/Interface.hs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
-- | Site interfaces as abstractions over interaction with outside world.
-- For example logging, storing new posts in DB, etc.
module Interface
( Site (..)
) where
{-| Site interfaces as abstractions over interaction with outside world.
For example logging, storing new posts in DB, etc.
-}
module Interface (
Site (..),
) where

import Data.Text (Text)
import Types

-- | Web site actions. It defines interfaces that connect logic of our site
-- with outside world: DBs, logger.
{-| Web site actions. It defines interfaces that connect logic of our site
with outside world: DBs, logger.
-}
data Site = Site
{ readBlogPost :: BlogPostId -> IO (Maybe BlogPost)
, writeBlogPost :: Text -> Text -> IO BlogPostId
Expand Down
35 changes: 18 additions & 17 deletions examples/mig-example-apps/Html/Internal/State.hs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
module Internal.State
( Env (..)
, initEnv
, randomBlogPostId
) where
module Internal.State (
Env (..),
initEnv,
randomBlogPostId,
) where

import Content
import Types

import Data.IORef
import Data.Text (Text)
import Data.Text qualified as Text
import Data.IORef
import Data.Time
import System.Random

-- | Site mutable state
data Env = Env
{ blogPosts :: IORef [BlogPost]
-- ^ for example we store posts in memory but it also can become a DB.
-- ^ for example we store posts in memory but it also can become a DB.
}

initEnv :: IO Env
Expand All @@ -28,16 +28,17 @@ poemToBlogPost :: Text -> IO BlogPost
poemToBlogPost poem = do
pid <- randomBlogPostId
time <- getCurrentTime
pure $ BlogPost
{ id = pid
, createdAt = time
, title =
let
ls = Text.lines poem
in
mconcat [ last ls, ": ", head ls]
, content = poem
}
pure $
BlogPost
{ id = pid
, createdAt = time
, title =
let
ls = Text.lines poem
in
mconcat [last ls, ": ", head ls]
, content = poem
}

-- | allocates fresh id for blog post
randomBlogPostId :: IO BlogPostId
Expand Down
Loading

0 comments on commit d102e31

Please sign in to comment.