This commit is contained in:
2025-11-28 20:21:30 +09:00
parent 68adfeb1d8
commit 2959775365
9 changed files with 75 additions and 12 deletions

View File

@@ -16,12 +16,15 @@ extra-source-files:
library library
exposed-modules: exposed-modules:
API.Server
Data.Persist.Tap Data.Persist.Tap
Data.Persist.TapTrans Data.Persist.TapTrans
Data.State.Track Data.State.Track
Data.Types Data.Types
Domain.Sample
Env.Cache Env.Cache
Env.Persist.Tap Env.Persist.Tap
Infrastructure.App
Infrastructure.Mongo Infrastructure.Mongo
Infrastructure.Persist.Tap Infrastructure.Persist.Tap
Lib Lib
@@ -62,7 +65,8 @@ library
, mtl , mtl
, optics , optics
, resource-pool , resource-pool
, spock , servant
, servant-server
, stm , stm
, text , text
, time , time
@@ -111,7 +115,8 @@ executable hq-sample
, mtl , mtl
, optics , optics
, resource-pool , resource-pool
, spock , servant
, servant-server
, stm , stm
, text , text
, time , time
@@ -166,7 +171,8 @@ test-suite hq-sample-test
, mtl , mtl
, optics , optics
, resource-pool , resource-pool
, spock , servant
, servant-server
, stm , stm
, text , text
, time , time

View File

@@ -41,7 +41,8 @@ dependencies:
- conferer - conferer
- aeson - aeson
- spock - servant
- servant-server
- wai-websockets - wai-websockets
- websockets - websockets

23
src/API/Server.hs Normal file
View File

@@ -0,0 +1,23 @@
module API.Server where
import Control.Monad.IO.Class (MonadIO (..))
import Infrastructure.App (AppM)
import Servant
import Servant.API
type SampleAPI =
Get '[JSON] String
:<|> Capture "text" String :> Get '[JSON] String
type API = "sample" :> SampleAPI
sampleServerM :: ServerT SampleAPI AppM
sampleServerM = getString :<|> getStringMulti
where
getString = do
liftIO . print $ "Hello Empty"
return "Hello"
getStringMulti t = do
liftIO . print $ t ++ "Hello"
return $ t ++ "Hello"

View File

@@ -30,3 +30,4 @@ data Tap = Tap
deriving (Show, Eq, Generic) deriving (Show, Eq, Generic)
newtype CreateTap = CreateTap Tap newtype CreateTap = CreateTap Tap

8
src/Domain/Sample.hs Normal file
View File

@@ -0,0 +1,8 @@
module Domain.Sample where
import Data.Maybe (fromMaybe)
import Data.Text (Text)
import Env.Cache (MonadCache (readCache))
getSample :: (MonadCache m) => Text -> m Text
getSample = fmap (fromMaybe "Nothing") . readCache

View File

@@ -4,4 +4,4 @@ import Data.Text (Text)
class (Monad m) => MonadCache m where class (Monad m) => MonadCache m where
writeCache :: Text -> Text -> m () writeCache :: Text -> Text -> m ()
readCache :: Text -> m (Maybe String) readCache :: Text -> m (Maybe Text)

18
src/Infrastructure/App.hs Normal file
View File

@@ -0,0 +1,18 @@
module Infrastructure.App (AppEnv (..), AppM) where
import Control.Monad.IO.Class (MonadIO (liftIO))
import Control.Monad.Reader (MonadReader (ask), ReaderT, asks)
import Control.Monad.Reader.Class (MonadReader)
import Infrastructure.Mongo (MonadMongo (runMongo), MongoEnv, runMongoM)
newtype AppEnv = AppEnv
{ appMongoEnv :: MongoEnv
}
newtype AppM a = AppM {unAppM :: ReaderT AppEnv IO a}
deriving (Functor, Applicative, Monad, MonadIO, MonadReader AppEnv)
instance MonadMongo AppM where
runMongo mm = AppM $ do
mongoEnv <- asks appMongoEnv
liftIO $ runMongoM mongoEnv mm

View File

@@ -1,7 +1,7 @@
module Infrastructure.Mongo where module Infrastructure.Mongo (MongoEnv (..), MonadMongo (..), MongoM (..), mkMongoEnv, runMongoAction, runMongoM) where
import Control.Monad.IO.Class (MonadIO (liftIO)) import Control.Monad.IO.Class (MonadIO (liftIO))
import Control.Monad.Reader (MonadReader (ask)) import Control.Monad.Reader (MonadReader (ask), ReaderT (runReaderT))
import Control.Monad.Trans.Reader (ReaderT) import Control.Monad.Trans.Reader (ReaderT)
import Data.Pool (Pool, PoolConfig, defaultPoolConfig, newPool, withResource) import Data.Pool (Pool, PoolConfig, defaultPoolConfig, newPool, withResource)
import Data.Text (Text) import Data.Text (Text)
@@ -14,6 +14,9 @@ data MongoEnv = MongoEnv
, mongoDBName :: Text , mongoDBName :: Text
} }
class (Monad m) => MonadMongo m where
runMongo :: MongoM a -> m a
newtype MongoM a = MongoM {unMongoM :: ReaderT MongoEnv IO a} newtype MongoM a = MongoM {unMongoM :: ReaderT MongoEnv IO a}
deriving (Functor, Applicative, Monad, MonadIO, MonadReader MongoEnv) deriving (Functor, Applicative, Monad, MonadIO, MonadReader MongoEnv)
@@ -39,3 +42,6 @@ runMongoAction action = do
MongoEnv{mongoPool, mongoDBName} <- ask MongoEnv{mongoPool, mongoDBName} <- ask
liftIO $ withResource mongoPool $ \pipe -> liftIO $ withResource mongoPool $ \pipe ->
access pipe master mongoDBName action access pipe master mongoDBName action
runMongoM :: MongoEnv -> MongoM a -> IO a
runMongoM env (MongoM r) = runReaderT r env

View File

@@ -6,7 +6,7 @@ import Data.Text (Text)
import Database.MongoDB import Database.MongoDB
import qualified Database.MongoDB as M import qualified Database.MongoDB as M
import Env.Persist.Tap (MonadTaps (..)) import Env.Persist.Tap (MonadTaps (..))
import Infrastructure.Mongo (MongoM, runMongoAction) import Infrastructure.Mongo (MonadMongo (runMongo), MongoM, runMongoAction)
import Serialize.Bson (BsonTrans (fromBson, toBson)) import Serialize.Bson (BsonTrans (fromBson, toBson))
import Serialize.Str (StrTrans (..)) import Serialize.Str (StrTrans (..))
@@ -46,14 +46,14 @@ instance BsonTrans Tap where
tapCollection :: Collection tapCollection :: Collection
tapCollection = "taps" tapCollection = "taps"
instance MonadTaps MongoM where instance (Monad m, MonadMongo m) => MonadTaps m where
createTap (CreateTap tap) = runMongoAction $ do createTap (CreateTap tap) = runMongo . runMongoAction $ do
insert tapCollection $ toBson tap insert tapCollection $ toBson tap
return tap return tap
getTap id = runMongoAction $ do getTap id = runMongo . runMongoAction $ do
doc <- findOne (select ["id" =: id] tapCollection) doc <- findOne (select ["id" =: id] tapCollection)
return $ fromBson =<< doc return $ fromBson =<< doc
deleteTap id = runMongoAction $ do deleteTap id = runMongo . runMongoAction $ do
delete (select ["id" =: id] tapCollection) delete (select ["id" =: id] tapCollection)