Skip to content

Commit 1c1baef

Browse files
committed
Merge pull request #1 from axsh/master
apply origin head
2 parents 932c021 + 76bd591 commit 1c1baef

14 files changed

Lines changed: 192 additions & 5 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# tiny-web-example
2+
EXAMPLE_ROOT=/opt/axsh/tiny-web-example
3+
4+
# Commnet out to run the vdc init script.
5+
#RUN=yes
6+
7+
## rack params
8+
RACK_ENV=development
9+
#BIND_ADDR=0.0.0.0
10+
#PORT=80
11+
#UNICORN_CONF=/etc/tiny-web-example/unicorn-common.conf
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
description "tiny web example: web application"
2+
author "axsh Co."
3+
4+
start on started tiny-web-example-webapp
5+
stop on stopped tiny-web-example-webapp
6+
7+
respawn
8+
respawn limit 5 60
9+
10+
env NAME=webapp
11+
12+
script
13+
[ -f /etc/default/tiny-web-example-${NAME} ] && . /etc/default/tiny-web-example-${NAME}
14+
# Make RUN=yes effective only at auto start.
15+
[ -n "$UPSTART_EVENTS" -a "x${RUN}" != "xyes" ] && {
16+
logger "[${NAME}] Skip auto start for ${NAME}. Edit /etc/default/tiny-web-example-${NAME} to set RUN=yes."
17+
exit 0
18+
}
19+
20+
ulimit -c ${DAEMON_COREFILE_LIMIT:-0}
21+
22+
[ -d "${EXAMPLE_ROOT}" ] || {
23+
logger "no such directory: ${EXAMPLE_ROOT}"
24+
exit 1
25+
}
26+
cd ${EXAMPLE_ROOT}/frontend/
27+
28+
exec bundle exec unicorn \
29+
-o ${BIND_ADDR:-0.0.0.0} \
30+
-p ${PORT:-80} \
31+
-c ${UNICORN_CONF:-/etc/tiny-web-example/unicorn-common.conf} ./config-${NAME}.ru \
32+
>> /var/log/tiny-web-example/${NAME}.log 2>&1
33+
end script
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
database_uri: 'mysql2://localhost/tiny_web_example?user=root'

‎frontend/config-webapp.ru‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
require 'rubygems'
44
require 'sinatra'
5+
require 'sinatra/config_file'
6+
7+
config_file 'config/webapp.yml', '/etc/tiny-web-example/webapp.yml'
58

69
$LOAD_PATH.unshift File.expand_path('..', __FILE__)
710

‎frontend/config/webapp.yml.example‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
database_uri: 'mysql2://localhost/tiny_web_example?user=root'

‎frontend/models/comment.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require 'sequel'
44

5-
Sequel.connect("mysql2://localhost/tiny_web_example?user=root")
5+
Sequel.connect(settings.database_uri)
66

77
class Comment < Sequel::Model
88
plugin :validation_helpers

‎frontend/views/index.erb‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
</div>
88
</div>
99
<div class="panel-body">
10+
<div class="col-sm-offset-2">
11+
<h4>
12+
<p class="text-danger"><%= @errors %></p>
13+
</h4>
14+
</div>
1015
<form action="/comment" method="post" class="form-horizontal">
1116
<div class="form-group">
1217
<label for="exampleInputName" class="col-sm-2 control-label">Name</label>
@@ -36,14 +41,14 @@
3641
<div class="panel-heading">
3742
<div class="panel-title">
3843
<h4>
39-
No. <%= comment.id %>
40-
Name <%= comment.display_name %>
41-
Created at <%= comment.created_at %>
44+
No. <%= h(comment.id) %>
45+
Name <%= h(comment.display_name) %>
46+
Created at <%= h(comment.created_at) %>
4247
</h4>
4348
</div>
4449
</div>
4550
<div class="panel-body">
46-
<%= comment.comment %>
51+
<%= h(comment.comment).gsub(/\r\n|\r|\n/, "<br />") %>
4752
</div>
4853
</div>
4954
<% end %>

‎frontend/webapp.rb‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,31 @@
33
require 'sinatra/base'
44

