diff --git a/contrib/etc/default/tiny-web-example-webapp b/contrib/etc/default/tiny-web-example-webapp new file mode 100644 index 0000000..9b8d90e --- /dev/null +++ b/contrib/etc/default/tiny-web-example-webapp @@ -0,0 +1,11 @@ +# tiny-web-example +EXAMPLE_ROOT=/opt/axsh/tiny-web-example + +# Commnet out to run the vdc init script. +#RUN=yes + +## rack params +RACK_ENV=development +#BIND_ADDR=0.0.0.0 +#PORT=80 +#UNICORN_CONF=/etc/tiny-web-example/unicorn-common.conf \ No newline at end of file diff --git a/contrib/etc/init/tiny-web-example-webapp.conf b/contrib/etc/init/tiny-web-example-webapp.conf new file mode 100644 index 0000000..074ced7 --- /dev/null +++ b/contrib/etc/init/tiny-web-example-webapp.conf @@ -0,0 +1,33 @@ +description "tiny web example: web application" +author "axsh Co." + +start on started tiny-web-example-webapp +stop on stopped tiny-web-example-webapp + +respawn +respawn limit 5 60 + +env NAME=webapp + +script + [ -f /etc/default/tiny-web-example-${NAME} ] && . /etc/default/tiny-web-example-${NAME} + # Make RUN=yes effective only at auto start. + [ -n "$UPSTART_EVENTS" -a "x${RUN}" != "xyes" ] && { + logger "[${NAME}] Skip auto start for ${NAME}. Edit /etc/default/tiny-web-example-${NAME} to set RUN=yes." + exit 0 + } + + ulimit -c ${DAEMON_COREFILE_LIMIT:-0} + + [ -d "${EXAMPLE_ROOT}" ] || { + logger "no such directory: ${EXAMPLE_ROOT}" + exit 1 + } + cd ${EXAMPLE_ROOT}/frontend/ + + exec bundle exec unicorn \ + -o ${BIND_ADDR:-0.0.0.0} \ + -p ${PORT:-80} \ + -c ${UNICORN_CONF:-/etc/tiny-web-example/unicorn-common.conf} ./config-${NAME}.ru \ + >> /var/log/tiny-web-example/${NAME}.log 2>&1 +end script \ No newline at end of file diff --git a/webapi/config/webapi.conf b/contrib/etc/tiny-web-example/webapi.conf similarity index 100% rename from webapi/config/webapi.conf rename to contrib/etc/tiny-web-example/webapi.conf diff --git a/contrib/etc/tiny-web-example/webapp.yml b/contrib/etc/tiny-web-example/webapp.yml new file mode 100644 index 0000000..73f783f --- /dev/null +++ b/contrib/etc/tiny-web-example/webapp.yml @@ -0,0 +1 @@ +database_uri: 'mysql2://localhost/tiny_web_example?user=root' \ No newline at end of file diff --git a/frontend/config-webapp.ru b/frontend/config-webapp.ru index 2869e22..74d39c1 100644 --- a/frontend/config-webapp.ru +++ b/frontend/config-webapp.ru @@ -2,6 +2,9 @@ require 'rubygems' require 'sinatra' +require 'sinatra/config_file' + +config_file 'config/webapp.yml', '/etc/tiny-web-example/webapp.yml' $LOAD_PATH.unshift File.expand_path('..', __FILE__) diff --git a/frontend/config/webapp.yml.example b/frontend/config/webapp.yml.example new file mode 100644 index 0000000..73f783f --- /dev/null +++ b/frontend/config/webapp.yml.example @@ -0,0 +1 @@ +database_uri: 'mysql2://localhost/tiny_web_example?user=root' \ No newline at end of file diff --git a/frontend/models/comment.rb b/frontend/models/comment.rb index b6b5fb3..ff73c21 100644 --- a/frontend/models/comment.rb +++ b/frontend/models/comment.rb @@ -2,7 +2,7 @@ require 'sequel' -Sequel.connect("mysql2://localhost/tiny_web_example?user=root") +Sequel.connect(settings.database_uri) class Comment < Sequel::Model plugin :validation_helpers diff --git a/frontend/views/index.erb b/frontend/views/index.erb index 085e107..02565c8 100644 --- a/frontend/views/index.erb +++ b/frontend/views/index.erb @@ -7,6 +7,11 @@
+
+

