3
1

フレームワーク「Spin」を簡単に触ってみた

Posted at

はじめに

以前、こちらの記事でruby.wasmを少し触っていたこともあり、wasm関係で「Spin」というフレームワークが存在することを教えていただいたので、どんなものなのか少し調べてみました!

Spinって何?

こちらの記事から抜粋させていただきます。
SpinとはWebAssemblyを使用するイベント駆動型フレームワーク。
SpinはWebアプリ用のフレームワークで、
HTTPリクエストに対してレスポンスを返すWASMモジュールを
作成するためのインターフェイスを提供する。
とのことです。

Spinを触ってみる

Spinのサポート言語の中にRubyがあったので、Spinを使用してRubyファイルの動かしてみようと思います。

Spinのインストール

こちらの記事を参考に進めていきます。
まずは、Ubuntu新しいディレクトリを作り移動する。

mkdir spin_project
cd spin_project

下記を実行!

curl -fsSL https://developer.fermyon.com/downloads/install.sh | bash

image.png

そして、パスの通っている /usr/local/bin/ ディレクトリに移動させる。

sudo mv spin /usr/local/bin/

下記コマンドが通ることを確認し、インストール完了!

spin --help
spin 1.5.1 (8d4334e 2023-09-26)
The Spin CLI

USAGE:
    spin <SUBCOMMAND>

OPTIONS:
    -h, --help       Print help information
    -V, --version    Print version information

SUBCOMMANDS:
    add          Scaffold a new component into an existing application
    build        Build the Spin application
    cloud*       Commands for publishing applications to the Fermyon Cloud.
    deploy       Package and upload an application to the Fermyon Cloud.
    doctor       Detect and fix problems with Spin applications
    help         Print this message or the help of the given subcommand(s)
    js2wasm*     A plugin to convert js files to Spin compatible modules
    login        Log into the Fermyon Cloud.
    new          Scaffold a new application based on a template
    plugins      Install/uninstall Spin plugins
    py2wasm*     A plugin to convert Python applications to Spin compatible modules
    registry     Commands for working with OCI registries to distribute applications
    templates    Commands for working with WebAssembly component templates
    up           Start the Spin application
    watch        Build and run the Spin application, rebuilding and restarting it when files
                     change

* implemented via plugin

SpinでRubyの実行を試みる【実行失敗】

こちらの記事を参考に進めていきます。
spin_projectディレクトリ配下に新しいディレクトリを作る。

mkdir hello-ruby
cd hello-ruby

こちらの2023-10-11-aのAssetsのruby-head-wasm32-unknown-wasi-full.tar.gzをダウンロードし、エクスプローラからvscode上のhello-rubyディレクトリにドラッグアンドドロップする。

ファイルを展開する

gzip -d ruby-head-wasm32-unknown-wasi-full.tar.gz
tar -xvf ruby-head-wasm32-unknown-wasi-full.tar

hello-rubyディレクトリ配下にlibと.gemのディレクトリを作成する。

mkdir lib
mkdir .gem

libディスプレイ配下に下記内容のhello.rbを作成する。

puts "content-type: text/plain"
puts ""
puts "Hello, World"

hello-rubyディレクトリ配下に下記内容のspin.tomlを作成する。

hello-rubyディレクトリで下記を実行し、サーバを起動する。

spin up
Logging component stdio to ".spin/logs/"

Serving http://127.0.0.1:3000
Available Routes:
  ruby: http://127.0.0.1:3000

http://127.0.0.1:3000 にアクセスしてみると、hello.rbが権限で弾かれている模様...

/: Operation not permitted -- /lib/hello.rb (LoadError)
2023-10-12T14:38:59.133785Z ERROR spin_trigger_http: Error processing request: invoking _start for component ruby

Caused by:
    0: error while executing at wasm backtrace:
           0: 0xa68d0c - <unknown>!<wasm function 12154>
           1: 0xa84fef - <unknown>!<wasm function 12312>
           2: 0xa9ab73 - <unknown>!<wasm function 12415>
       note: using the `WASMTIME_BACKTRACE_DETAILS=1` environment variable may show more debugging information
    1: Exited with i32 exit status 1    

libディレクトリに移動して、下記実行。

sudo chmod 777 hello.rb

再度、サーバ起動するも同様に権限で弾かれた...
llでhello.rbの権限を確認すると、-rwxrwxrwxで、オーナーはroom以外になっていたので、問題ないはずだが、うーーーん...
hello.rbの中身が実行されたものが表示された画面を確認できると想定していたので、悲しいです。(申し訳ございません...)知見ある方いらっしゃいましたら、ご教示いただけますと幸いです...!

おわりに

今回Spinというフレームワークを調査し、触ってみたもののまだ自分の中に落とし込めていない状態なので、今後時間をみつけて深ぼっていければと思いました!

参考記事

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1