IT練習ノート

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

ServantのFileUploadサンプル

package

servant-multipart: multipart/form-data (e.g file upload) support for servant

$ cabal install servant-multipart 
Resolving dependencies...
Notice: installing into a sandbox located at
foo/.cabal-sandbox
Downloading http-client-0.5.6.1...
Configuring natural-transformation-0.4...
Configuring http-client-0.5.6.1...
Building natural-transformation-0.4...

...

sample

servant file upload sample

client

$ curl -i -X PUT http://127.0.0.1:8081/file -F "upfile1=@MyWebApp_upload.hs" -F "xx=yy"
HTTP/1.1 100 Continue

HTTP/1.1 200 OK
Transfer-Encoding: chunked
Date: Tue, 14 Mar 2017 12:03:05 GMT
Server: Warp/3.2.9
Content-Type: application/json;charset=utf-8

{"msg":"\"upfile doesn't exist\"","len":-1}
$
$ curl -i -X PUT http://127.0.0.1:8081/file -F "upfile=@MyWebApp_upload.hs" -F "xx=yy"
HTTP/1.1 100 Continue

HTTP/1.1 200 OK
Transfer-Encoding: chunked
Date: Tue, 14 Mar 2017 12:03:13 GMT
Server: Warp/3.2.9
Content-Type: application/json;charset=utf-8

{"msg":"uploaded","len":1633}
$ ls -al MyWebApp_upload.hs 
-rw-r--r--  1 foo bar  1633  3 4 11:01 MyWebApp_upload.hs
$ 

server log

*Main> main
FileData {fdInputName = "upfile1", fdFileName = "MyWebApp_upload.hs", fdFileCType = "application/octet-stream", fdFilePath = "/var/folders/hw/p4bp49hd7v9_1j60sjvmhqnr0000gn/T/servant-multipart1974930991404280278.buf"}
not exist
FileData {fdInputName = "upfile", fdFileName = "MyWebApp_upload.hs", fdFileCType = "application/octet-stream", fdFilePath = "/var/folders/hw/p4bp49hd7v9_1j60sjvmhqnr0000gn/T/servant-multipart8933518161505795335.buf"}

tidalをreplから使う

1: SuperColliderを起動する。

2: SuperDirtを起動する。

SuperDirt.start;

3: ghciを起動、ライブラリの取り込み

$ cabal repl
GHCi, version 8.0.1: http://www.haskell.org/ghc/  :? for help
Prelude> import Sound.Tidal.Context  -- インポート

4: repl上でコーディングする

Prelude Sound.Tidal.Context> :t bpsUtils  -- 時間を取得する
bpsUtils :: IO (Double -> IO (), IO Rational)
Prelude Sound.Tidal.Context> let s = bpsUtils


Prelude Sound.Tidal.Context> :t superDirtSetters  -- パターン関数を取得する
superDirtSetters
  :: IO Time
     -> IO
          (ParamPattern -> IO (),
           (Time -> [ParamPattern] -> ParamPattern) -> ParamPattern -> IO ())
Prelude Sound.Tidal.Context> let dd = s >>= \x -> superDirtSetters (snd x)

Prelude Sound.Tidal.Context> :t sound
sound :: Pattern String -> ParamPattern
Prelude Sound.Tidal.Context> :t atom
atom :: a -> Pattern a
Prelude Sound.Tidal.Context> :t sound . atom -- 文字列からパターンを作成する
sound . atom :: String -> ParamPattern

Prelude Sound.Tidal.Context> dd >>= \x -> fst x (sound (atom "bd")) -- バスドラが鳴る

(solved) The pkg-config package 'libpcre' is required but it could not be found.

pkg-confgとして認識されているか確認する。

