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.hs 為 helloworld 的入口。
src/Lib.hs 為 helloworld 的主代碼,專門給 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.hs 和 app/Main.hs 里面的 someFunc 函數(shù)名也應該做相應的改動,這些就留給讀者自己完成了。