keight 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +263 -0
- data/Rakefile +92 -0
- data/bench/bench.rb +278 -0
- data/bench/benchmarker.rb +502 -0
- data/bin/k8rb +496 -0
- data/keight.gemspec +36 -0
- data/lib/keight/skeleton/.gitignore +10 -0
- data/lib/keight/skeleton/app/action.rb +98 -0
- data/lib/keight/skeleton/app/api/hello.rb +39 -0
- data/lib/keight/skeleton/app/form/.keep +0 -0
- data/lib/keight/skeleton/app/helper/.keep +0 -0
- data/lib/keight/skeleton/app/model/.keep +0 -0
- data/lib/keight/skeleton/app/model.rb +144 -0
- data/lib/keight/skeleton/app/page/welcome.rb +17 -0
- data/lib/keight/skeleton/app/template/_layout.html.eruby +56 -0
- data/lib/keight/skeleton/app/template/welcome.html.eruby +6 -0
- data/lib/keight/skeleton/app/usecase/.keep +0 -0
- data/lib/keight/skeleton/config/app.rb +29 -0
- data/lib/keight/skeleton/config/app_dev.private +11 -0
- data/lib/keight/skeleton/config/app_dev.rb +8 -0
- data/lib/keight/skeleton/config/app_prod.rb +7 -0
- data/lib/keight/skeleton/config/app_stg.rb +5 -0
- data/lib/keight/skeleton/config/app_test.private +11 -0
- data/lib/keight/skeleton/config/app_test.rb +8 -0
- data/lib/keight/skeleton/config/server_puma.rb +22 -0
- data/lib/keight/skeleton/config/server_unicorn.rb +21 -0
- data/lib/keight/skeleton/config/urlpath_mapping.rb +16 -0
- data/lib/keight/skeleton/config.rb +44 -0
- data/lib/keight/skeleton/config.ru +21 -0
- data/lib/keight/skeleton/index.txt +38 -0
- data/lib/keight/skeleton/static/lib/jquery/1.11.3/jquery.min.js +6 -0
- data/lib/keight/skeleton/static/lib/jquery/1.11.3/jquery.min.js.gz +0 -0
- data/lib/keight/skeleton/static/lib/modernizr/2.8.3/modernizr.min.js +4 -0
- data/lib/keight/skeleton/static/lib/modernizr/2.8.3/modernizr.min.js.gz +0 -0
- data/lib/keight/skeleton/tmp/upload/.keep +0 -0
- data/lib/keight.rb +2017 -0
- data/test/data/example1.jpg +0 -0
- data/test/data/example1.png +0 -0
- data/test/data/multipart.form +0 -0
- data/test/data/wabisabi.js +77 -0
- data/test/data/wabisabi.js.gz +0 -0
- data/test/keight_test.rb +3161 -0
- data/test/oktest.rb +1537 -0
- metadata +114 -0
@@ -0,0 +1,144 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
##
|
4
|
+
## Define base classes of models here.
|
5
|
+
##
|
6
|
+
|
7
|
+
## ***********************************************
|
8
|
+
## The followings are just examples.
|
9
|
+
## Remove them and define your own classes.
|
10
|
+
## ***********************************************
|
11
|
+
|
12
|
+
class Entity
|
13
|
+
end
|
14
|
+
|
15
|
+
## ex: Customer, Department, Company, Product, ...
|
16
|
+
class ResourceEntity < Entity # or NounEntity
|
17
|
+
end
|
18
|
+
|
19
|
+
## ex: SalesOrder, Shipping, Requisition, Approval, ...
|
20
|
+
class EventEntity < Entity # or VerbEntity
|
21
|
+
end
|
22
|
+
|
23
|
+
## ex: Client, Supplier, Position, ...
|
24
|
+
class RoleEntity < Entity
|
25
|
+
end
|
26
|
+
|
27
|
+
## ex: Belonging, Writing, BOM, ...
|
28
|
+
class MappingEntity < Entity
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
## Define event class as exception in order to be
|
33
|
+
## caught in My::Action class.
|
34
|
+
class Event < Exception
|
35
|
+
end
|
36
|
+
|
37
|
+
class NotExist < Event
|
38
|
+
end
|
39
|
+
|
40
|
+
class NotPermitted < Event
|
41
|
+
end
|
42
|
+
|
43
|
+
class NotCorrect < Event
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
## Form class
|
48
|
+
class Form
|
49
|
+
|
50
|
+
def initialize(params={})
|
51
|
+
@params = params
|
52
|
+
end
|
53
|
+
|
54
|
+
attr_reader :params, :errors
|
55
|
+
|
56
|
+
def self.new_from(model)
|
57
|
+
params = {
|
58
|
+
#'val' => model.val,
|
59
|
+
}
|
60
|
+
self.new(params)
|
61
|
+
end
|
62
|
+
|
63
|
+
def validate
|
64
|
+
@errors = errors = {}
|
65
|
+
#
|
66
|
+
#k = 'val'
|
67
|
+
#v = @params[k].to_s.strip
|
68
|
+
#if v.empty?
|
69
|
+
# errors[k] = "Required"
|
70
|
+
#elsif v !~ /\A\d+\z/
|
71
|
+
# errors[k] = "Integer expected"
|
72
|
+
#end
|
73
|
+
#@params[k] = v
|
74
|
+
#
|
75
|
+
return errors
|
76
|
+
end
|
77
|
+
|
78
|
+
def error(name)
|
79
|
+
@errors[name]
|
80
|
+
end
|
81
|
+
|
82
|
+
def populate_into(model)
|
83
|
+
#model.val = @params['val'].to_i
|
84
|
+
return model
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
## Operation class (aka Service class)
|
91
|
+
class Operation
|
92
|
+
|
93
|
+
def initialize(db, login_user=nil)
|
94
|
+
@db = db
|
95
|
+
@login_user = login_user
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
|
101
|
+
## Usecase class in order to separate business logics from framework.
|
102
|
+
class UseCase
|
103
|
+
|
104
|
+
def initialize(db, login_user=nil)
|
105
|
+
@db = db
|
106
|
+
@login_user = login_user
|
107
|
+
end
|
108
|
+
|
109
|
+
def run(params, *urlpath_params)
|
110
|
+
errors = validate(params)
|
111
|
+
return nil, errors if errors
|
112
|
+
args = accommodate(params, *urlpath_params)
|
113
|
+
model, error = execute(*args)
|
114
|
+
return nil, error if error
|
115
|
+
content = represent(model)
|
116
|
+
return content, nil
|
117
|
+
end
|
118
|
+
|
119
|
+
private
|
120
|
+
|
121
|
+
## validate params and return errors as Hash object.
|
122
|
+
def validate(params)
|
123
|
+
errors = {}
|
124
|
+
return errors
|
125
|
+
end
|
126
|
+
|
127
|
+
## convert params and urlpath params into args of execute()
|
128
|
+
def accommodate(params, *urlpath_params)
|
129
|
+
args = urlpath_params + [params]
|
130
|
+
return args
|
131
|
+
end
|
132
|
+
|
133
|
+
## execute business logic
|
134
|
+
def execute(*args)
|
135
|
+
model = nil
|
136
|
+
return model
|
137
|
+
end
|
138
|
+
|
139
|
+
## convert model object into JSON or HTML
|
140
|
+
def represent(model)
|
141
|
+
return {}
|
142
|
+
end
|
143
|
+
|
144
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
<% # -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
@page_title ||= "Example"
|
4
|
+
|
5
|
+
#cdn_baseurl = 'https://ajax.googleapis.com/ajax/libs' # Google
|
6
|
+
cdn_baseurl = 'https://cdnjs.cloudflare.com/ajax/libs' # CDNJS
|
7
|
+
|
8
|
+
ga_code = $config.google_analytics_code
|
9
|
+
|
10
|
+
if $config.app_env == 'dev'
|
11
|
+
cdn_baseurl = '/static/lib'
|
12
|
+
ga_code = nil
|
13
|
+
end
|
14
|
+
|
15
|
+
%>
|
16
|
+
<!doctype html>
|
17
|
+
<html class="no-js" lang="">
|
18
|
+
<head>
|
19
|
+
<meta charset="utf-8" />
|
20
|
+
<meta http-equiv="x-ua-compatible" content="ie=edge" />
|
21
|
+
<title><%= @page_title %></title>
|
22
|
+
<meta name="description" content="" />
|
23
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
24
|
+
<!--link rel="stylesheet" href="/static/css/normalize.css" /-->
|
25
|
+
<!--link rel="stylesheet" href="/static/css/main.css" /-->
|
26
|
+
<script src="<%= cdn_baseurl %>/modernizr/2.8.3/modernizr.min.js"></script>
|
27
|
+
</head>
|
28
|
+
<body>
|
29
|
+
|
30
|
+
<header>
|
31
|
+
</header>
|
32
|
+
<div id="main" class="main">
|
33
|
+
<%== @_content %>
|
34
|
+
</div>
|
35
|
+
<footer>
|
36
|
+
</footer>
|
37
|
+
|
38
|
+
<script src="<%= cdn_baseurl %>/jquery/1.11.3/jquery.min.js"></script>
|
39
|
+
<script>window.jQuery || document.write('<script src="/static/lib/jquery/1.11.3/jquery.min.js"><\/script>')</script>
|
40
|
+
|
41
|
+
<% ### load site-specific files ### %>
|
42
|
+
<!--script src="/static/js/site.js"></script-->
|
43
|
+
|
44
|
+
<% ### Google Analytics ### %>
|
45
|
+
<% if ga_code %>
|
46
|
+
<script>
|
47
|
+
(function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
|
48
|
+
function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
|
49
|
+
e=o.createElement(i);r=o.getElementsByTagName(i)[0];
|
50
|
+
e.src='https://www.google-analytics.com/analytics.js';
|
51
|
+
r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
|
52
|
+
ga('create','<%= ga_code %>','auto');ga('send','pageview');
|
53
|
+
</script>
|
54
|
+
<% end %>
|
55
|
+
</body>
|
56
|
+
</html>
|
File without changes
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
class Config
|
4
|
+
|
5
|
+
add :app_env , ENV['APP_ENV'] , "environment name"
|
6
|
+
|
7
|
+
## database
|
8
|
+
add :db_host , 'localhost' , "DB host name"
|
9
|
+
add :db_port , 5432 , "DB port number"
|
10
|
+
add :db_name , 'xxxx' , "DB database"
|
11
|
+
add :db_user , 'xxxx' , "DB user name"
|
12
|
+
add :db_pass , SECRET , "DB password"
|
13
|
+
|
14
|
+
## Rack::Session::Cookie
|
15
|
+
add :session_key , 'rack.session' , "cookie name"
|
16
|
+
add :session_domain , nil , "cookie domain"
|
17
|
+
add :session_path , '/' , "cookie path"
|
18
|
+
add :session_expire_after , 24 * 60 * 60 , "cookie expires"
|
19
|
+
add :session_secret , SECRET , "secret key"
|
20
|
+
add :session_old_secret , SECRET , "old secret key"
|
21
|
+
|
22
|
+
## Keight
|
23
|
+
add :k8_upload_dir , './tmp/upload' , "for uploaded files"
|
24
|
+
add :k8_rackapp_urlpath_cache_size , 0 , "0: cache disabled"
|
25
|
+
|
26
|
+
## add your own configs here
|
27
|
+
add :google_analytics_code , nil , "ex: 'UA-XXXXX-X'"
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
## usage: puma -C config/server_puma.rb
|
4
|
+
## ref: https://github.com/puma/puma
|
5
|
+
|
6
|
+
environment 'production'
|
7
|
+
port 9292
|
8
|
+
daemonize false
|
9
|
+
threads 8,32
|
10
|
+
workers 3
|
11
|
+
preload_app!
|
12
|
+
|
13
|
+
on_worker_boot do
|
14
|
+
##
|
15
|
+
## configuration here
|
16
|
+
##
|
17
|
+
|
18
|
+
#ActiveSupport.on_load(:active_record) do
|
19
|
+
# ActiveRecord::Base.establish_connection
|
20
|
+
#end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
## usage: unicorn -p 9292 -E production -c ./config/server_unicorn.rb
|
4
|
+
## ref: https://devcenter.heroku.com/articles/rails-unicorn
|
5
|
+
|
6
|
+
worker_processes Integer(ENV["WEB_CONCURRENCY"] || 4)
|
7
|
+
timeout 10
|
8
|
+
preload_app true
|
9
|
+
|
10
|
+
before_fork do |server, worker|
|
11
|
+
Signal.trap 'TERM' do
|
12
|
+
puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
|
13
|
+
Process.kill 'QUIT', Process.pid
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
after_fork do |server, worker|
|
18
|
+
Signal.trap 'TERM' do
|
19
|
+
puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
$urlpath_mapping = [
|
4
|
+
['/' , "./app/page/welcome:WelcomePage"],
|
5
|
+
['/api', [
|
6
|
+
['/hello' , "./app/api/hello:HelloAPI"],
|
7
|
+
#['/books' , "./app/api/books:BooksAPI"],
|
8
|
+
#['/books/{book_id}/comments' , "./app/api/books:BookCommentsAPI"],
|
9
|
+
#['/orders' , "./app/api/orders:OrdersAPI"],
|
10
|
+
]],
|
11
|
+
['/admin', [
|
12
|
+
#['/books' , "./app/admin/books:AdminBooksPage"],
|
13
|
+
#['/orders' , "./app/admin/orders:AdminOrdersPage"],
|
14
|
+
]],
|
15
|
+
['/static' , "./app/action:My::StaticPage"],
|
16
|
+
]
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
app_env = ENV['APP_ENV'] # 'dev', 'prod', 'stg', 'test'
|
4
|
+
if app_env.nil? || app_env.empty?
|
5
|
+
$stderr.write '
|
6
|
+
**
|
7
|
+
** ERROR: Set $APP_ENV environment variable at first.
|
8
|
+
**
|
9
|
+
** Example (MacOSX, UNIX):
|
10
|
+
** $ export APP_ENV=dev # development mode
|
11
|
+
** $ export APP_ENV=prod # production mode
|
12
|
+
** $ export APP_ENV=stg # staging mode
|
13
|
+
**
|
14
|
+
'
|
15
|
+
exit 1
|
16
|
+
end
|
17
|
+
|
18
|
+
if defined?(Config) # Ruby < 2.2 has obsoleted 'Config' object
|
19
|
+
Object.class_eval do
|
20
|
+
remove_const :Config
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
require 'keight'
|
26
|
+
|
27
|
+
class Config < K8::BaseConfig
|
28
|
+
end
|
29
|
+
|
30
|
+
require_relative "config/app"
|
31
|
+
require_relative "config/app_#{app_env}"
|
32
|
+
fpath = File.join(File.dirname(__FILE__), "config", "app_#{app_env}.private")
|
33
|
+
load fpath if File.file?(fpath)
|
34
|
+
|
35
|
+
errmsg = Config.validate_values()
|
36
|
+
if errmsg
|
37
|
+
$stderr.write(errmsg)
|
38
|
+
exit 1
|
39
|
+
end
|
40
|
+
|
41
|
+
## directory to place uploaded files
|
42
|
+
ENV['K8_UPLOAD_DIR'] = $config.k8_upload_dir if $config.k8_upload_dir
|
43
|
+
|
44
|
+
$config = Config.new()
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'keight'
|
4
|
+
|
5
|
+
## create $config object
|
6
|
+
require_relative 'config'
|
7
|
+
|
8
|
+
## get $urlpath_mapping
|
9
|
+
require_relative 'config/urlpath_mapping'
|
10
|
+
|
11
|
+
## create application object
|
12
|
+
opts = $config.get_all(:k8_rackapp_) # ex: {urlpath_cache_size: 1000}
|
13
|
+
app = K8::RackApplication.new($urlpath_mapping, opts)
|
14
|
+
$urlpath_mapping = nil
|
15
|
+
|
16
|
+
## cookie store session
|
17
|
+
require 'rack/session/cookie'
|
18
|
+
use Rack::Session::Cookie, $config.get_all(:session_)
|
19
|
+
|
20
|
+
## start application
|
21
|
+
run app
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
config.ru for 'rackup' command
|
3
|
+
config.rb define $config object
|
4
|
+
config/ configurations
|
5
|
+
config/app.rb common config
|
6
|
+
config/app_dev.rb for development
|
7
|
+
config/app_dev.private (private configs)
|
8
|
+
config/app_prod.rb for production
|
9
|
+
config/app_stg.rb for staging
|
10
|
+
config/app_test.rb for test
|
11
|
+
config/app_test.private (private configs)
|
12
|
+
config/server_unicorn.rb for unicorn server
|
13
|
+
config/server_puma.rb for puma server
|
14
|
+
config/urlpath_mapping.rb urlpath mapping
|
15
|
+
app/ application code
|
16
|
+
app/action.rb base action class
|
17
|
+
app/api/ for API
|
18
|
+
app/api/hello.rb example API
|
19
|
+
app/page/ for HTML
|
20
|
+
app/page/welcome.rb example HTML page
|
21
|
+
app/model.rb base classes of models
|
22
|
+
app/model/ model classes
|
23
|
+
app/usecase/ usecase classes
|
24
|
+
app/form/ HTML form
|
25
|
+
app/helper/ helper modules
|
26
|
+
app/template/ template files
|
27
|
+
app/template/_layout.html.eruby default layout template
|
28
|
+
app/template/welcome.html.eruby welcome page template
|
29
|
+
static/ static files
|
30
|
+
static/lib/ library such as jquery
|
31
|
+
static/lib/jquery/
|
32
|
+
static/lib/jquery/1.11.3
|
33
|
+
static/lib/modernizr/
|
34
|
+
static/lib/modernizr/2.8.3
|
35
|
+
tmp/ temporary directory
|
36
|
+
tmp/upload/ for uploaded files
|
37
|
+
.gitignore for Git
|
38
|
+
|