55
class WebApp < Sinatra::Base
6+
7+
configure do
8+
use Rack::Session::Pool
9+
end
10+
11+
helpers do
12+
include Rack::Utils
13+
alias_method :h, :escape_html
14+
end
15+
616
get '/' do
17+
@errors = session[:errors]
18+
session.clear
19+
720
@comments = Comment.dataset.all
821
erb :index
922
end
1023

1124
post '/comment' do
25+
begin
1226
Comment.create(:display_name => params["display_name"],
1327
:comment => params["comment"])
28+
rescue Sequel::ValidationFailed => e
29+
session[:errors] = e
30+
end
1431
redirect '/'
1532
end
1633
end
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
%define _prefix_path opt/axsh
2+
%define _tiny_web_example_git_uri git://github.com/axsh/tiny_web_example.git
3+
%define oname tiny-web-example
4+
5+
%define release_id 1.daily
6+
%{?build_id:%define release_id %{build_id}}
7+
%{?repo_uri:%define _tiny_web_example_git_uri %{repo_uri}}
8+
9+
Summary: Tiny Web Example.
10+
Name: %{oname}
11+
Version: 0.0.1
12+
Release: %{release_id}%{?dist}
13+
License: see https://github.com/axsh/tiny_web_example/blob/master/README.md
14+
Group: Development/Languages
15+
URL: http://example.jp/
16+
Source0: %{_vdc_git_uri}
17+
Prefix: /%{_prefix_path}
18+
19+
BuildRequires: mysql-devel
20+
21+
Requires: nginx
22+
Requires: mysql-server
23+
24+
25+
%description
26+
Tiny Web Example
27+
28+
%prep
29+
[ -d %{name}-%{version} ] && rm -rf [ -d %{name}-%{version} ]
30+
git clone %{_tiny_web_example_git_uri} %{name}-%{version}
31+
cd %{name}-%{version}
32+
[ -z "%{build_id}" ] || {
33+
build_id=%{build_id}
34+
git checkout ${build_id##*git}
35+
unset build_id
36+
} && :
37+
38+
%setup -T -D
39+
40+
%build
41+
RUBYDIR=/usr/bin/ruby rpmbuild/rules build
42+
43+
%install
44+
[ -d ${RPM_BUILD_ROOT} ] && rm -rf ${RPM_BUILD_ROOT}
45+
mkdir -p ${RPM_BUILD_ROOT}/%{prefix}/%{oname}
46+
47+
components="
48+
frontend
49+
webapi
50+
"
51+
for component in ${components}; do
52+
rsync -aHA --exclude=".git/*" --exclude="*~" --exclude="*/cache/*.gem" --exclude="*/cache/bundler/git/*" `pwd`/${component} ${RPM_BUILD_ROOT}/%{prefix}/%{oname}/
53+
done
54+
unset components
55+
56+
[ -d ${RPM_BUILD_ROOT}/etc ] || mkdir -p ${RPM_BUILD_ROOT}/etc
57+
rsync -aHA `pwd`/contrib/etc/default ${RPM_BUILD_ROOT}/etc
58+
rsync -aHA `pwd`/contrib/etc/init ${RPM_BUILD_ROOT}/etc
59+
rsync -aHA `pwd`/contrib/etc/%{oname} ${RPM_BUILD_ROOT}/etc
60+
61+
mkdir -p ${RPM_BUILD_ROOT}/var/log/%{oname}
62+
63+
%clean
64+
RUBYDIR=/usr/bin/ruby rpmbuild/rules clean
65+
rm -rf $RPM_BUILD_ROOT
66+
67+
%post
68+
/sbin/chkconfig mysqld on
69+
70+
%files
71+
%defattr(-,root,root,-)
72+
%{prefix}/%{oname}
73+
%dir /etc/%{oname}
74+
%dir /var/log/%{oname}
75+
%config(noreplace) /etc/default/tiny-web-example-webapi
76+
%config(noreplace) /etc/default/tiny-web-example-webapp
77+
%config(noreplace) /etc/init/tiny-web-example-webapi.conf
78+
%config(noreplace) /etc/init/tiny-web-example-webapp.conf
79+
%config(noreplace) /etc/tiny-web-example/unicorn-common.conf
80+
%config(noreplace) /etc/tiny-web-example/webapi.conf
81+
%config(noreplace) /etc/tiny-web-example/webapp.yml

0 commit comments

Comments
 (0)