とーますメモ

Ruby on Rails / Goなどの学習メモ

【Rails5】バリデーションエラーでtranslation missingが出る。

自分の場合、英語で表示したかったの
以下を設定したが何も変わらず。

失敗した例:
config/application.rb

config.i18n.default_locale = :en

以下を設定したら上手く表示された。

成功した例:
config/application.rb

config.i18n.fallbacks = [:en]


[参考]
how to use rails i18n fallback features - Stack Overflow

【Atom】拡張子を追加し、ハイライトカラーを割り当てる方法

file-typesを使用する。

atom.io

インストール後、Atom -> Config...を選び、config.csonを開く。

その後、以下のように設定

例)liquid拡張子のファイルをRailsのERBとして割り当てる。

"*":
  "file-types":
    liquid: "text.html.erb"

設定できる設定一覧は以下。

  • source.c
  • source.cake
  • source.clojure
  • source.coffee
  • source.coffee.jsx
  • source.cpp
  • source.cs
  • source.css
  • source.css.less
  • source.css.scss
  • source.csx
  • source.gfm
  • source.git-config
  • source.go
  • source.gotemplate
  • source.java
  • source.java-properties
  • source.js
  • source.js.jsx
  • source.js.rails source.js.jquery
  • source.js.regexp
  • source.js.regexp.replacement
  • source.json
  • source.litcoffee
  • source.makefile
  • source.nant-build
  • source.objc
  • source.objcpp
  • source.perl
  • source.perl6
  • source.plist
  • source.python
  • source.regexp.python
  • source.ruby
  • source.ruby.rails
  • source.ruby.rails.rjs
  • source.sass
  • source.shell
  • source.sql
  • source.sql.mustache
  • source.sql.ruby
  • source.strings
  • source.toml
  • source.yaml
  • text.git-commit
  • text.git-rebase
  • text.html.basic
  • text.html.erb
  • text.html.gohtml
  • text.html.jsp
  • text.html.mustache
  • text.html.php
  • text.html.ruby
  • text.hyperlink
  • text.junit-test-report
  • text.plain
  • text.plain.null-grammar
  • text.python.console
  • text.python.traceback
  • text.shell-session
  • text.todo
  • text.xml
  • text.xml.plist
  • text.xml.xsl

atom.io

【さくらVPS】Nginx + Goのインストール・設定

前回、NginxとGoをそれぞれインストールした。

thoames.hatenadiary.jp
thoames.hatenadiary.jp

ここでは以下の前提で設定する。

Goは「127.0.0.1:5050」で既に動作しているという前提で
話をすすめる。

$ ./app &


nginxの設定を以下のようにすれば動作する。

設定

default.conf

server {
    listen              80;
    server_name  localhost;

    location / {
        proxy_pass  http://127.0.0.1:5050;
    }
}

GoでFastCGIを使用するため
"net/http/fcgi"パッケージを使用する場合は、
以下のように設定できるっぽい。

server {
    listen              80;
    server_name  localhost;

    location / {
        fastcgi_pass  127.0.0.1:5050;
        include       fastcgi_params;
    }
}

再起動

$ sudo service nginx restart

【Go】Goアプリをデーモン化(常駐化)させてみた。

Supervisorという「プロセス管理/デーモン化」ツールが便利そう。
色んなサイトで紹介されていたので、使用してみる。

インストール

$ sudo yum install supervisor

設定ファイル編集

$vi /etc/supervisord.conf

一番下にGoアプリの設定を以下のように追記。
アプリ名:sample
アプリのディレクトリ:/home/xxxxx/go_deploy/
アプリ実行ファイル名:app
ユーザ名:YYYYY

[program:sample]
command=/home/xxxxx/go_deploy/app
autostart=true
autorestart=true
stopsignal=TERM
stdout_logfile=/var/log/supervisor/%(program_name).stdout.log
stderr_logfile=/var/log/supervisor/%(program_name).stderr.log
user=YYYYY

起動

$ sudo service supervisord start

autostartがtrueになっているので、自動でスタート。
細かいプロセス管理は以下の「supervisorctl」コマンドを使用する。
www.task-notes.com

動作中の全プロセス確認

$ sudo supervisorctl status

指定したアプリのみ起動

$ sudo supervisorctl start sample

指定したアプリのステータス確認

$ sudo supervisorctl status sample

指定したアプリのみ停止

$ sudo supervisorctl stop sample

ログファイルの場所

/var/log/supervisor/supervisord.log

注意:テンプレートなどをGo内で使用している場合は(つまり一つのバイナリファイル内にすべてのデータが無い場合)
go-bindataなどを使用して、テンプレートやアセット系のファイルを一つのバイナリにしてあげないとsupervisorでは動作しないっぽい。。
(自分はできなかったが、他に方法をご存じの方がいれば、ご指摘いただけるとありがたいです)

[参考]
SupervisorでGolangで書かれたアプリケーションのデーモン化をした話 - こんちゃんブログ
Supervisorで簡単にデーモン化 - Qiita
適当なスクリプトをデーモン化するのにSupervisorが便利 - id:anatooのブログ

【さくらVPS】Nginxインストール・設定

yumに入っているものは古いため、
リポジトリを登録してインストール

Nginx用のリポジトリを作成

$ vim /etc/yum.repo.d/nginx.repo

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/rhel/$releasever/$basearch/
gpgcheck=0
enabled=1

インストール

$ sudo yum install -y nginx

起動設定

起動しているhttpdをまずは止めてから設定

$ chkconfig httpd off
$ service httpd stop
$ chkconfig nginx on
$ service nginx start
$ systemctl status nginx

サーバのIPアドレスにアクセスすると
以下の画面が表示されます。

f:id:Thoames1212:20180112103938p:plain


[参考]
umegusa.hatenablog.jp