2

i am using servant and Network.Wai.Application.Static for json api and static file serving respectively. I want to join these two Application such that if staticApp fails to serve, request should go to jsonApp.

I tried finding out and read how websocketsOr has done it.WebsocketsOr

I finally wrote :

app :: NW.Application -> NW.Application -> NW.Application
app staticApp apiApp req respond =
  staticApp req (\ response ->
                  do
                    if (Status.statusCode . NW.responseStatus $ response) < 300
                    then respond response
                    else apiApp req respond)

But it seems like someone else (wai itself) would have done it, handling many possible cases.. But i am not able to find.

What are the edges cases i am missing (exceptions ?? ) and what other api can i use to do the same ?

ase
  • 13,231
  • 4
  • 34
  • 46
Ashish Negi
  • 5,193
  • 8
  • 51
  • 95

1 Answers1

1

I asked this on #servant channel and alpounet told me about servant Raw that allows to put our own Application handler. They even have a serveDirectory api as well :)

import qualified Servant as S
import Servant ((:>), (:<|>))

type JobApi = "job" :> "status" :> S.Capture "id" ST.JobId :> S.Get '[S.JSON] ST.JobStatus
         :<|> "job" :> S.ReqBody '[S.JSON] ST.JobPostBody :> S.Post '[S.JSON] ST.JobId
         :<|> "job" :> S.Capture "id" ST.JobId :> S.Get '[S.JSON] ST.JobResult
         :<|> S.Raw

serverRouter :: ST.Server -> S.Server JobApi
serverRouter server = statusGet
    S.:<|> jobPost
    S.:<|> jobGet
    S.:<|> S.serveDirectory "assets"
  where 
    -- ... so on
Ashish Negi
  • 5,193
  • 8
  • 51
  • 95