IT練習ノート

IT関連で調べたこと(実際は嵌ったこと)を書いています。

3分でHaskellのWebアプリ

のサンプルを動かします。但し、待ち時間を除きます。

Beginning Haskell - A Project-Based Approach | Alejandro Serrano Mena | ApressのWebアプリの説明では、フレームワークとしてscottyを使っているので、導入してみました。

バージョンの確認

webapp $ cabal -version
cabal: unrecognised command: -version (try --help)
webapp $ cabal --version
cabal-install version 1.24.0.0
compiled using version 1.24.0.0 of the Cabal library 
webapp $ cabal repl
GHCi, version 8.0.1: http://www.haskell.org/ghc/  :? for help
Prelude> :q
Leaving GHCi.

作業場所の作成

webapp $ mkdir scotty02
webapp $ cd scotty02
scotty02 $ ls
scotty02 $ cabal sandbox init
Writing a default package environment file to
/Users//work03/webapp/scotty02/cabal.sandbox.config
Creating a new sandbox at
/Users//work03/webapp/scotty02/.cabal-sandbox

scottyのインストール

scotty02 $ cabal install scotty
Resolving dependencies...
Notice: installing into a sandbox located at
/Users//work03/webapp/scotty02/.cabal-sandbox
Downloading ansi-terminal-0.6.2.3...
Downloading appar-0.1.4...
Configuring base64-bytestring-1.0.0.1...
Configuring base-compat-0.9.1...

途中省略

Installed wai-extra-3.0.19
Downloading scotty-0.11.0...
Configuring scotty-0.11.0...
Building scotty-0.11.0...
Installed scotty-0.11.0
Updating documentation index

サンプルアプリケーションの作成

scotty02 $ cat hello.hs 
{-# LANGUAGE OverloadedStrings #-}

import Web.Scotty

main :: IO ()
main = scotty 3010 $ do
  get "/" $ do
    html "Hello, Scotty World!"

起動

scotty02 $ cabal exec runghc hello.hs 
Setting phasers to stun... (port 3010) (ctrl-c to quit)

Webアクセス

client$ curl http://localhost:3010/
Hello, Scotty World!
client$