火曜日, 6月 17, 2025
- Advertisment -
ホームニューステックニュースWasm を使えば Go サーバーが無料でデプロイできるんです!

Wasm を使えば Go サーバーが無料でデプロイできるんです!


過去に Vercel に Go サーバーを無料でデプロイできるという記事を書きました。

https://zenn.dev/otakakot/articles/9e9269a87aafeb

待望(?) の新シリーズです。
ついに新たなサービスを見つけました。
WebAssembly(Wasm) コンポーネントを使用した Spin というフレームワークがデプロイできる Fermyon Cloud です!

https://www.fermyon.com/

今回は Spin というフレームワークを使って Fermyon Cloud に Go サーバーをデプロイするまでの流れを簡単にご紹介します。
ちなみに、「Wasm を使えば」と記載していますが Wasm の知識はほぼ不要です。
Go が書ければ問題ないです。

Mozilla にドキュメントがあるので以下をご参考いただければと思います。

https://developer.mozilla.org/ja/docs/WebAssembly

また、最近 syumai さんが Go を WebAssembly に関して解説くださっていたのでこちらをご参考いただくのもよいかと思います。

以前私も Wasm に関する登壇をしたことがあるのでこちらもご参考いただければと思います。

https://spinframework.dev/v3/index

Spin is an open source framework for building and running fast, secure, and composable cloud microservices with WebAssembly.
Spinは、WebAssemblyを使用して高速で安全かつ組み合わせて利用できるクラウドマイクロサービスを作成し実行するためのオープンソースフレームワークです。

公式ドキュメントに Spin を使って作成されたアプリケーションも紹介されていました。

https://spinframework.dev/v3/see-what-people-have-built-with-spin

さまざまなアプリケーションが構築できそうだと感じます。

GitHub のリポジトリはこちらです。

https://github.com/spinframework/spin

https://www.fermyon.com/cloud

The Fermyon Cloud is a cloud application platform for WebAssembly-based serverless functions and microservices.
Fermyon Cloudは、WebAssemblyベースのサーバーレス関数とマイクロサービス向けのクラウドアプリケーションプラットフォームです。

さまざまなプランが用意されていますが、Starterプランは無料で利用することができます!

https://www.fermyon.com/pricing

Fermyon のアカウントを用意します。
以下の公式サイトにアクセスし右上の Spin Quickstart から始めることができます。

https://developer.fermyon.com/

GitHub 連携にてログインすることが可能です。
また、クレジットカードの登録不要で開始することが可能です。

各種操作を行うために Spin CLI をインストールします。

https://spinframework.dev/v3/install

いくつかインストール方法はありますが、今回は brew にてインストールします。

brew tap spinframework/tap
brew install spinframework/tap/spin
spin –version
spin 3.2.0 (c9be6d8 2025-03-24)

Fermyon Cloud にデプロイするためにログインをします。

https://spinframework.dev/v3/quickstart#deploy-your-application-to-fermyon-cloud

spin login
spin login
The `cloud` plugin is required. Installing now.
Plugin 'cloud' was installed successfully!

Description:
	Commands for publishing applications to the Fermyon Cloud.

Homepage:
	https://github.com/fermyon/cloud-plugin

Copy your one-time code:

XXXXXXXX

...and open the authorization page in your browser:

https://cloud.fermyon.com/device-authorization

Waiting for device authorization...

コマンドを実行するとワンタイムコードを取得できるのでブラウザにて入力します。

また、TinyGo も必要となるためインストールします。

https://tinygo.org/getting-started/install/

brew tap tinygo-org/tools
brew install tinygo
tinygo version
tinygo version 0.37.0 darwin/arm64 (using go version go1.24.4 and LLVM version 19.1.2)

当たり前ですが Go もインストールしておきます。

https://go.dev/doc/install

go version
go version go1.24.4 darwin/arm64

以下のコマンドで Spin アプリケーションを初期化します。

spin new
Pick a template to start your application with:
  http-c (HTTP request handler using C and the Zig toolchain)
  http-empty (HTTP application with no components)
