Skip to content

Instantly share code, notes, and snippets.

@boj
Created November 15, 2017 08:47
Show Gist options
  • Save boj/8249f7786af8227b06a1a9713f56c7eb to your computer and use it in GitHub Desktop.
Save boj/8249f7786af8227b06a1a9713f56c7eb to your computer and use it in GitHub Desktop.
Show how to use Haskell type symbols in Servant APIs to generate string URLs
#! /usr/bin/env nix-shell
#! nix-shell -i 'runghc -XOverloadedStrings -XDataKinds -XTypeOperators' -p 'ghc.withPackages (pkgs: [ pkgs.servant-server ])'
module Main where
import Data.List
import Data.Proxy
import GHC.TypeLits
import Servant
type ApiTerm = "api"
type VersionTerm = "v1"
type FooTerm = "foo"
type BarTerm = "bar"
type Api = ApiTerm :> VersionTerm :> (
FooTerm :> Get '[JSON] ()
:<|> BarTerm :> Get '[JSON] ()
)
main :: IO ()
main = do
let a = symbolVal (Proxy :: Proxy ApiTerm)
b = symbolVal (Proxy :: Proxy VersionTerm)
c = symbolVal (Proxy :: Proxy FooTerm)
f = "/" ++ intercalate "/" [a, b, c]
print f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment