scotty 系列教程之前言

scotty 系列教程是建立在讀者具有一定 haskell 基礎的前提上,
若沒有請移步去閱讀一本關于 haskell 編程的書,如:《Real World Haskell》。

scotty 系列教程將一步一步引導讀者開發(fā)一個 coin 項目。涉及的內容如下:

  • 如何使用 stack 快速構建 haskell 項目
  • 第一個應用 echo
  • 訪問 mysql 數(shù)據(jù)庫
  • 使用 haxl 框架
  • rest ful json api
  • 使用 graphql
  • 結語

預備工作

haskell 所提供 cabal 構建工具相對來將比較復雜,所以我選擇使用 stack 構建工具。

安裝 stack

詳細的安裝請自行查看文檔。
我自己使用的是 gentoo,安裝如下:

emerge --ask dev-haskell/stack 

mac os 安裝如下:

brew install haskell-stack 

第一個項目 hello world

$ stack new helloworld
Downloading template "new-template" to create project "helloworld" in helloworld/ ...
Looking for .cabal or package.yaml files to use to init the project.
Using cabal packages:
- helloworld/helloworld.cabal

Selecting the best among 10 snapshots...

Downloaded lts-8.5 build plan.
* Matches lts-8.5

Selected resolver: lts-8.5
Initialising configuration using resolver: lts-8.5
Total number of user packages considered: 1
Writing configuration to file: helloworld/stack.yaml
All done.

我們從日志可以看到 stack 使用的最新的模版創(chuàng)建 helloworld 這個項目,并且下載了最新的編譯解決方案 lts-8.5。

編譯

我們進入 helloworld 目錄進行編譯:

$ cd helloworld 
$ stack build 
No compiler found, expected minor version match with ghc-8.0.2 (x86_64) (based on resolver setting in /Users/lmj/repo/dispatch/helloworld/stack.yaml).
To install the correct GHC into /Users/lmj/.stack/programs/x86_64-osx/, try running "stack setup" or use the "--install-ghc" flag. To use your system GHC installation, run "stack config set system-ghc --global true", or use the "--system-ghc" flag.

報錯了,從日志可以知道 stack 沒有找到 ghc 編譯器。
我們可以簡單執(zhí)行 stack setup 來安裝 ghc 編譯器。
我自己是使用系統(tǒng)的 ghc brew install ghc,所以后面的教程使用的都是系統(tǒng)的 ghc

加了 --system-ghc 參數(shù)重新編譯

$ stack build --system-ghc
Warning: File listed in helloworld.cabal file does not exist: README.md
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 )
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-44GC3BWnM1QFjPMM1ngxcQ
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...

編譯成功了,我們執(zhí)行一下它:

$ stack exec --system-ghc helloworld-exe 
someFunc

哈哈,成功了。

Hello World!

下面我們把 someFunc 改成我們要的 Hello World!。
我們看一下目錄樹:

$ tree 
.
├── LICENSE
├── Setup.hs
├── app
│   └── Main.hs
├── helloworld.cabal
├── src
│   └── Lib.hs
├── stack.yaml
└── test
    └── Spec.hs

3 directories, 7 files

目錄樹中 app/Main.hshelloworld 的入口。
src/Lib.hshelloworld 的主代碼,專門給 app/Main.hs 調用的。

我們打開 src/Lib.hs:

module Lib
    ( someFunc
    ) where

someFunc :: IO ()
someFunc = putStrLn "someFunc"

putStrLn "someFunc" 就是輸出的信息,我們把 someFunc 改為 Hello World! 今天的教程就完工。

改完如下:

module Lib
    ( someFunc
    ) where

someFunc :: IO ()
someFunc = putStrLn "Hello World!"

編譯執(zhí)行一下:

$ stack build --system-ghc
Warning: File listed in helloworld.cabal file does not exist: README.md
helloworld-0.1.0.0: unregistering (local file changes: src/Lib.hs)
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-44GC3BWnM1QFjPMM1ngxcQ
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...

$ stack exec --system-ghc helloworld-exe
Hello World!

課后作業(yè)

src/Lib.hsapp/Main.hs 里面的 someFunc 函數(shù)名也應該做相應的改動,這些就留給讀者自己完成了。

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

相關閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,113評論 25 709
  • //Clojure入門教程: Clojure – Functional Programming for the J...
    葡萄喃喃囈語閱讀 4,053評論 0 7
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,591評論 19 139
  • 發(fā)現(xiàn) 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 15,410評論 4 61
  • 走在冬雨的甬道上, 風掠過滿地的滄桑。 萬木調落的季節(jié)里, 就連翠竹也抵不住昨日的霜。 桂花卻在寒冷中綻放, 綠蘿...
    冬蟬子閱讀 533評論 2 2

友情鏈接更多精彩內容