scotty 系列教程之前言

作者: Lupino | 来源:发表于2017-03-16 14:47 被阅读92次

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 函数名也应该做相应的改动,这些就留给读者自己完成了。

相关文章

  • scotty 系列教程之前言

    scotty 系列教程是建立在读者具有一定 haskell 基础的前提上,若没有请移步去阅读一本关于 haskel...

  • scotty 系列教程之 echo server

    本章为 scotty 系列教程的第二篇,读者可先阅读 第一篇 学习如何使用 stack 构建一个 Haskell...

  • CocoaPods学习笔记之一

    前言 看了Cocoapods系列教程了,很赞,本篇文章纯属笔记,原创地址Cocoapods系列教程(一)——入门 ...

  • 30 行代码绘出你的微信朋友统计图

    前言 大家好,这里是「brucepk」爬虫 系列教程。此文首发于「brucepk」公众号,欢迎大家关注。此系列教程...

  • 【IntelliJ IDEA学习---一、IntelliJ ID

    前言 本系列教程从 IntelliJ IDEA 的安装、卸载、软件设置、项目配置等各个方面进行讲解。通过本系列教程...

  • springboot系列教程导学篇

    spring boot2.0系列教程学习之导学篇 springboot 2.0深度学习系列教程。 Spring B...

  • Material Design系列教程 - 前言

    Material Design 谷歌在发布 Android 5.0 Lollipop 的时候,同时为安卓应用引进了...

  • Scotty

    “一位颓废的音乐诗人Scott,在晨曦中痛饮最后一杯威士忌悄然离去,在也没人知道他去了哪里,也没有人关心他是否还活...

  • Scotty

    听着听着,开着灯陷入了黑暗。 断不了的,是空洞的嘈杂。 空气慢慢下沉,传来威士忌的醇华。 Allan Taylor...

  • Scotty

    比较喜欢的一首歌,特别是他的歌词Scotty - Allan Taylor Scotty checked out ...

网友评论

    本文标题:scotty 系列教程之前言

    本文链接:https://www.haomeiwen.com/subject/dscjnttx.html