Upgrade to Pro — share decks privately, control downloads, hide ads and more …

About mruby

y-yagi
July 20, 2018
87

About mruby

Ginza.rb 第61回

y-yagi

July 20, 2018
Tweet

Transcript

  1. mruby mruby rbenv install mruby-1.4.1 $ mirb mirb - Embeddable

    Interactive Ruby Shell > MRUBY_VERSION => "1.4.1" > nil&.to_s => nil > {"a" => 1}.transform_values {|v| v * 2 } => {"a"=>2}
  2. mrbgems mrbgems # build_config.rb conf.gem :core => 'mruby-eval' conf.gem :mgem

    => 'mruby-io' conf.gem :mgem => 'mruby-dir' conf.gem :mgem => 'mruby-mtest' conf.gem :mgem => 'mruby-onig-regexp' conf.gem :github => 'iij/mruby-require'
  3. "A utility for setting up a CLI with mruby that

    compiles binaries to Linux, OS X, and Windows." mrubyでCLIを作る為のボイラープレート 複数プラットフォーム向けのバイナリが作成出来る クロスコンパイル用のDockerイメージが提供されており Docker上でビルドを行う mruby-cli mruby-cli
  4. mruby-cli-docker mruby-cli-docker ... # install osx cross compiling tools RUN

    cd /opt/ && \ git clone https://github.com/tpoechtrager/osxcross.git COPY MacOSX10.10.sdk.tar.bz2 /opt/osxcross/tarballs/ RUN echo "\n" | bash /opt/osxcross/build.sh RUN rm /opt/osxcross/tarballs/* ENV PATH /opt/osxcross/target/bin:$PATH ENV SHELL /bin/bash # install msitools RUN cd /tmp && wget https://launchpad.net/ubuntu/+archive/primary/+files/gcab_0.6.ori && tar -xf gcab_0.6.orig.tar.xz && cd gcab-0.6 && ./configure && make && make install https://github.com/hone/mruby-cli- docker/blob/master/Dockerfile
  5. haconiwa haconiwa Namespace.unshare(Namespace::CLONE_NEWNS) Namespace.unshare(Namespace::CLONE_NEWPID) Mount.make_private "/" Mount.bind_mount "/var/lib/myroot", "/var/lib/haconiwa/root" Dir.chroot

    "/var/lib/haconiwa" Dir.chdir "/" c = Process.fork { Mount.mount "proc", "/proc", :type => "proc" Exec.exec "/bin/sh" } pid, ret = Process.waitpid2 c puts "Container exited with: #{ret.inspect}" https://github.com/haconiwa/haconiwa#programming-the- container-world-by-mruby
  6. 組み込み系 組み込み系 mruby を搭載したマイコンボードがある(らしい) GR-CITRUS: enzi - mrubyラピッドプロトタイピングプラットフォーム: IoTアプリ開発フレームワークもある Plato

    - mruby IoT Framework: この辺り詳しい方がいたらお話うかがいたいです :pray: http://gadget.renesas.com/ja/product/citrus.html http://enzi.cc/ http://plato.click/
  7. H2O/mruby H2O/mruby # h2o.conf(H2Oの設定ファイル) hosts: default: listen: port: 8080 paths:

    /hello: mruby.handler-file: /etc/h2o/scripts/hello.rb /: file.dir: /var/www/html file.dirlisting: ON access-log: /dev/stdout error-log: /dev/stderr # /etc/h2o/scripts/hello.rb Proc.new do |env| # HTTP status, Headers, Body [200, {'content-type' => 'text/plain'}, ["Hello world\n"]] end
  8. H2O/mruby H2O/mruby コードはこちら $ docker run -p "8080:8080" -ti yyagi/h2o-mruby-server

    $ curl http://localhost:8080/hello https://github.com/y-yagi/h2o-mruby-server
  9. H2O/mruby H2O/mruby 特定のIPからのアクセスのみ許容する、というハンドラを書く場合 # /etc/h2o/scripts/access_controler.rb class AccessController def call(env) if

    /\A172\.17\./.match(env["REMOTE_ADDR"]) # 399を返すと次のハンドラに処理をdelegate出来る [399, {}, []] else [403, {'content-type' => 'text/plain'}, ["access forbidden\n"]] end end end AccessController.new
  10. H2O/mruby H2O/mruby # h2o.conf hosts: default: listen: port: 8080 paths:

    /: mruby.handler-file: /etc/h2o/scripts/access_controler.rb /hello: mruby.handler-file: /etc/h2o/scripts/hello.rb /: file.dir: /var/www/html file.dirlisting: ON
  11. H2O/mrubyでテスト H2O/mrubyでテスト require "../scripts/access_controler" class AccessControllerTest < MTest::Unit::TestCase def test_access_allowed

    result = AccessController.new.call({"REMOTE_ADDR" => "172.17.0.1"}) assert_equal [399, {}, []], result end def test_access_forbidden result = AccessController.new.call({"REMOTE_ADDR" => "127.0.0.1"}) assert_equal [403, {'content-type' => 'text/plain'}, ["access forbidden\n"]], result end end
  12. H2O/mrubyでテスト H2O/mrubyでテスト $ /path/to/mruby access_controler_test.rb # Running tests: .. Finished

    tests in 0.000389s, 5141.3882 tests/s, 5141.3882 assertions/s. 2 tests, 2 assertions, 0 failures, 0 errors, 0 skips
  13. H2O/mruby H2O/mruby class Addresser def call(env) query = env["QUERY_STRING"] return

    unprocessable_entity if query.empty? postcode = query.split("=")[1] return unprocessable_entity if postcode.to_i.zero? [200, {"content-type" => "application/json"}, [fetch(postcode)]] end def unprocessable_entity res = "invalid request".to_json [422, {"content-type" => "application/json"}, [res]] end https://github.com/y-yagi/h2o-mruby- server/blob/master/scripts/addresser.rb
  14. H2O/mruby H2O/mruby def fetch(postcode) redis = H2O::Redis.new(host: 'redis', port: 16379)

    redis.connect cached = redis.get(postcode.to_s).join if cached.nil? url = "https://postcode-jp.appspot.com/api/postcode? general=true&office=false&postcode=#{postcode}" status, _, body = http_request(url).join cached = body.join if status == 200 redis.set(postcode, cached.to_s).join end end cached end https://github.com/y-yagi/h2o-mruby- server/blob/master/scripts/addresser.rb
  15. 参考資料 参考資料 未来のサーバ基盤へのHaconiwa/mrubyの関わり / haconiwa-and-future-os H2O x mrubyで人はどれだけ幸せになれるのか How happy

    they became with H2O/mruby and the future of HTTP https://speakerdeck.com/udzura/haconiwa-and-future-os https://www.slideshare.net/ichitonagata/h2o-x-mruby- 72949986 https://www.slideshare.net/ichitonagata/how- happy-they-became-with-h2omruby-and-the-future-of- http