scotty 系列教程之前言

scotty 系列教程是建立在读者具有一定 haskell 基础的前提上,
若没有请移步去阅读一本关于 haskell 编程的书,如:《Real World Haskell》。

scotty 系列教程将一步一步引导读者开发一个 coin 项目。涉及的内容如下:

  • 如何使用 stack 快速构建 haskell 项目
  • 第一个应用 echo
  • 访问 mysql 数据库
  • 使用 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 使用的最新的模版创建 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 编译器。
我们可以简单执行 stack setup 来安装 ghc 编译器。
我自己是使用系统的 ghc brew install ghc,所以后面的教程使用的都是系统的 ghc

加了 --system-ghc 参数重新编译

$ 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...

编译成功了,我们执行一下它:

$ 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!"

编译执行一下:

$ 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!

课后作业

src/Lib.hsapp/Main.hs 里面的 someFunc 函数名也应该做相应的改动,这些就留给读者自己完成了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,653评论 25 709
  • //Clojure入门教程: Clojure – Functional Programming for the J...
    葡萄喃喃呓语阅读 9,226评论 0 7
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,477评论 19 139
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,223评论 4 61
  • 走在冬雨的甬道上, 风掠过满地的沧桑。 万木调落的季节里, 就连翠竹也抵不住昨日的霜。 桂花却在寒冷中绽放, 绿萝...
    冬蝉子阅读 3,385评论 2 2