Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions contrib/etc/default/tiny-web-example-webapp
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions contrib/etc/init/tiny-web-example-webapp.conf
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
1 change: 1 addition & 0 deletions contrib/etc/tiny-web-example/webapp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
database_uri: 'mysql2://localhost/tiny_web_example?user=root'
3 changes: 3 additions & 0 deletions frontend/config-webapp.ru
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down
1 change: 1 addition & 0 deletions frontend/config/webapp.yml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
database_uri: 'mysql2://localhost/tiny_web_example?user=root'
2 changes: 1 addition & 1 deletion frontend/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions frontend/views/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
</div>
</div>
<div class="panel-body">
<div class="col-sm-offset-2">
<h4>
<p class="text-danger"><%= @errors %></p>
</h4>
</div>
<form action="/comment" method="post" class="form-horizontal">
<div class="form-group">
<label for="exampleInputName" class="col-sm-2 control-label">Name</label>
Expand Down Expand Up @@ -36,14 +41,14 @@
<div class="panel-heading">
<div class="panel-title">
<h4>
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) %>
</h4>
</div>
</div>
<div class="panel-body">
<%= comment.comment %>
<%= h(comment.comment).gsub(/\r\n|\r|\n/, "<br />") %>
</div>
</div>
<% end %>
Expand Down
17 changes: 17 additions & 0 deletions frontend/webapp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
81 changes: 81 additions & 0 deletions rpmbuild/SPECS/tiny-web-example.spec
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions rpmbuild/rules
Original file line number Diff line number Diff line change
@@ -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

1 change: 1 addition & 0 deletions webapi/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions webapi/config/webapi.conf.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Database connection string
database_uri 'mysql2://localhost/tiny_web_example?user=root'
1 change: 1 addition & 0 deletions webapi/lib/webapi/configurations/webapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]

Expand Down