scotty 系列教程之 echo server

本章為 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

這里用了 getpost 的方法,然后通過 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 添加到 librarybuild-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 的代碼,getpost 的處理代碼是一樣的,我們是否寫成一個(gè)函數(shù)呢? 答案是可以的。

閱讀文檔發(fā)現(xiàn) getpost 定義如下:

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è)試如上,由讀者自行完成。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,534評(píng)論 19 139
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,901評(píng)論 25 709
  • 關(guān)于JavaScript繼承這一塊,其實(shí)困擾了我很久,更多的是強(qiáng)行記憶,沒有真正的理解,我看了很多書籍,博客關(guān)于J...
    moonburn閱讀 484評(píng)論 0 8
  • 1 上學(xué)≠學(xué)習(xí)最近與身邊的人聊天,發(fā)現(xiàn)許多工作中的人都是不讀書的,下班的業(yè)余時(shí)間都是在追劇,或者干別的事情,我對(duì)此...
    致遠(yuǎn)007閱讀 747評(píng)論 0 0
  • 是夜 明明四周安靜得一塌糊涂,我卻只覺得吵 像是有千百只蚊蟲在耳邊嗡嗡嚷嚷地亂叫 我似乎覺著有些耳鳴眼花 原諒我竟...
    若許寧閱讀 247評(píng)論 0 0

友情鏈接更多精彩內(nèi)容