$ pkg-config --libs libpcre
Package libpcre was not found in the pkg-config search path.
Perhaps you should add the directory containing `libpcre.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libpcre' found

profileの編集

$ vim ~/.bash_profile 

.bash_profileに下記を追加。

export PKG_CONFIG_PATH=/usr/lib/pkgconfig

パスを登録

source ~/.bash_profile 

認識されていることを確認

$ pkg-config --libs libpcre
-lpcre  

再インストール

$ cabal install pcre-light-0.4.0.4 --reinstall
Resolving dependencies...
In order, the following will be installed:
pcre-light-0.4.0.4 (via: mysql-simple-0.4.0.1) (new version)
mysql-simple-0.4.0.1 (via: persistent-mysql-2.6) (reinstall) (changes: pcre-light-0.4.0.3 removed)
persistent-mysql-2.6 (reinstall) (changes: mysql-simple-0.4.0.1 removed)
Warning: Note that reinstalls are always dangerous. Continuing anyway...
Notice: installing into a sandbox located at
/work03/webapp/servant04/.cabal-sandbox
Configuring pcre-light-0.4.0.4...
Building pcre-light-0.4.0.4...
Installed pcre-light-0.4.0.4
Configuring mysql-simple-0.4.0.1...
Building mysql-simple-0.4.0.1...
Installed mysql-simple-0.4.0.1
Configuring persistent-mysql-2.6...
Building persistent-mysql-2.6...
Installed persistent-mysql-2.6
Updating documentation index
/work03/webapp/servant04/.cabal-sandbox/share/doc/x86_64-osx-ghc-8.0.1/index.html

ServantのFormサンプル

POSTFormデータを送信するServantの実装サンプルを、本家サイトも含めて探していたのですが、見つけられませんでした。結果的にはとっても簡単でした。FromFormインスタンスにするだけでした。

data Res = Res { ret :: Int} deriving (Eq, Show, Generic) -- Formデータを受け取るデータ構造

instance FromForm Req --- FromFormのインスタンスにする。

Servant Form Sample

実行

$ curl -w '\n' 'http://localhost:8081/add' --data 'x=1&y=1' -XPOST
{"ret":2}

送信したデータの型が不正の場合は400となる。

$ curl http://127.0.0.1:8081/add -d "x=1&y=a" -XPOST
could not parse: `a' (input does not start with a digit)

The pkg-config package 'libpcre' is required but it could not be found.

stackcabal hellを解決してくれるはずですが、上手くいかないケースもあるようです。servant,persistent,mysqlの組み合わせで環境を作ろうとしましたができませんでした。

新規作成

foo$ stack new servant06

ghcを取得

foo$ stack setup

ゾルバは8.3

resolver: lts-8.3

cabalファイルに必要なライブラリを記載

foo$ vim servant06.cabal 
 22 executable servant06-exe
 23   hs-source-dirs:      app
 24   main-is:             Main.hs
 25   ghc-options:         -threaded -rtsopts -with-rtsopts=-N
 26   build-depends:       base
 27                      , servant06
 28                      , servant-server 
 29                      , persistent
 30                      , persistent-mysql

ビルドする

foo$ stack build

....

servant06-0.1.0.0: unregistering (missing dependencies: persistent-mysql)
pcre-light-0.4.0.4: configure
base16-bytestring-0.1.1.6: download
mysql-0.1.4: download
base16-bytestring-0.1.1.6: configure
blaze-textual-0.2.1.0: download
base16-bytestring-0.1.1.6: build
mysql-0.1.4: configure
base16-bytestring-0.1.1.6: copy/register
mysql-0.1.4: build
blaze-textual-0.2.1.0: configure
blaze-textual-0.2.1.0: build
blaze-textual-0.2.1.0: copy/register
mysql-0.1.4: copy/register
Progress: 4/7
--  While building package pcre-light-0.4.0.4 using:
      /Users/bar/.stack/setup-exe-cache/x86_64-osx/setup-Simple-Cabal-1.24.2.0-ghc-8.0.2 --builddir=.stack-work/dist/x86_64-osx/Cabal-1.24.2.0 configure --with-ghc=/Users/bar/.stack/programs/x86_64-osx/ghc-8.0.2/bin/ghc --with-ghc-pkg=/Users/bar/.stack/programs/x86_64-osx/ghc-8.0.2/bin/ghc-pkg --user --package-db=clear --package-db=global --package-db=/Users/bar/.stack/snapshots/x86_64-osx/lts-8.3/8.0.2/pkgdb --libdir=/Users/bar/.stack/snapshots/x86_64-osx/lts-8.3/8.0.2/lib --bindir=/Users/bar/.stack/snapshots/x86_64-osx/lts-8.3/8.0.2/bin --datadir=/Users/bar/.stack/snapshots/x86_64-osx/lts-8.3/8.0.2/share --libexecdir=/Users/bar/.stack/snapshots/x86_64-osx/lts-8.3/8.0.2/libexec --sysconfdir=/Users/bar/.stack/snapshots/x86_64-osx/lts-8.3/8.0.2/etc --docdir=/Users/bar/.stack/snapshots/x86_64-osx/lts-8.3/8.0.2/doc/pcre-light-0.4.0.4 --htmldir=/Users/bar/.stack/snapshots/x86_64-osx/lts-8.3/8.0.2/doc/pcre-light-0.4.0.4 --haddockdir=/Users/bar/.stack/snapshots/x86_64-osx/lts-8.3/8.0.2/doc/pcre-light-0.4.0.4 --dependency=base=base-4.9.1.0 --dependency=bytestring=bytestring-0.10.8.1
    Process exited with code: ExitFailure 1
    Logs have been written to: /Users/bar/work03/webapp/servant06/.stack-work/logs/pcre-light-0.4.0.4.log

    Configuring pcre-light-0.4.0.4...
    setup-Simple-Cabal-1.24.2.0-ghc-8.0.2: The pkg-config package 'libpcre' is
    required but it could not be found.