> http-go (HTTP request handler using (Tiny)Go)
  http-grain (HTTP request handler using Grain)
  http-js (HTTP request handler using JavaScript)
  http-php (HTTP request handler using PHP)
  http-py (HTTP request handler using Python)
  http-rust (HTTP request handler using Rust)
  http-ts (HTTP request handler using TypeScript)
  http-zig (HTTP request handler using Zig)
  redirect (Redirects a HTTP route)
  redis-go (Redis message handler using (Tiny)Go)
  redis-js (Redis message handler using JavaScript)
  redis-rust (Redis message handler using Rust)
  redis-ts (Redis message handler using TypeScript)
  static-fileserver (Serves static files from an asset directory)
Pick a template to start your application with: http-go (HTTP request handler using (Tiny)Go)
Enter a name for your new application: app-name>
Description: 
HTTP path: /...

いくつかの言語を選択できますが、今回は Go での開発がしたいので http-go (HTTP request handler using (Tiny)Go) を選択します。
実行が完了すると以下のコード群でディレクトリが作成されます。

.
├── go.mod
├── go.sum
├── main.go
└── spin.toml

詳しいコードについては以下のリポジトリにて確認できます。

https://github.com/otakakot/sample-go-spin-fermyon/tree/298e3fcea41147a7fb6dfa13d01c9f444ae1779e

以下のコマンドで Spin アプリケーションをビルドします。

そして以下のコマンドで実際にアプリケーションを起動します。

アプリケーションを起動すると以下のようなログが出力されサーバーにアクセスが可能となります。

Logging component stdio to ".spin/logs/"

Serving http://127.0.0.1:3000
Available Routes:
  app-name>: http://127.0.0.1:3000 (wildcard)

実際にアクセスすると以下のようにレスポンスを取得できます。

curl -i localhost:3000
HTTP/1.1 200 OK
content-type: text/plain
content-length: 13
date: Sun, 15 Jun 2025 07:06:29 GMT

Hello World!

また、Spin は watch コマンドも用意されておりホットリロードにて開発も可能です。(便利!!!)

以下のコマンドにてアプリケーションをデプロイできます。

コマンドを実行すると以下のようなログが出力されクラウド環境へとデプロイされます。

Uploading app-name> version 0.1.0 to Fermyon Cloud...
Deploying...
Waiting for application to become ready...... ready

View application:   https://app-name>.fermyon.app/
Manage application: https://cloud.fermyon.com/app/app-name>

実際にアクセスすると以下のようにレスポンスを取得できます。

curl -i https://app-name>.fermyon.app/
HTTP/2 200 
content-type: text/plain
date: Sun, 15 Jun 2025 07:14:30 GMT
content-length: 13

Hello World!

めちゃくちゃ簡単に Go サーバーをクラウド環境へとデプロイできました!!!

デプロイ時のログにて出力される Manage application: https://cloud.fermyon.com/app/ にてアクセスしウェブ画面 Dashboard > > App Settings にて Delete app を実行します。

CLI の Reference を確認しましたが、 CLI からの削除はできなそうでした。

https://spinframework.dev/v3/cli-reference

Wasm を(裏で)利用している Go サーバーを無料でデプロイできるサービスを紹介しました。
しかも実装(ルーティングまわり)が net/http を使ったときの実装とほぼ変わらずなところも推せます。( Vercel はちょっと癖があるのでそこが微妙と感じています… )

https://github.com/otakakot/sample-go-spin-fermyon/blob/main/main.go

また、Spin の機能一覧を見ているといろいろできそうなので非常に可能性も感じています。

https://github.com/spinframework/spin?tab=readme-ov-file#language-support-for-spin-features

ほかの機能を使った実装もそのうちご紹介できればと思います。

今回実装したコードは以下に置いておきます。

https://github.com/otakakot/sample-go-spin-fermyon



Source link

Views: 0

RELATED ARTICLES

返事を書く

あなたのコメントを入力してください。
ここにあなたの名前を入力してください

- Advertisment -