gotta-serve 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f6beffd9c7dbefb6ef46adbdbae5bdf6546f202a811806ea57522f506c3e3fe1
4
+ data.tar.gz: 3ffe9507ad818cc7d6bc110c72d6b6d84080e1792e681e06fcba2a2d83152aee
5
+ SHA512:
6
+ metadata.gz: 39fbc9fcac40f58592ef1d4610ab82646a8026ae2a0d8dc00e1a03be41894c410924346e4fc9627694374fd9551e74f6f6b4d06b96350921a0d650a60cdfcc3f
7
+ data.tar.gz: e909a3e0bed0c9ba6f4ac14683d24a561dc28cdc2e35268ca3cddf5ff8979c266b4eb901931e3e5f57e8fc634c46fd44a82f49414761f61b5b10f4d12e899523
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.gem
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.3
7
+ before_install: gem install bundler -v 2.0.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at paulo@parruda.net. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in gotta-serve.gemspec
4
+ gemspec
@@ -0,0 +1,201 @@
1
+ Commons Clause License Condition
2
+ ================================
3
+
4
+ The Software is provided to you by the Licensor under the License,
5
+ as defined below, subject to the following condition.
6
+
7
+ Without limiting other conditions in the License, the grant of rights
8
+ under the License will not include, and the License does not grant to
9
+ you, the right to Sell the Software.
10
+
11
+ For purposes of the foregoing, "Sell" means practicing any or all of
12
+ the rights granted to you under the License to provide to third
13
+ parties, for a fee or other consideration (including without
14
+ limitation fees for hosting or consulting/ support services related
15
+ to the Software), a product or service whose value derives, entirely
16
+ or substantially, from the functionality of the Software. Any license
17
+ notice or attribution required by the License must also include this
18
+ license condition notice.
19
+
20
+ Software: gotta-serve
21
+ License: Apache 2.0
22
+ Licensor: Paulo Arruda (parrudaj at gmail dot com)
23
+
24
+
25
+ Apache License
26
+ ==============
27
+
28
+ Version 2.0, January 2004
29
+ http://www.apache.org/licenses/
30
+
31
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
32
+
33
+ 1. Definitions.
34
+
35
+ "License" shall mean the terms and conditions for use, reproduction,
36
+ and distribution as defined by Sections 1 through 9 of this document.
37
+
38
+ "Licensor" shall mean the copyright owner or entity authorized by
39
+ the copyright owner that is granting the License.
40
+
41
+ "Legal Entity" shall mean the union of the acting entity and all
42
+ other entities that control, are controlled by, or are under common
43
+ control with that entity. For the purposes of this definition,
44
+ "control" means (i) the power, direct or indirect, to cause the
45
+ direction or management of such entity, whether by contract or
46
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
47
+ outstanding shares, or (iii) beneficial ownership of such entity.
48
+
49
+ "You" (or "Your") shall mean an individual or Legal Entity
50
+ exercising permissions granted by this License.
51
+
52
+ "Source" form shall mean the preferred form for making modifications,
53
+ including but not limited to software source code, documentation
54
+ source, and configuration files.
55
+
56
+ "Object" form shall mean any form resulting from mechanical
57
+ transformation or translation of a Source form, including but
58
+ not limited to compiled object code, generated documentation,
59
+ and conversions to other media types.
60
+
61
+ "Work" shall mean the work of authorship, whether in Source or
62
+ Object form, made available under the License, as indicated by a
63
+ copyright notice that is included in or attached to the work
64
+ (an example is provided in the Appendix below).
65
+
66
+ "Derivative Works" shall mean any work, whether in Source or Object
67
+ form, that is based on (or derived from) the Work and for which the
68
+ editorial revisions, annotations, elaborations, or other modifications
69
+ represent, as a whole, an original work of authorship. For the purposes
70
+ of this License, Derivative Works shall not include works that remain
71
+ separable from, or merely link (or bind by name) to the interfaces of,
72
+ the Work and Derivative Works thereof.
73
+
74
+ "Contribution" shall mean any work of authorship, including
75
+ the original version of the Work and any modifications or additions
76
+ to that Work or Derivative Works thereof, that is intentionally
77
+ submitted to Licensor for inclusion in the Work by the copyright owner
78
+ or by an individual or Legal Entity authorized to submit on behalf of
79
+ the copyright owner. For the purposes of this definition, "submitted"
80
+ means any form of electronic, verbal, or written communication sent
81
+ to the Licensor or its representatives, including but not limited to
82
+ communication on electronic mailing lists, source code control systems,
83
+ and issue tracking systems that are managed by, or on behalf of, the
84
+ Licensor for the purpose of discussing and improving the Work, but
85
+ excluding communication that is conspicuously marked or otherwise
86
+ designated in writing by the copyright owner as "Not a Contribution."
87
+
88
+ "Contributor" shall mean Licensor and any individual or Legal Entity
89
+ on behalf of whom a Contribution has been received by Licensor and
90
+ subsequently incorporated within the Work.
91
+
92
+ 2. Grant of Copyright License. Subject to the terms and conditions of
93
+ this License, each Contributor hereby grants to You a perpetual,
94
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
95
+ copyright license to reproduce, prepare Derivative Works of,
96
+ publicly display, publicly perform, sublicense, and distribute the
97
+ Work and such Derivative Works in Source or Object form.
98
+
99
+ 3. Grant of Patent License. Subject to the terms and conditions of
100
+ this License, each Contributor hereby grants to You a perpetual,
101
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
102
+ (except as stated in this section) patent license to make, have made,
103
+ use, offer to sell, sell, import, and otherwise transfer the Work,
104
+ where such license applies only to those patent claims licensable
105
+ by such Contributor that are necessarily infringed by their
106
+ Contribution(s) alone or by combination of their Contribution(s)
107
+ with the Work to which such Contribution(s) was submitted. If You
108
+ institute patent litigation against any entity (including a
109
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
110
+ or a Contribution incorporated within the Work constitutes direct
111
+ or contributory patent infringement, then any patent licenses
112
+ granted to You under this License for that Work shall terminate
113
+ as of the date such litigation is filed.
114
+
115
+ 4. Redistribution. You may reproduce and distribute copies of the
116
+ Work or Derivative Works thereof in any medium, with or without
117
+ modifications, and in Source or Object form, provided that You
118
+ meet the following conditions:
119
+
120
+ (a) You must give any other recipients of the Work or
121
+ Derivative Works a copy of this License; and
122
+
123
+ (b) You must cause any modified files to carry prominent notices
124
+ stating that You changed the files; and
125
+
126
+ (c) You must retain, in the Source form of any Derivative Works
127
+ that You distribute, all copyright, patent, trademark, and
128
+ attribution notices from the Source form of the Work,
129
+ excluding those notices that do not pertain to any part of
130
+ the Derivative Works; and
131
+
132
+ (d) If the Work includes a "NOTICE" text file as part of its
133
+ distribution, then any Derivative Works that You distribute must
134
+ include a readable copy of the attribution notices contained
135
+ within such NOTICE file, excluding those notices that do not
136
+ pertain to any part of the Derivative Works, in at least one
137
+ of the following places: within a NOTICE text file distributed
138
+ as part of the Derivative Works; within the Source form or
139
+ documentation, if provided along with the Derivative Works; or,
140
+ within a display generated by the Derivative Works, if and
141
+ wherever such third-party notices normally appear. The contents
142
+ of the NOTICE file are for informational purposes only and
143
+ do not modify the License. You may add Your own attribution
144
+ notices within Derivative Works that You distribute, alongside
145
+ or as an addendum to the NOTICE text from the Work, provided
146
+ that such additional attribution notices cannot be construed
147
+ as modifying the License.
148
+
149
+ You may add Your own copyright statement to Your modifications and
150
+ may provide additional or different license terms and conditions
151
+ for use, reproduction, or distribution of Your modifications, or
152
+ for any such Derivative Works as a whole, provided Your use,
153
+ reproduction, and distribution of the Work otherwise complies with
154
+ the conditions stated in this License.
155
+
156
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
157
+ any Contribution intentionally submitted for inclusion in the Work
158
+ by You to the Licensor shall be under the terms and conditions of
159
+ this License, without any additional terms or conditions.
160
+ Notwithstanding the above, nothing herein shall supersede or modify
161
+ the terms of any separate license agreement you may have executed
162
+ with Licensor regarding such Contributions.
163
+
164
+ 6. Trademarks. This License does not grant permission to use the trade
165
+ names, trademarks, service marks, or product names of the Licensor,
166
+ except as required for reasonable and customary use in describing the
167
+ origin of the Work and reproducing the content of the NOTICE file.
168
+
169
+ 7. Disclaimer of Warranty. Unless required by applicable law or
170
+ agreed to in writing, Licensor provides the Work (and each
171
+ Contributor provides its Contributions) on an "AS IS" BASIS,
172
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
173
+ implied, including, without limitation, any warranties or conditions
174
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
175
+ PARTICULAR PURPOSE. You are solely responsible for determining the
176
+ appropriateness of using or redistributing the Work and assume any
177
+ risks associated with Your exercise of permissions under this License.
178
+
179
+ 8. Limitation of Liability. In no event and under no legal theory,
180
+ whether in tort (including negligence), contract, or otherwise,
181
+ unless required by applicable law (such as deliberate and grossly
182
+ negligent acts) or agreed to in writing, shall any Contributor be
183
+ liable to You for damages, including any direct, indirect, special,
184
+ incidental, or consequential damages of any character arising as a
185
+ result of this License or out of the use or inability to use the
186
+ Work (including but not limited to damages for loss of goodwill,
187
+ work stoppage, computer failure or malfunction, or any and all
188
+ other commercial damages or losses), even if such Contributor
189
+ has been advised of the possibility of such damages.
190
+
191
+ 9. Accepting Warranty or Additional Liability. While redistributing
192
+ the Work or Derivative Works thereof, You may choose to offer,
193
+ and charge a fee for, acceptance of support, warranty, indemnity,
194
+ or other liability obligations and/or rights consistent with this
195
+ License. However, in accepting such obligations, You may act only
196
+ on Your own behalf and on Your sole responsibility, not on behalf
197
+ of any other Contributor, and only if You agree to indemnify,
198
+ defend, and hold each Contributor harmless for any liability
199
+ incurred by, or claims asserted against, such Contributor by reason
200
+ of your accepting any such warranty or additional liability.
201
+
@@ -0,0 +1,13 @@
1
+ Copyright from 2019 Paulo Arruda (parrudaj at gmail dot com)
2
+
3
+ Licensed under the Commons Clause + Apache License, Version 2.0
4
+ (the "License"); you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
@@ -0,0 +1,31 @@
1
+ # Gotta Serve!
2
+
3
+ TODO: Delete this and the text above, and describe your gem
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'gotta-serve'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install gotta-serve
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ Bug reports and pull requests are welcome on GitHub at https://github.com/gotta-dev/gotta-serve. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
28
+
29
+ ## Code of Conduct
30
+
31
+ Everyone interacting in the Gotta::Serve project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/gotta-dev/gotta-serve/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "gotta/serve"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,28 @@
1
+ #!/bin/bash
2
+ set -e
3
+ ! which rvm && echo "Sorry, but this script only works with RVM" && exit 1
4
+ [[ -z $GEM_HOME ]] && echo "You need to have \$GEM_HOME set before continuing." && exit 1
5
+ GEMS=$GEM_HOME/gems
6
+ echo Gems detected at: $GEMS
7
+ #which gotta-serve &&
8
+ echo "Removing all installed versions" && gem uninstall -a -I -x gotta-serve
9
+ echo "Cleaning up stale links"
10
+ rm -rf $GEMS/gotta-serve-0*
11
+ echo "Building gem from source"
12
+ VERSION=$(gem build gotta-serve.gemspec |grep Version|cut -f4 -d" ")
13
+ echo "Installing new gem"
14
+ gem install gotta-serve-$VERSION.gem
15
+ SOURCECODE=$PWD
16
+ INSTALL_DIR=$GEMS/gotta-serve-$VERSION
17
+ echo -e "\n\nThe directory below is about to be removed. Press ENTER to continue or CTRL+C to abort."
18
+ echo $INSTALL_DIR
19
+ read
20
+ rm -rf $INSTALL_DIR
21
+ echo "Creating symlink $INSTALL_DIR -> $SOURCECODE"
22
+ ln -s $SOURCECODE $INSTALL_DIR
23
+ echo "Running 'gotta-serve -v'"
24
+ touch ".test"
25
+ [[ -e $INSTALL_DIR/.test ]] && rm .test && echo Done && exit 0
26
+ rm -f .test
27
+ echo "ERROR: The symbolik link $INSTALL_DIR is not working."
28
+ exit 1
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'gotta/serve/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'gotta-serve'
9
+ spec.version = Gotta::Serve::VERSION
10
+ spec.authors = ['Paulo Arruda']
11
+ spec.email = ['parrudaj@gmail.com']
12
+
13
+ spec.summary = 'Gotta Serve'
14
+ spec.description = 'Gotta Serve'
15
+ spec.homepage = 'https://github.com/gotta-dev'
16
+ spec.license = 'Apache-2.0'
17
+
18
+ spec.add_runtime_dependency 'sinatra', '~> 2.0'
19
+ spec.add_runtime_dependency 'sinatra-contrib', '~> 2.0'
20
+
21
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
22
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
23
+ if spec.respond_to?(:metadata)
24
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
25
+
26
+ spec.metadata['homepage_uri'] = spec.homepage
27
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
28
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
29
+ else
30
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
31
+ 'public gem pushes.'
32
+ end
33
+
34
+ # Specify which files should be added to the gem when it is released.
35
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
36
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
37
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
38
+ end
39
+ spec.bindir = 'exe'
40
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
41
+ spec.require_paths = ['lib']
42
+
43
+ spec.add_development_dependency 'bundler', '~> 2.0'
44
+ spec.add_development_dependency 'rake', '~> 10.0'
45
+ spec.add_development_dependency 'rspec', '~> 3.0'
46
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright from 2019 Paulo Arruda (parrudaj at gmail dot com)
4
+ #
5
+ # Licensed under the Commons Clause + Apache License, Version 2.0
6
+ # (the "License"); you may not use this file except in compliance with
7
+ # the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ require 'sinatra'
19
+ require 'sinatra/contrib'
20
+ require 'gotta/serve/version'
21
+ require 'gotta/common/project'
22
+ require 'gotta/queue/memory'
23
+
24
+ module Gotta
25
+ module Serve
26
+ project = Gotta::Project.new(Dir.pwd)
27
+ Gotta::Project.config = project
28
+ require 'gotta/serve/constants'
29
+
30
+ class << self
31
+ attr_accessor :input_queue, :queue_driver
32
+ end
33
+
34
+ def self.start!(config)
35
+ queue_driver = Gotta::Queue::Memory::Driver
36
+ input_queue = queue_driver.new('input')
37
+ puts "Gotta running #{config.environment} environment."
38
+ puts "Gotta visit #{config.scheme}://#{config.bind_address}:#{config.port}"
39
+ ENV['ENVIRONMENT'] = config.environment
40
+ require 'gotta/serve/app'
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,187 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright from 2019 Paulo Arruda (parrudaj at gmail dot com)
4
+ #
5
+ # Licensed under the Commons Clause + Apache License, Version 2.0
6
+ # (the "License"); you may not use this file except in compliance with
7
+ # the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ module Gotta
19
+ module Serve
20
+ class App < Sinatra::Application
21
+ set :show_exceptions, SHOW_EXCEPTIONS
22
+ set :logging, LOGGING
23
+ set :root, SERVER_ROOT
24
+ set :public_folder, PUBLIC_DIR
25
+ set :static, SERVE_STATIC
26
+ set :static_cache_control, [:must_revalidate, :proxy_revalidate, max_age: 0]
27
+
28
+ register Sinatra::MultiRoute
29
+
30
+ before do
31
+ cache_control :must_revalidate, :proxy_revalidate, max_age: 0
32
+ end
33
+
34
+ route :head, :get, :post, :put, :patch, :delete, '/*' do
35
+ splat = params['splat'][0]
36
+ component_name = resolve_component_name(splat)
37
+ request_headers, original_request_id = set_request_headers(env)
38
+
39
+ body = parse_body(request,
40
+ request_headers['Content-Type'],
41
+ request.request_method)
42
+
43
+ query_params = parse_query(request.query_string)
44
+
45
+ secrets = Gotta::Project.config.secrets[ENVIRONMENT]
46
+ context = { environment: ENVIRONMENT }
47
+
48
+ event = Gotta::Event.new(body: body,
49
+ query_params: query_params,
50
+ headers: request_headers,
51
+ context: context,
52
+ secrets: secrets)
53
+
54
+ log_request_message(component_name,
55
+ request,
56
+ request_uuid,
57
+ query_params,
58
+ body,
59
+ request_headers)
60
+
61
+ payload = {
62
+ load_path: component_name,
63
+ response_queue: SecureRandom.uuid
64
+ event: event
65
+ }
66
+ queue = Gotta::Serve.input_queue.push(payload.to_msgpack)
67
+ response_queue = Gotta::Serve.queue_driver.new(payload[:response_queue])
68
+
69
+ time, response = parse_response(response_queue.pop)
70
+
71
+ status response.status
72
+ headers set_response_headers(response,
73
+ request_uuid,
74
+ original_request_id,
75
+ time)
76
+
77
+ response_body, print_body = parse_response(response)
78
+ log_response_message(component_name,
79
+ time,
80
+ request_uuid,
81
+ response,
82
+ print_body)
83
+
84
+ body response_body
85
+ end
86
+
87
+ def set_request_headers(env)
88
+ request_headers = parse_headers(env)
89
+ request_headers['X-Request-Id'] = SecureRandom.uuid
90
+ request_headers['Request-Method'] = request.request_method
91
+ [request_headers, request_headers['X-Original-Request-Id']]
92
+ end
93
+
94
+ def log_request_message(component_name,
95
+ request,
96
+ request_uuid,
97
+ query_params,
98
+ body,
99
+ request_headers)
100
+
101
+ puts "[#{component_name}] <- [REQUEST: #{request.request_method} \"#{request.fullpath}\"] request_id=\"#{request_uuid}\" body=#{body.inspect} query_params=#{query_params} headers=#{request_headers}"
102
+ end
103
+
104
+ def log_response_message(component_name,
105
+ time,
106
+ request_uuid,
107
+ response,
108
+ print_body)
109
+
110
+ puts "[#{component_name}] -> [RESPONSE: #{time}ms] request_id=\"#{request_uuid}\" status=#{response.status} body=#{print_body.inspect} headers=#{response.headers}"
111
+ end
112
+
113
+ def set_response_headers(response,
114
+ request_uuid,
115
+ original_request_id = nil,
116
+ time)
117
+
118
+ response.headers['X-Request-Id'] = request_uuid
119
+ response.headers['X-Original-Request-Id'] = original_request_id if original_request_id
120
+ response.headers['X-Execution-time'] = "#{time}ms"
121
+ response.headers
122
+ end
123
+
124
+ def parse_response(response)
125
+ return [Base64.urlsafe_decode64(response.body), "Base64(#{response.body})"] if response.binary?
126
+
127
+ [response.body, response.body]
128
+ end
129
+
130
+ def resolve_component_name(splat)
131
+ return Gotta::Project.config.root_to if splat == ''
132
+ return Gotta::Project.config.catch_all unless File.directory?(name)
133
+
134
+ # TODO: 'catch_all' should render when the handler file
135
+ # cannot be found.
136
+ # Maybe this could happen when the handler file can't
137
+ # be found (trigger by gotta-mod)
138
+
139
+ # if !is_a_component?(splat)
140
+ # return Gotta::Project.config.catch_all
141
+ # end
142
+ splat
143
+ end
144
+
145
+ def parse_body(request, content_type, method)
146
+ return nil if method == 'GET'
147
+ return form_content(request.env) if content_type == 'application/x-www-form-urlencoded'
148
+
149
+ body_content(request.body)
150
+ end
151
+
152
+ def form_content(request_env)
153
+ return {} if (content = request_env['rack.request.form_hash']).nil?
154
+
155
+ content.to_json
156
+ end
157
+
158
+ def body_content(request_body)
159
+ return {} if (content = request_body.read).nil?
160
+
161
+ content
162
+ end
163
+
164
+ def parse_query(query_string)
165
+ hash = {}
166
+ query_string.split('&').each do |param|
167
+ key, value = param.split('=')
168
+ hash[key] = value
169
+ end
170
+ hash
171
+ end
172
+
173
+ def parse_headers(env)
174
+ result = {}
175
+ env.select { |e| e.match(/^HTTP_/) }.each do |k, v|
176
+ newkey = k.sub(/^HTTP_/, '').split('_').map(&:capitalize).join('-')
177
+ result[newkey] = v
178
+ end
179
+ result['Content-Type'] = env['CONTENT_TYPE']
180
+ result['Request-Method'] = env['REQUEST_METHOD']
181
+ result['Content-Length'] = env['CONTENT_LENGTH']
182
+ result['Remote-Addr'] = env['REMOTE_ADDR']
183
+ result
184
+ end
185
+ end
186
+ end
187
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright from 2019 Paulo Arruda (parrudaj at gmail dot com)
4
+ #
5
+ # Licensed under the Commons Clause + Apache License, Version 2.0
6
+ # (the "License"); you may not use this file except in compliance with
7
+ # the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ Dir.glob("#{__dir__}/cli/*.rb").each { |f| require f }
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright from 2019 Paulo Arruda (parrudaj at gmail dot com)
4
+ #
5
+ # Licensed under the Commons Clause + Apache License, Version 2.0
6
+ # (the "License"); you may not use this file except in compliance with
7
+ # the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ require 'gotta/cli/command'
19
+
20
+ module Gotta
21
+ module Serve
22
+ module CLI
23
+ class Serve < Gotta::CLI::Command::Base
24
+ command 'serve'
25
+
26
+ def run!
27
+ require 'gotta/serve'
28
+ Gotta::Serve.start!(self)
29
+ end
30
+
31
+ def scheme
32
+ "http#{'s' if ssl}"
33
+ end
34
+
35
+ def port
36
+ options '-p', '--port', default: 3000
37
+ end
38
+
39
+ def ssl
40
+ flag '--ssl', default: false
41
+ end
42
+
43
+ def bind_address
44
+ options '-b', '--bind-address', default: '0.0.0.0'
45
+ end
46
+
47
+ def environment
48
+ options '-e', '--env', default: 'development'
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright from 2019 Paulo Arruda (parrudaj at gmail dot com)
4
+ #
5
+ # Licensed under the Commons Clause + Apache License, Version 2.0
6
+ # (the "License"); you may not use this file except in compliance with
7
+ # the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ module Gotta
19
+ module Serve
20
+ SERVER_ROOT = Dir.pwd
21
+ PUBLIC_DIR = Project.config.public_dir
22
+ SHOW_EXCEPTIONS = true
23
+ LOGGING = true
24
+ SERVE_STATIC = true
25
+ end
26
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright from 2019 Paulo Arruda (parrudaj at gmail dot com)
4
+ #
5
+ # Licensed under the Commons Clause + Apache License, Version 2.0
6
+ # (the "License"); you may not use this file except in compliance with
7
+ # the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ module Gotta
19
+ module Serve
20
+ VERSION = "0.1.0"
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gotta-serve
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Paulo Arruda
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-06-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sinatra
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sinatra-contrib
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: Gotta Serve
84
+ email:
85
+ - parrudaj@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - CODE_OF_CONDUCT.md
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - NOTICE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - dev-setup.sh
102
+ - gotta-serve.gemspec
103
+ - lib/gotta/serve.rb
104
+ - lib/gotta/serve/app.rb
105
+ - lib/gotta/serve/cli.rb
106
+ - lib/gotta/serve/cli/serve.rb
107
+ - lib/gotta/serve/constants.rb
108
+ - lib/gotta/serve/version.rb
109
+ homepage: https://github.com/gotta-dev
110
+ licenses:
111
+ - Apache-2.0
112
+ metadata:
113
+ homepage_uri: https://github.com/gotta-dev
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.7.8
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Gotta Serve
134
+ test_files: []