-
-
Save adetokunbo/88ef73721a2e1a2179444f5b184a4bf3 to your computer and use it in GitHub Desktop.
servant redirects, new generation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{- | |
$ curl -X POST localhost:9876/dog -v | |
* Connected to localhost (127.0.0.1) port 9876 (#0) | |
> POST /dog HTTP/1.1 | |
> Host: localhost:9876 | |
> User-Agent: curl/7.55.1 | |
> Accept: */* | |
> | |
< HTTP/1.1 301 Moved Permanently | |
< Transfer-Encoding: chunked | |
< Date: Tue, 17 Oct 2017 12:08:01 GMT | |
< Server: Warp/3.2.13 | |
< Content-Type: application/json;charset=utf-8 | |
< Location: https://google.com/?q=dog | |
< | |
* Connection #0 to host localhost left intact | |
-} | |
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE KindSignatures #-} | |
{-# LANGUAGE TypeOperators #-} | |
import GHC.TypeLits | |
import Network.Wai.Handler.Warp | |
import Servant | |
type PostRedirect (code :: Nat) loc | |
= Verb 'POST code '[JSON] (Headers '[Header "Location" loc] NoContent) | |
redirect | |
:: ToHttpApiData loc | |
=> loc -- ^ what to put in the 'Location' header | |
-> Handler (Headers '[Header "Location" loc] NoContent) | |
redirect a = return (addHeader a NoContent) | |
type API = Capture "q" String :> PostRedirect 301 String | |
api :: Proxy API | |
api = Proxy | |
server :: Server API | |
server query = redirect ("https://google.com/?q=" ++ query) | |
main :: IO () | |
main = run 9876 (serve api server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment