1
0
mirror of https://github.com/alaudidaelark/coupon-servant.git synced 2026-03-07 22:12:34 +00:00

integrated a swagger json api endpoint

This commit is contained in:
2017-05-06 15:25:27 +05:30
parent 00523772ac
commit 5f19bd02ac
5 changed files with 23 additions and 14 deletions

View File

@@ -6,9 +6,11 @@
module Api (module Api,module Models) where
import Data.Proxy
import Data.Swagger
import Data.Text
import Models
import Servant.API
import SwaggerGen
type CouponApi =
"coupon" :> "add" :> ReqBody '[JSON] Coupon :> Post '[JSON] (Maybe Coupon)
@@ -18,7 +20,12 @@ type CouponApi =
type BillCouponApi =
"billcoupon" :> ReqBody '[JSON] BillCoupon :> Post '[JSON] CouponResult
type ServerApi = CouponApi :<|> BillCouponApi
type SwaggerApi = "swagger.json" :> Get '[JSON] Swagger
type ServerApi = CouponApi :<|> BillCouponApi :<|> SwaggerApi
couponApi :: Proxy (CouponApi :<|> BillCouponApi)
couponApi = Proxy
api :: Proxy ServerApi
api = Proxy

View File

@@ -17,6 +17,7 @@ import Network.Wai
import Network.Wai.Handler.Warp as Warp
import Network.Wai.Middleware.Cors
import Servant
import SwaggerGen
-- import Network.Wai.Middleware.RequestLogger (logStdoutDev)
couponServer :: ConnectionPool -> Server CouponApi
@@ -51,9 +52,11 @@ billCouponServer _ = billCouponComputeH
billCouponCompute bill = do print bill
return $ Applied 100
swaggerServer :: Server SwaggerApi
swaggerServer = liftIO $ return $ swaggerDoc couponApi
server :: ConnectionPool -> Server ServerApi
server pool = couponServer pool :<|> billCouponServer pool
server pool = couponServer pool :<|> billCouponServer pool :<|> swaggerServer
app :: ConnectionPool -> Application
@@ -74,5 +77,4 @@ mkApp sqliteFile = do
return $ app pool
run :: String -> IO ()
run dbConnStr =
Warp.run 3000 =<< mkPgApp dbConnStr
run dbConnStr = Warp.run 3000 =<< mkPgApp dbConnStr

View File

@@ -5,7 +5,6 @@
module SwaggerGen where
import Api
import Control.Lens
import Data.Aeson
import Data.Aeson.Types (camelTo2)
@@ -14,6 +13,8 @@ import Data.Proxy
import Data.String.Conversions
import Data.Swagger
import qualified Data.Text as T
import Models
import Servant.API
import Servant.Swagger
modifier :: String -> String
@@ -24,15 +25,17 @@ prefixSchemaOptions = defaultSchemaOptions { fieldLabelModifier = modifier }
instance ToSchema Coupon where declareNamedSchema = genericDeclareNamedSchema prefixSchemaOptions
swaggerDoc :: Swagger
swaggerDoc = toSwagger api
-- swaggerDoc :: Swagger
swaggerDoc :: HasSwagger api => Proxy api -> Swagger
swaggerDoc api = toSwagger api
& host ?~ Host {_hostName = "localhost",_hostPort = Just 3000}
& info.title .~ "Coupon Api"
& info.version .~ "v1"
-- & applyTagsFor billOp ["billcoupon" & description ?~ "Text"]
genSwaggerDoc :: IO ()
genSwaggerDoc = BL8.writeFile "swagger.json" (encode swaggerDoc)
-- genSwaggerDoc :: IO ()
-- genSwaggerDoc = BL8.writeFile "swagger.json" (encode swaggerDoc)
-- billOp :: Traversal' Swagger Operation
-- billOp = subOperations (Proxy :: Proxy BillCouponApi) (Proxy :: Proxy ServerApi)