mirror of
https://github.com/monadicsystems/okapi.git
synced 2024-11-26 21:39:51 +03:00
Make various tweaks to names and documentation
This commit is contained in:
parent
11501cec43
commit
15e307a63e
@ -185,13 +185,13 @@ todoAPI conn =
|
||||
healthCheck :: Okapi Response
|
||||
healthCheck = do
|
||||
methodGET
|
||||
optional $ pathParam @Text `equals` ""
|
||||
optional $ pathParam @Text `is` ""
|
||||
respond ok
|
||||
|
||||
getTodo :: Connection -> Okapi Response
|
||||
getTodo conn = do
|
||||
methodGET
|
||||
pathParam @Text `equals` "todos"
|
||||
pathParam @Text `is` "todos"
|
||||
todoID <- pathParam @Int
|
||||
maybeTodo <- lift $ selectTodo conn todoID
|
||||
case maybeTodo of
|
||||
@ -201,7 +201,7 @@ getTodo conn = do
|
||||
getAllTodos :: Connection -> Okapi Response
|
||||
getAllTodos conn = do
|
||||
methodGET
|
||||
pathParam @Text `equals` "todos"
|
||||
pathParam @Text `is` "todos"
|
||||
status <- optional $ queryParam @TodoStatus "status"
|
||||
todos <- lift $ selectAllTodos conn status
|
||||
ok & setJSON todos & respond
|
||||
@ -209,7 +209,7 @@ getAllTodos conn = do
|
||||
createTodo :: Connection -> Okapi Response
|
||||
createTodo conn = do
|
||||
methodPOST
|
||||
pathParam @Text `equals` "todos"
|
||||
pathParam @Text `is` "todos"
|
||||
todoForm <- bodyForm
|
||||
lift $ insertTodoForm conn todoForm
|
||||
respond ok
|
||||
@ -217,7 +217,7 @@ createTodo conn = do
|
||||
editTodo :: Connection -> Okapi Response
|
||||
editTodo conn = do
|
||||
methodPUT
|
||||
equals @Text pathParam "todos"
|
||||
is @Text pathParam "todos"
|
||||
todoID <- pathParam @Int
|
||||
todoForm <- bodyForm @TodoForm
|
||||
lift $ updateTodo conn todoID todoForm
|
||||
@ -226,7 +226,7 @@ editTodo conn = do
|
||||
forgetTodo :: Connection -> Okapi Response
|
||||
forgetTodo conn = do
|
||||
methodDELETE
|
||||
pathParam @Text `equals` "todos"
|
||||
pathParam @Text `is` "todos"
|
||||
todoID <- pathParam @Int
|
||||
lift $ deleteTodo conn todoID
|
||||
respond ok
|
||||
|
@ -20,7 +20,7 @@ type Okapi a = OkapiT IO a
|
||||
calc :: Okapi Response
|
||||
calc = do
|
||||
methodGET
|
||||
pathParam @Text `equals` "calc"
|
||||
pathParam @Text `is` "calc"
|
||||
addOp <|> subOp <|> mulOp <|> divOp
|
||||
|
||||
respond :: Response -> Okapi Response
|
||||
@ -32,7 +32,7 @@ respond response = do
|
||||
|
||||
addOp :: Okapi Response
|
||||
addOp = do
|
||||
pathParam @Text `equals` "add"
|
||||
pathParam @Text `is` "add"
|
||||
(x, y) <- getArgs
|
||||
ok
|
||||
& setJSON (x + y)
|
||||
@ -40,7 +40,7 @@ addOp = do
|
||||
|
||||
subOp :: Okapi Response
|
||||
subOp = do
|
||||
pathParam @Text `equals` "sub" <|> pathParam @Text `equals` "minus"
|
||||
pathParam @Text `is` "sub" <|> pathParam @Text `is` "minus"
|
||||
(x, y) <- getArgs
|
||||
ok
|
||||
& setJSON (x - y)
|
||||
@ -48,7 +48,7 @@ subOp = do
|
||||
|
||||
mulOp :: Okapi Response
|
||||
mulOp = do
|
||||
pathParam @Text `equals` "mul"
|
||||
pathParam @Text `is` "mul"
|
||||
(x, y) <- getArgs
|
||||
ok
|
||||
& setJSON (x * y)
|
||||
@ -62,7 +62,7 @@ data DivResult = DivResult
|
||||
|
||||
divOp :: Okapi Response
|
||||
divOp = do
|
||||
pathParam @Text `equals` "div"
|
||||
pathParam @Text `is` "div"
|
||||
(x, y) <- getArgs
|
||||
guardThrow forbidden (y == 0)
|
||||
ok
|
||||
|
@ -6,6 +6,7 @@
|
||||
{-# LANGUAGE QuasiQuotes #-}
|
||||
{-# LANGUAGE RecordWildCards #-}
|
||||
{-# LANGUAGE DeriveGeneric #-}
|
||||
{-# LANGUAGE DeriveAnyClass #-}
|
||||
|
||||
module Main where
|
||||
|
||||
@ -54,9 +55,7 @@ data Car = Car
|
||||
, carYear :: Int
|
||||
, carMiles :: Int
|
||||
, carPrice :: Float
|
||||
} deriving (Eq, Show, Generic)
|
||||
|
||||
instance FromForm Car where
|
||||
} deriving (Eq, Show, Generic, FromForm)
|
||||
|
||||
pattern HomeRoute = (GET, [])
|
||||
|
||||
|
@ -32,7 +32,7 @@ api eventSource = do
|
||||
where
|
||||
index :: Okapi Response
|
||||
index = do
|
||||
pathEnd <|> pathParam @String `equals` ""
|
||||
pathEnd <|> pathParam @String `is` ""
|
||||
ok
|
||||
& setHeader ("Content-Type", "text/html")
|
||||
& setBodyFile "examples/sse/sse.html"
|
||||
@ -40,7 +40,7 @@ api eventSource = do
|
||||
|
||||
sse :: EventSource -> Okapi Response
|
||||
sse eventSource = do
|
||||
pathParam @String `equals` "events"
|
||||
pathParam @String `is` "events"
|
||||
ok
|
||||
& setBodyEventSource eventSource
|
||||
& return
|
||||
|
@ -132,13 +132,13 @@ todoAPI conn =
|
||||
healthCheck :: Okapi Response
|
||||
healthCheck = do
|
||||
methodGET
|
||||
optional $ pathParam @Text `equals` ""
|
||||
optional $ pathParam @Text `is` ""
|
||||
respond ok
|
||||
|
||||
getTodo :: Connection -> Okapi Response
|
||||
getTodo conn = do
|
||||
methodGET
|
||||
pathParam @Text `equals` "todos"
|
||||
pathParam @Text `is` "todos"
|
||||
todoID <- pathParam @Int
|
||||
maybeTodo <- lift $ selectTodo conn todoID
|
||||
case maybeTodo of
|
||||
@ -148,7 +148,7 @@ getTodo conn = do
|
||||
getAllTodos :: Connection -> Okapi Response
|
||||
getAllTodos conn = do
|
||||
methodGET
|
||||
pathParam @Text `equals` "todos"
|
||||
pathParam @Text `is` "todos"
|
||||
status <- optional $ queryParam @TodoStatus "status"
|
||||
todos <- lift $ selectAllTodos conn status
|
||||
ok & setJSON todos & respond
|
||||
@ -156,7 +156,7 @@ getAllTodos conn = do
|
||||
createTodo :: Connection -> Okapi Response
|
||||
createTodo conn = do
|
||||
methodPOST
|
||||
pathParam @Text `equals` "todos"
|
||||
pathParam @Text `is` "todos"
|
||||
todoForm <- bodyForm
|
||||
lift $ insertTodoForm conn todoForm
|
||||
respond ok
|
||||
@ -164,7 +164,7 @@ createTodo conn = do
|
||||
editTodo :: Connection -> Okapi Response
|
||||
editTodo conn = do
|
||||
methodPUT
|
||||
equals @Text pathParam "todos"
|
||||
is @Text pathParam "todos"
|
||||
todoID <- pathParam @Int
|
||||
todoForm <- bodyForm @TodoForm
|
||||
lift $ updateTodo conn todoID todoForm
|
||||
@ -173,7 +173,7 @@ editTodo conn = do
|
||||
forgetTodo :: Connection -> Okapi Response
|
||||
forgetTodo conn = do
|
||||
methodDELETE
|
||||
pathParam @Text `equals` "todos"
|
||||
pathParam @Text `is` "todos"
|
||||
todoID <- pathParam @Int
|
||||
lift $ deleteTodo conn todoID
|
||||
respond ok
|
||||
|
@ -1,4 +1,14 @@
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-# LANGUAGE TypeApplications #-}
|
||||
|
||||
module Main where
|
||||
|
||||
import Data.Text
|
||||
import Okapi
|
||||
import Rel8
|
||||
|
||||
main :: IO ()
|
||||
main = undefined
|
||||
main = run id $ do
|
||||
methodGET
|
||||
pathParam @Text `is` "hello"
|
||||
return $ setHTML "<h1>Hello world</h1>" $ ok
|
||||
|
@ -5,7 +5,7 @@ cabal-version: 1.12
|
||||
-- see: https://github.com/sol/hpack
|
||||
|
||||
name: okapi
|
||||
version: 0.1.0.2
|
||||
version: 0.2.0.0
|
||||
description: Please see the README on GitHub at <https://github.com/githubuser/okapi#readme>
|
||||
category: Web
|
||||
homepage: https://github.com/MonadicSystems/okapi#readme
|
||||
|
@ -1,5 +1,5 @@
|
||||
name: okapi
|
||||
version: 0.1.0.2
|
||||
version: 0.2.0.0
|
||||
github: "MonadicSystems/okapi"
|
||||
license: BSD3
|
||||
author: "Monadic Systems LLC"
|
||||
|
30
src/Okapi.hs
30
src/Okapi.hs
@ -18,7 +18,7 @@
|
||||
{-# LANGUAGE ViewPatterns #-}
|
||||
{-# OPTIONS_HADDOCK show-extensions #-}
|
||||
|
||||
-- | Okapi is the microframework you should use if you get the chance.
|
||||
-- | Okapi is a micro web framework.
|
||||
module Okapi
|
||||
( -- * Parsing
|
||||
-- $parsers
|
||||
@ -99,7 +99,7 @@ module Okapi
|
||||
|
||||
-- ** Combinators
|
||||
-- $combinators
|
||||
equals,
|
||||
is,
|
||||
satisfies,
|
||||
Okapi.look,
|
||||
module Combinators,
|
||||
@ -458,31 +458,31 @@ method = do
|
||||
pure method'
|
||||
|
||||
methodGET :: MonadOkapi m => m ()
|
||||
methodGET = equals method $ Just HTTP.methodGet
|
||||
methodGET = is method $ Just HTTP.methodGet
|
||||
|
||||
methodPOST :: MonadOkapi m => m ()
|
||||
methodPOST = equals method $ Just HTTP.methodPost
|
||||
methodPOST = is method $ Just HTTP.methodPost
|
||||
|
||||
methodHEAD :: MonadOkapi m => m ()
|
||||
methodHEAD = equals method $ Just HTTP.methodHead
|
||||
methodHEAD = is method $ Just HTTP.methodHead
|
||||
|
||||
methodPUT :: MonadOkapi m => m ()
|
||||
methodPUT = equals method $ Just HTTP.methodPut
|
||||
methodPUT = is method $ Just HTTP.methodPut
|
||||
|
||||
methodDELETE :: MonadOkapi m => m ()
|
||||
methodDELETE = equals method $ Just HTTP.methodDelete
|
||||
methodDELETE = is method $ Just HTTP.methodDelete
|
||||
|
||||
methodTRACE :: MonadOkapi m => m ()
|
||||
methodTRACE = equals method $ Just HTTP.methodTrace
|
||||
methodTRACE = is method $ Just HTTP.methodTrace
|
||||
|
||||
methodCONNECT :: MonadOkapi m => m ()
|
||||
methodCONNECT = equals method $ Just HTTP.methodConnect
|
||||
methodCONNECT = is method $ Just HTTP.methodConnect
|
||||
|
||||
methodOPTIONS :: MonadOkapi m => m ()
|
||||
methodOPTIONS = equals method $ Just HTTP.methodOptions
|
||||
methodOPTIONS = is method $ Just HTTP.methodOptions
|
||||
|
||||
methodPATCH :: MonadOkapi m => m ()
|
||||
methodPATCH = equals method $ Just HTTP.methodPatch
|
||||
methodPATCH = is method $ Just HTTP.methodPatch
|
||||
|
||||
methodEnd :: MonadOkapi m => m ()
|
||||
methodEnd = do
|
||||
@ -692,8 +692,8 @@ vaultWipe = State.modify (\state -> state {stateVault = Vault.empty})
|
||||
|
||||
-- $combinators
|
||||
|
||||
equals :: (Eq a, MonadOkapi m) => m a -> a -> m ()
|
||||
equals action desired = satisfies action (desired ==)
|
||||
is :: (Eq a, MonadOkapi m) => m a -> a -> m ()
|
||||
is action desired = satisfies action (desired ==)
|
||||
|
||||
satisfies :: (Eq a, MonadOkapi m) => m a -> (a -> Bool) -> m ()
|
||||
satisfies action predicate = do
|
||||
@ -1073,13 +1073,13 @@ applyMiddlewares middlewares handler =
|
||||
-- TODO: Is this needed? Idea taken from OCaml Dream framework
|
||||
|
||||
scope :: MonadOkapi m => Path -> [Middleware m] -> Middleware m
|
||||
scope prefix middlewares handler = path `equals` prefix >> applyMiddlewares middlewares handler
|
||||
scope prefix middlewares handler = path `is` prefix >> applyMiddlewares middlewares handler
|
||||
|
||||
clearHeadersMiddleware :: MonadOkapi m => Middleware m
|
||||
clearHeadersMiddleware handler = setHeaders [] <$> handler
|
||||
|
||||
prefixPathMiddleware :: MonadOkapi m => Path -> Middleware m
|
||||
prefixPathMiddleware prefix handler = path `equals` prefix >> handler
|
||||
prefixPathMiddleware prefix handler = path `is` prefix >> handler
|
||||
|
||||
-- $routing
|
||||
--
|
||||
|
Loading…
Reference in New Issue
Block a user