git.iko.soy/typeable/servant
on master help sign in
62 lines 2.0 kB history raw
1# Serving web applications over HTTPS
3This short recipe shows how one can serve a servant application
4over HTTPS, by simply using `warp-tls` instead of `warp` to provide
5us a `run` function for running the `Application` that we get by
6calling `serve`.
8As usual, we start by clearing our throat of a few language extensions
9and imports.
11``` haskell
12{-# LANGUAGE DataKinds #-}
13{-# LANGUAGE TypeOperators #-}
14import Network.Wai
15import Network.Wai.Handler.Warp
16import Network.Wai.Handler.WarpTLS
17import Servant
18```
20No need to work with a complicated API here, let's
21make it as simple as it gets:
23``` haskell
24type API = Get '[JSON] Int
26api :: Proxy API
27api = Proxy
29server :: Server API
30server = return 10
32app :: Application
33app = serve api server
34```
36It's now time to actually run the `Application`.
37The [`warp-tls`](https://hackage.haskell.org/package/warp-tls/docs/Network-Wai-Handler-WarpTLS.html)
38package provides two functions for running an `Application`, called
39[`runTLS`](https://hackage.haskell.org/package/warp-tls/docs/Network-Wai-Handler-WarpTLS.html#v:runTLS)
40and [`runTLSSocket`](https://hackage.haskell.org/package/warp-tls/docs/Network-Wai-Handler-WarpTLS.html#v:runTLSSocket).
41We will be using the first one.
43It takes two arguments,
44[the TLS settings](https://hackage.haskell.org/package/warp-tls/docs/Network-Wai-Handler-WarpTLS.html#t:TLSSettings)
45(certificates, keys, ciphers, etc)
46and [the warp settings](https://hackage.haskell.org/package/warp/docs/Network-Wai-Handler-Warp-Internal.html#t:Settings)
47(port, logger, etc).
49We will be using very simple settings for this example but you are of
50course invited to read the documentation for those types to find out
51about all the knobs that you can play with.
53``` haskell
54main :: IO ()
55main = runTLS tlsOpts warpOpts app
57 where tlsOpts = tlsSettings "cert.pem" "secret-key.pem"
58 warpOpts = setPort 8080 defaultSettings
59```
61This program is available as a cabal project
62[here](https://github.com/haskell-servant/servant/tree/master/doc/cookbook/https).