haskell - Adding response header in Servant -
i trying figure out how add cors
response header in servant (basically, set response header "access-control-allow-origin: *"). wrote small test case below addheader
function errors out. appreciate figuring out error below.
code:
{-# language cpp #-} {-# language datakinds #-} {-# language derivegeneric #-} {-# language typefamilies #-} {-# language typeoperators #-} {-# language overloadedstrings #-} module main import data.aeson import ghc.generics import network.wai import servant import network.wai.handler.warp (run) import control.monad.trans.either import control.monad.io.class (liftio) import control.monad (when, (<$!>)) import data.text t import data.configurator c import data.maybe import system.exit (exitfailure) data user = user { name :: t.text , password :: t.text } deriving (eq, show, generic) instance tojson user instance fromjson user type token = t.text type userapi = "grant" :> reqbody '[json] user :> post '[json] (headers '[header "access-control-allow-origin" t.text] token) userapi :: proxy userapi userapi = proxy authuser :: user -> bool authuser u = case (password u) of "somepass" -> true _ -> false server :: server userapi server = users users :: user -> eithert servanterr io token users u = case (authuser u) of true -> return $ addheader "*" $ ("ok" :: token) false -> return $ addheader "*" $ ("notok" :: token) app :: application app = serve userapi server main :: io () main = run 8081 app
this error get:
src/test.hs:43:10: couldn't match type ‘headers '[header "access-control-allow-origin" text] text’ ‘text’ expected type: server userapi actual type: user -> eithert servanterr io token in expression: users in equation ‘server’: server = users users :: user -> eithert servanterr io token users u = case (authuser u) of { true -> return $ addheader "*" $ ("something" :: token) false -> return $ addheader "*" $ ("something" :: token) } src/test.hs:46:28: couldn't match type ‘text’ ‘headers '[header h v0] text’ in expression: addheader "*" in second argument of ‘($)’, namely ‘addheader "*" $ ("something" :: token)’ in expression: return $ addheader "*" $ ("something" :: token) src/test.hs:47:29: couldn't match type ‘text’ ‘headers '[header h1 v1] text’ in expression: addheader "*" in second argument of ‘($)’, namely ‘addheader "*" $ ("something" :: token)’ in expression: return $ addheader "*" $ ("something" :: token)
i have working version simpler api (simple get
) works. but, userapi
of above type, errors out. addheader
function type seems agree type signature way think it. missing here or won't error out this.
madjar suggested this, expand upon it: addheader
changes return type:
x :: int x = 5 y :: headers '[header "someheader" string] int y = addheader "headerval" y
in case, means have update type of users
binding return either servanterr io (headers '[header "access-control-allow-origin" t.text] token
more generally, can use :kind! server userapi
in ghci see type synonym expands - that's helpful servant!
Comments
Post a Comment