Skip to content

Commit

Permalink
small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
zachary822 committed Jan 12, 2025
1 parent d24c2c8 commit 19120ab
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
28 changes: 14 additions & 14 deletions app/Lib/OpenApi/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,6 @@ import GHC.Generics (Generic)
import Lib.Common.Types
import Network.HTTP.Types (Status, ok200)

data OpenApi = OpenApi
{ _openapi :: Version
, _paths :: M.Map Text PathItemObject
, _components :: Components
}
deriving (Generic, Show, Eq)

instance FromJSON OpenApi where
parseJSON = withObject "OpenApi" $ \o ->
OpenApi
<$> o .: "openapi"
<*> o .: "paths"
<*> (maybe mempty id <$> o .:? "components")

newtype Version = Version (Int, Int, Int) deriving (Generic, Show, Eq, Ord)

instance FromJSON Version where
Expand Down Expand Up @@ -245,4 +231,18 @@ instance FromJSON Ref where
parseJSON = withObject "ref" $ \o ->
Ref <$> o .: "$ref"

data OpenApi = OpenApi
{ _openapi :: Version
, _paths :: M.Map Text PathItemObject
, _components :: Components
}
deriving (Generic, Show, Eq)

instance FromJSON OpenApi where
parseJSON = withObject "OpenApi" $ \o ->
OpenApi
<$> o .: "openapi"
<*> o .: "paths"
<*> (maybe mempty id <$> o .:? "components")

makeLenses ''OpenApi
14 changes: 7 additions & 7 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Control.Monad
import Control.Monad.IO.Class
import Data.Aeson
import Data.Aeson.Key (fromText)
import Data.Foldable (sequenceA_, traverse_)
import Data.Foldable (for_, sequenceA_, traverse_)
import Data.List.NonEmpty qualified as N
import Data.Map.Strict qualified as M
import Data.Maybe (catMaybes, fromMaybe)
Expand Down Expand Up @@ -123,16 +123,16 @@ main = do
setHeader "Content-Type" "text/html"
raw $ renderHtml (swaggerPage "/openapi.yaml")

forM_ (api ^. paths . to M.toList & traverse . _2 %~ pathLookup pathItems) $
for_ (api ^. paths . to M.toList & traverse . _2 %~ pathLookup pathItems) $
\case
(path, Just (PathItem{..})) -> do
let prepareOp verb o =
flip (maybe mempty) o $
\(OperationObject resp) ->
forM_ (resp ^. to M.toList & traverse . _2 %~ responseLookup responses) $
for_ (resp ^. to M.toList & traverse . _2 %~ responseLookup responses) $
\case
(sts, Just (Response{..})) ->
forM_ (M.toList content) $ \(mt, MediaTypeObject schema) ->
for_ (M.toList content) $ \(mt, MediaTypeObject schema) ->
verb (Capture (sanitizePath path)) $ do
status (getStatus sts)
setHeader "Content-Type" (TL.fromStrict mt)
Expand All @@ -151,10 +151,10 @@ main = do
prepareOp (addroute TRACE) traceOp
flip (maybe mempty) headOp $
\(OperationObject resp) ->
forM_ (resp ^. to M.toList & traverse . _2 %~ responseLookup responses) $
for_ (resp ^. to M.toList & traverse . _2 %~ responseLookup responses) $
\case
(sts, Just (Response{..})) ->
forM_ (M.toList content) $ \(mt, _) ->
for_ (M.toList content) $ \(mt, _) ->
(addroute HEAD) (Capture (sanitizePath path)) $ do
status (getStatus sts)
setHeader "Content-Type" (TL.fromStrict mt)
Expand All @@ -164,7 +164,7 @@ main = do
scotty _port $ do
mws

forM_ (M.toList ps) $ \(path, PathConfig{..}) -> do
for_ (M.toList ps) $ \(path, PathConfig{..}) -> do
let addroute' = maybe matchAny (addroute . unMethod) responseMethod
addroute' (fromString path) $ do
sequenceA_ $
Expand Down

0 comments on commit 19120ab

Please sign in to comment.