ServantのFormサンプル
POST
でForm
データを送信するServant
の実装サンプルを、本家サイトも含めて探していたのですが、見つけられませんでした。結果的にはとっても簡単でした。FromForm
のインスタンスにするだけでした。
data Res = Res { ret :: Int} deriving (Eq, Show, Generic) -- Formデータを受け取るデータ構造 instance FromForm Req --- FromFormのインスタンスにする。
実行
$ 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.
stack
はcabal 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.
ログ全体
PythonでBlenderのオブジェクトを操作する(Quaternion)
Quaternion
を使ってオブジェクトを回転させます。
Math Types & Utilities (mathutils) — Blender 2.63.7 - API documentation
オブジェクトの移動がわかりやすくなるように目安の円を描きます。
bpy.ops.mesh.primitive_circle_add(location=(0,0,0), radius=4, rotation=(0,0,0)) bpy.ops.mesh.primitive_circle_add(location=(0,0,0), radius=4, rotation=(pi/2,0,0)) bpy.ops.mesh.primitive_circle_add(location=(0,0,0), radius=4, rotation=(pi/2,0,pi/2))
トーラスを描きます。
bpy.ops.mesh.primitive_torus_add(location=(4,0,0), major_radius=0.5) obj = bpy.context.active_object
Quaternion
を使ってトーラスを移動します。
Quaternion
のベクトル(虚部)を(0,0,1)
にとって、z軸を中心に回転をさせていきます。
obj.location.rotate(Quaternion((cos((pi/16)/2), 0.0, 0.0, sin((pi/16)/2)))) obj.location.rotate(Quaternion((cos((pi/16)/2), 0.0, 0.0, sin((pi/16)/2)))) obj.location.rotate(Quaternion((cos((pi/16)/2), 0.0, 0.0, sin((pi/16)/2))))
円の上にトーラスが乗っていることがわかります。
最小のServantのサンプル
$ curl http://127.0.0.1:8081/myapi [{"name":"abc","id":1}]