extra-lib-dirsを指定しても同様

foo$ stack build --extra-lib-dirs="/usr/local/lib/pkgconfig" --extra-include-dirs="/usr/local/include"
pcre-light-0.4.0.4: configure
Progress: 1/4
--  While building package pcre-light-0.4.0.4 using:
      /Users/ogawanaoto/.stack/setup-exe-cache/x86_64-osx/setup-Simple-Cabal-1.24.2.0-ghc-8.0.2 --builddir=.stack-work/dist/x86_64-osx/Cabal-1.24.2.0 configure --with-ghc=/Users/ogawanaoto/.stack/programs/x86_64-osx/ghc-8.0.2/bin/ghc --with-ghc-pkg=/Users/ogawanaoto/.stack/programs/x86_64-osx/ghc-8.0.2/bin/ghc-pkg --user --package-db=clear --package-db=global --package-db=/Users/ogawanaoto/.stack/snapshots/x86_64-osx/lts-8.3/8.0.2/pkgdb --libdir=/Users/ogawanaoto/.stack/snapshots/x86_64-osx/lts-8.3/8.0.2/lib --bindir=/Users/ogawanaoto/.stack/snapshots/x86_64-osx/lts-8.3/8.0.2/bin --datadir=/Users/ogawanaoto/.stack/snapshots/x86_64-osx/lts-8.3/8.0.2/share --libexecdir=/Users/ogawanaoto/.stack/snapshots/x86_64-osx/lts-8.3/8.0.2/libexec --sysconfdir=/Users/ogawanaoto/.stack/snapshots/x86_64-osx/lts-8.3/8.0.2/etc --docdir=/Users/ogawanaoto/.stack/snapshots/x86_64-osx/lts-8.3/8.0.2/doc/pcre-light-0.4.0.4 --htmldir=/Users/ogawanaoto/.stack/snapshots/x86_64-osx/lts-8.3/8.0.2/doc/pcre-light-0.4.0.4 --haddockdir=/Users/ogawanaoto/.stack/snapshots/x86_64-osx/lts-8.3/8.0.2/doc/pcre-light-0.4.0.4 --dependency=base=base-4.9.1.0 --dependency=bytestring=bytestring-0.10.8.1 --extra-include-dirs=/usr/local/include --extra-lib-dirs=/usr/local/lib/pkgconfig
    Process exited with code: ExitFailure 1
    Logs have been written to: /Users/ogawanaoto/work03/webapp/servant06/.stack-work/logs/pcre-light-0.4.0.4.log

    Configuring pcre-light-0.4.0.4...
    setup-Simple-Cabal-1.24.2.0-ghc-8.0.2: The pkg-config package 'libpcre' is
    required but it could not be found.

ログ全体

The pkg-config package 'libpcre' is required but i …