git.iko.soy/typeable/servant
on f76c729 help sign in
50 lines 1.4 kB history raw
1{-# LANGUAGE TypeFamilies #-}
2{-# LANGUAGE FlexibleContexts #-}
3{-# LANGUAGE OverloadedStrings #-}
5-- | This module lets you implement 'Server's for defined APIs. You'll
6-- most likely just need 'serve'.
7module Servant.Server
8 ( -- * Run a wai application from an API
9 serve
11 , -- * Construct a wai Application from an API
12 toApplication
14 , -- * Handlers for all standard combinators
15 HasServer(..)
16 , Server
17 ) where
19import Data.Proxy (Proxy)
20import Network.Wai (Application)
21import Servant.API (Canonicalize, canonicalize)
22import Servant.Server.Internal
25-- * Implementing Servers
27-- | 'serve' allows you to implement an API and produce a wai 'Application'.
28--
29-- Example:
30--
31-- > type MyApi = "books" :> Get '[JSON] [Book] -- GET /books
32-- > :<|> "books" :> ReqBody Book :> Post '[JSON] Book -- POST /books
33-- >
34-- > server :: Server MyApi
35-- > server = listAllBooks :<|> postBook
36-- > where listAllBooks = ...
37-- > postBook book = ...
38-- >
39-- > myApi :: Proxy MyApi
40-- > myApi = Proxy
41-- >
42-- > app :: Application
43-- > app = serve myApi server
44-- >
45-- > main :: IO ()
46-- > main = Network.Wai.Handler.Warp.run 8080 app
47serve :: HasServer (Canonicalize layout) => Proxy layout -> Server layout -> Application
48serve p server = toApplication (route (canonicalize p) server)
50type Server layout = Server' (Canonicalize layout)