5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Amethyst で HelloWorld

Posted at

Amethyst には Rails での rails コマンドのようなジェネレータはまだないっぽいけど、プロジェクトのスケルトンを作ってくれる amethyst-bin というものがあるようなのでそれを使って HelloWorld してみた。

Sdogruyol/amethyst-bin
https://github.com/Sdogruyol/amethyst-bin

試した時のバージョンは Crystal 0.7.4, Amethyst 0.1.7。

まずは amethyst-bin のインストール。
ここでは PATH の通っている ~/bin 以下に入れた。

% curl https://raw.githubusercontent.com/Sdogruyol/amethyst-bin/master/amethyst > ~/bin/amethyst
% chmod +x ~/bin/amethyst

amethyst のプロジェクト作成。
crystal deps 呼んで依存ライブラリのインストールするところも自動でやってくれる。

% amethyst helloworld 
      create  helloworld/.gitignore
      create  helloworld/LICENSE
      create  helloworld/README.md
      create  helloworld/.travis.yml
      create  helloworld/Projectfile
      create  helloworld/src/helloworld.cr
      create  helloworld/src/helloworld/version.cr
      create  helloworld/spec/spec_helper.cr
      create  helloworld/spec/helloworld_spec.cr
Initialized empty Git repository in /home/akishin/src/crystal/helloworld/.git/
Cloning into '.deps/Codcore-amethyst'...
remote: Counting objects: 2576, done.
remote: Total 2576 (delta 0), reused 0 (delta 0), pack-reused 2576
Receiving objects: 100% (2576/2576), 800.14 KiB | 286.00 KiB/s, done.
Resolving deltas: 100% (1603/1603), done.
Checking connectivity... done.
Cloning into '.deps/spalger-crystal-mime'...
remote: Counting objects: 40, done.
remote: Total 40 (delta 0), reused 0 (delta 0), pack-reused 40
Receiving objects: 100% (40/40), 13.65 KiB | 0 bytes/s, done.
Resolving deltas: 100% (13/13), done.
Checking connectivity... done.

プロジェクトのディレクトリに移動。

% cd helloworld

以下のような構造が生成されている。

% tree
.
├── LICENSE
├── Projectfile
├── README.md
├── libs
│   ├── amethyst -> ../.deps/Codcore-amethyst/src
│   └── mime -> ../.deps/spalger-crystal-mime/src
├── spec
│   ├── helloworld_spec.cr
│   └── spec_helper.cr
└── src
    ├── helloworld
    │   ├── controllers
    │   ├── version.cr
    │   └── views
    └── helloworld.cr

8 directories, 7 files

src/helloworld/controllers の下に world_controller.cr を以下の内容で作成。

require "amethyst"
class WorldController < Base::Controller
  actions :hello

  view "hello", "#{__DIR__}/../views"
  def hello
    @name = "World"
    respond_to do |format|
      format.html { render "hello" }
    end
  end
end

src/helloworld/views/hello.ecr を以下の内容で作成。

Hello, <%= name %>

src/helloworld.cr を以下のように編集。

require "amethyst"
require "./helloworld/controllers/world_controller"
class HelloworldApp < Base::App
  routes.draw do
    # Here you define your routes e.g
    get "/hello", "world#hello"
    register WorldController
  end
end

app = HelloworldApp.new
app.serve

サーバ起動。

% crystal run src/helloworld.cr                                                                                                       (git)-[master] 
[Amethyst 0.1.7] serving application "helloworld" at http://127.0.0.1:8080

ブラウザから http://127.0.0.1:8080/hello にアクセスして「Hello, World」と表示されれば OK。

5
5
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
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?