+

<%= @errors %>

+

+
@@ -36,14 +41,14 @@

- No. <%= comment.id %> - Name <%= comment.display_name %> - Created at <%= comment.created_at %> + No. <%= h(comment.id) %> + Name <%= h(comment.display_name) %> + Created at <%= h(comment.created_at) %>

- <%= comment.comment %> + <%= h(comment.comment).gsub(/\r\n|\r|\n/, "
") %>
<% end %> diff --git a/frontend/webapp.rb b/frontend/webapp.rb index f75409b..8f6300a 100644 --- a/frontend/webapp.rb +++ b/frontend/webapp.rb @@ -3,14 +3,31 @@ require 'sinatra/base' class WebApp < Sinatra::Base + + configure do + use Rack::Session::Pool + end + + helpers do + include Rack::Utils + alias_method :h, :escape_html + end + get '/' do + @errors = session[:errors] + session.clear + @comments = Comment.dataset.all erb :index end post '/comment' do + begin Comment.create(:display_name => params["display_name"], :comment => params["comment"]) + rescue Sequel::ValidationFailed => e + session[:errors] = e + end redirect '/' end end diff --git a/rpmbuild/SPECS/tiny-web-example.spec b/rpmbuild/SPECS/tiny-web-example.spec new file mode 100644 index 0000000..2786ae8 --- /dev/null +++ b/rpmbuild/SPECS/tiny-web-example.spec @@ -0,0 +1,81 @@ +%define _prefix_path opt/axsh +%define _tiny_web_example_git_uri git://github.com/axsh/tiny_web_example.git +%define oname tiny-web-example + +%define release_id 1.daily +%{?build_id:%define release_id %{build_id}} +%{?repo_uri:%define _tiny_web_example_git_uri %{repo_uri}} + +Summary: Tiny Web Example. +Name: %{oname} +Version: 0.0.1 +Release: %{release_id}%{?dist} +License: see https://github.com/axsh/tiny_web_example/blob/master/README.md +Group: Development/Languages +URL: http://example.jp/ +Source0: %{_vdc_git_uri} +Prefix: /%{_prefix_path} + +BuildRequires: mysql-devel + +Requires: nginx +Requires: mysql-server + + +%description +Tiny Web Example + +%prep +[ -d %{name}-%{version} ] && rm -rf [ -d %{name}-%{version} ] +git clone %{_tiny_web_example_git_uri} %{name}-%{version} +cd %{name}-%{version} +[ -z "%{build_id}" ] || { + build_id=%{build_id} + git checkout ${build_id##*git} + unset build_id +} && : + +%setup -T -D + +%build +RUBYDIR=/usr/bin/ruby rpmbuild/rules build + +%install +[ -d ${RPM_BUILD_ROOT} ] && rm -rf ${RPM_BUILD_ROOT} +mkdir -p ${RPM_BUILD_ROOT}/%{prefix}/%{oname} + +components=" + frontend + webapi +" +for component in ${components}; do + rsync -aHA --exclude=".git/*" --exclude="*~" --exclude="*/cache/*.gem" --exclude="*/cache/bundler/git/*" `pwd`/${component} ${RPM_BUILD_ROOT}/%{prefix}/%{oname}/ +done +unset components + +[ -d ${RPM_BUILD_ROOT}/etc ] || mkdir -p ${RPM_BUILD_ROOT}/etc +rsync -aHA `pwd`/contrib/etc/default ${RPM_BUILD_ROOT}/etc +rsync -aHA `pwd`/contrib/etc/init ${RPM_BUILD_ROOT}/etc +rsync -aHA `pwd`/contrib/etc/%{oname} ${RPM_BUILD_ROOT}/etc + +mkdir -p ${RPM_BUILD_ROOT}/var/log/%{oname} + +%clean +RUBYDIR=/usr/bin/ruby rpmbuild/rules clean +rm -rf $RPM_BUILD_ROOT + +%post +/sbin/chkconfig mysqld on + +%files +%defattr(-,root,root,-) +%{prefix}/%{oname} +%dir /etc/%{oname} +%dir /var/log/%{oname} +%config(noreplace) /etc/default/tiny-web-example-webapi +%config(noreplace) /etc/default/tiny-web-example-webapp +%config(noreplace) /etc/init/tiny-web-example-webapi.conf +%config(noreplace) /etc/init/tiny-web-example-webapp.conf +%config(noreplace) /etc/tiny-web-example/unicorn-common.conf +%config(noreplace) /etc/tiny-web-example/webapi.conf +%config(noreplace) /etc/tiny-web-example/webapp.yml diff --git a/rpmbuild/rules b/rpmbuild/rules new file mode 100755 index 0000000..13fada8 --- /dev/null +++ b/rpmbuild/rules @@ -0,0 +1,31 @@ +#!/usr/bin/make -f + +CURDIR ?= $(PWD) + +build: build-stamp +build-stamp: bundle-install + touch $@ +bundle-install: bundle-install-stamp +bundle-install-stamp: + (cd $(CURDIR)/webapi && rm -rf vendor/bundle && mkdir -p vendor/bundle && bundle install --standalone --path vendor/bundle) + (cd $(CURDIR)/frontend && rm -rf vendor/bundle && mkdir -p vendor/bundle && bundle install --standalone --path vendor/bundle) + + # replace /usr/bin/ruby shebangs. + # unicorn shebang: /this/will/be/overwritten/or/wrapped/anyways/do/not/worry/ruby + egrep -m 1 -r '^#!.*/ruby' $(CURDIR) | \ + grep -v '^Binary file' | \ + awk -F: '{print $$1}' | \ + while read -r i; do \ + echo $$i; \ + cp -p $$i $$i.sed ;\ + sed -e 's|^#!.*/ruby|#!/usr/bin/ruby|g' < $$i > $$i.sed ;\ + mv $$i.sed $$i ;\ + done + + touch $@ + +clean: + rm -rf $(CURDIR)/vendor/bundle + rm -f $(CURDIR)/bundle-install-stamp + rm -f build-stamp + diff --git a/webapi/Rakefile b/webapi/Rakefile index eab97d9..402925f 100644 --- a/webapi/Rakefile +++ b/webapi/Rakefile @@ -10,6 +10,7 @@ task :environment do require 'webapi' Webapi::Configurations.load Webapi::Configurations::Webapi, [ ENV['CONF_PATH'].to_s, + '/etc/tiny-web-example/webapi.conf', File.expand_path('config/webapi.conf', ::WEBAPI_ROOT) ] end diff --git a/webapi/config/webapi.conf.example b/webapi/config/webapi.conf.example new file mode 100644 index 0000000..fac9fc7 --- /dev/null +++ b/webapi/config/webapi.conf.example @@ -0,0 +1,2 @@ +# Database connection string +database_uri 'mysql2://localhost/tiny_web_example?user=root' diff --git a/webapi/lib/webapi/configurations/webapi.rb b/webapi/lib/webapi/configurations/webapi.rb index f563790..19c2b4d 100644 --- a/webapi/lib/webapi/configurations/webapi.rb +++ b/webapi/lib/webapi/configurations/webapi.rb @@ -7,6 +7,7 @@ module Configurations class Webapi < Fuguta::Configuration usual_paths [ ENV['CONF_PATH'].to_s, + '/etc/tiny-web-example/webapi.conf', File.expand_path('config/webapi.conf', ::WEBAPI_ROOT) ]