本章為 scotty 系列教程的第二篇,讀者可先閱讀 第一篇 學(xué)習(xí)如何使用 stack 構(gòu)建一個(gè) Haskell 的項(xiàng)目。
本章的目標(biāo)是使用 scotty 構(gòu)建一個(gè) echo server,用 curl 測(cè)試如下:
$ curl http://127.0.0.1:3000/?echo=abc
abc
$ curl http://127.0.0.1:3000/ -d echo=cdf
cdf
我們繼續(xù)使用 上篇 所建立的 helloworld 為基礎(chǔ)建立 echo server。
修改 src/Lib.hs 如下:
{-# LANGUAGE OverloadedStrings #-}
module Lib
( someFunc
) where
import Web.Scotty
someFunc :: IO ()
someFunc = scotty 3000 $ do
get "/" $ do
echo <- param "echo"
text echo
post "/" $ do
echo <- param "echo"
text echo
這里用了 get 和 post 的方法,然后通過 param 獲取 echo 的信息然后輸出。
編譯一下:
$ stack build --system-ghc
Warning: File listed in helloworld.cabal file does not exist: README.md
helloworld-0.1.0.0: build (lib + exe)
Preprocessing library helloworld-0.1.0.0...
[1 of 1] Compiling Lib ( src/Lib.hs, .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/Lib.o )
/Users/lmj/repo/dispatch/helloworld/src/Lib.hs:7:1: error:
Failed to load interface for ‘Web.Scotty’
Use -v to see a list of the files searched for.
Warning: File listed in helloworld.cabal file does not exist: README.md
-- While building package helloworld-0.1.0.0 using:
/Users/lmj/.stack/setup-exe-cache/x86_64-osx/Cabal-simple_mPHDZzAJ_1.24.2.0_ghc-8.0.2 --builddir=.stack-work/dist/x86_64-osx/Cabal-1.24.2.0 build lib:helloworld exe:helloworld-exe --ghc-options " -ddump-hi -ddump-to-file"
Process exited with code: ExitFailure 1
編譯器抱怨沒有找到 Web.Scotty,這是缺少依賴 scotty 導(dǎo)致的。
打開 helloworld.cabal, 將 scotty 添加到 library 的 build-depends, 如下:
library
hs-source-dirs: src
exposed-modules: Lib
build-depends: base >= 4.7 && < 5
, scotty
default-language: Haskell2010
重新編譯一下:
$ stack build --system-ghc
Warning: File listed in helloworld.cabal file does not exist: README.md
fail-4.9.0.0: using precompiled package
nats-1.1.1: using precompiled package
regex-base-0.93.2: using precompiled package
regex-posix-0.95.2: using precompiled package
regex-compat-0.95.1: using precompiled package
scotty-0.11.0: using precompiled package
helloworld-0.1.0.0: configure (lib + exe)
Configuring helloworld-0.1.0.0...
helloworld-0.1.0.0: build (lib + exe)
Preprocessing library helloworld-0.1.0.0...
[1 of 1] Compiling Lib ( src/Lib.hs, .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/Lib.o )
Preprocessing executable 'helloworld-exe' for helloworld-0.1.0.0...
[1 of 1] Compiling Main ( app/Main.hs, .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/helloworld-exe/helloworld-exe-tmp/Main.o ) [Lib changed]
Linking .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/helloworld-exe/helloworld-exe ...
Warning: File listed in helloworld.cabal file does not exist: README.md
helloworld-0.1.0.0: copy/register
Installing library in
/Users/lmj/repo/dispatch/helloworld/.stack-work/install/x86_64-osx/lts-8.5/8.0.2/lib/x86_64-osx-ghc-8.0.2/helloworld-0.1.0.0-1f3dj2GjzoiFgLQm1bUvr4
Installing executable(s) in
/Users/lmj/repo/dispatch/helloworld/.stack-work/install/x86_64-osx/lts-8.5/8.0.2/bin
Registering helloworld-0.1.0.0...
Completed 7 action(s).
接下來運(yùn)行測(cè)試一下:
$ stack exec --system-ghc helloworld-exe
Setting phasers to stun... (port 3000) (ctrl-c to quit)
打開另外一個(gè)終端測(cè)試:
$ curl http://127.0.0.1:3000/?echo=abc
abc
$ curl http://127.0.0.1:3000/ -d echo=cdf
cdf
到這里整個(gè) echo server 基本完成。
繼續(xù)看 src/Lib.hs 的代碼,get 和 post 的處理代碼是一樣的,我們是否寫成一個(gè)函數(shù)呢? 答案是可以的。
閱讀文檔發(fā)現(xiàn) get 和 post 定義如下:
get :: RoutePattern -> ActionM () -> ScottyM ()
post :: RoutePattern -> ActionM () -> ScottyM ()
所以可以定義一個(gè) echoHandler 如下:
echoHandler :: ActionM ()
echoHandler = do
echo <- param "echo"
text echo
修改 src/Lib.hs 如下:
{-# LANGUAGE OverloadedStrings #-}
module Lib
( someFunc
) where
import Web.Scotty
someFunc :: IO ()
someFunc = scotty 3000 $ do
get "/" echoHandler
post "/" echoHandler
echoHandler :: ActionM ()
echoHandler = do
echo <- param "echo"
text echo
編譯測(cè)試如上,由讀者自行完成。