packages feed

servant-multipart-client-0.12.2 / exe / Server.hs

{-# LANGUAGE DataKinds #-}{-# LANGUAGE TypeOperators #-}{-# LANGUAGE OverloadedStrings #-}{-# LANGUAGE TypeFamilies #-}import Control.Monadimport Control.Monad.IO.Classimport Network.Socket           (withSocketsDo)import Network.Wai.Handler.Warpimport Servantimport Servant.Multipartimport qualified Data.ByteString.Lazy as LBS-- Our API, which consists in a single POST endpoint at /-- that takes a multipart/form-data request body and-- pretty-prints the data it got to stdout before returning 0.type API = MultipartForm Mem (MultipartData Mem) :> Post '[JSON] Integerapi :: Proxy APIapi = Proxy-- The handler for our single endpoint.-- Its concrete type is:--   MultipartData -> Handler Integer---- MultipartData consists in textual inputs,-- accessible through its "inputs" field, as well-- as files, accessible through its "files" field.upload :: Server APIupload multipartData = liftIO $ do  putStrLn "Inputs:"  forM_ (inputs multipartData) $ \input ->    putStrLn $ "  " ++ show (iName input)          ++ " -> " ++ show (iValue input)  forM_ (files multipartData) $ \file -> do    let content = fdPayload file    putStrLn $ "Content of " ++ show (fdFileName file)    LBS.putStr content  return 0startServer :: IO ()startServer = run 8080 $ serve api uploadmain :: IO ()main = withSocketsDo startServer