ratalada 0.2.0
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/.envrc +1 -0
- data/.gitignore +11 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +125 -0
- data/LICENSE +201 -0
- data/README.md +108 -0
- data/Rakefile +11 -0
- data/bin/console +8 -0
- data/bin/increment-version +44 -0
- data/bin/release-gem +32 -0
- data/bin/setup +10 -0
- data/bin/tag-version +19 -0
- data/bin/test +14 -0
- data/examples/README.md +56 -0
- data/examples/brute.rb +28 -0
- data/examples/falcon.rb +16 -0
- data/examples/puma.rb +16 -0
- data/examples/sinatra.rb +23 -0
- data/exe/ratalada +6 -0
- data/flake.lock +61 -0
- data/flake.nix +38 -0
- data/lib/ratalada/falcon.rb +41 -0
- data/lib/ratalada/puma.rb +24 -0
- data/lib/ratalada/sinatra.rb +20 -0
- data/lib/ratalada/version.rb +5 -0
- data/lib/ratalada/version.rb.erb +5 -0
- data/lib/ratalada.rb +121 -0
- data/ratalada.gemspec +38 -0
- data/ratalada.gemspec.erb +34 -0
- metadata +146 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 151b6598952c6c1c73529cae9e85a504fc988a9fb9d2e204b2b676ad152764cd
|
|
4
|
+
data.tar.gz: 0be2cfa6dec001003c79735dd0cfcba9efa7b009eab66cdaea871e1644337a92
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: a64d981797ad742c62841c638ad2d748e2c3f0ea6906db0678f5fd871e8a28d18ec0b655db234bc7950d480f2112d4dff376d275b084207b5d54a9490f310cc4
|
|
7
|
+
data.tar.gz: d267fee55bb3af866a888539a9f32d6bc3715a5e6091415a2a9523ec6c9eb069603628b7595a5786c1b6f6c5b7281684a936d49570904f52c34f31a1e7ecbb29
|
data/.envrc
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
use flake
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
ratalada (0.1.1)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
async (2.42.0)
|
|
10
|
+
console (~> 1.29)
|
|
11
|
+
fiber-annotation
|
|
12
|
+
io-event (~> 1.11)
|
|
13
|
+
metrics (~> 0.12)
|
|
14
|
+
traces (~> 0.18)
|
|
15
|
+
async-container (0.37.0)
|
|
16
|
+
async (~> 2.22)
|
|
17
|
+
async-http (0.95.1)
|
|
18
|
+
async (>= 2.10.2)
|
|
19
|
+
async-pool (~> 0.11)
|
|
20
|
+
io-endpoint (~> 0.14)
|
|
21
|
+
io-stream (~> 0.6)
|
|
22
|
+
metrics (~> 0.12)
|
|
23
|
+
protocol-http (~> 0.62)
|
|
24
|
+
protocol-http1 (~> 0.39)
|
|
25
|
+
protocol-http2 (~> 0.26)
|
|
26
|
+
protocol-url (~> 0.2)
|
|
27
|
+
traces (~> 0.10)
|
|
28
|
+
async-http-cache (0.4.6)
|
|
29
|
+
async-http (~> 0.56)
|
|
30
|
+
async-pool (0.11.2)
|
|
31
|
+
async (>= 2.0)
|
|
32
|
+
async-service (0.24.1)
|
|
33
|
+
async
|
|
34
|
+
async-container (~> 0.34)
|
|
35
|
+
string-format (~> 0.2)
|
|
36
|
+
async-utilization (0.4.0)
|
|
37
|
+
console (~> 1.0)
|
|
38
|
+
bake (0.25.0)
|
|
39
|
+
bigdecimal
|
|
40
|
+
samovar (~> 2.1)
|
|
41
|
+
base64 (0.3.0)
|
|
42
|
+
bigdecimal (4.1.2)
|
|
43
|
+
console (1.36.0)
|
|
44
|
+
fiber-annotation
|
|
45
|
+
fiber-local (~> 1.1)
|
|
46
|
+
json
|
|
47
|
+
falcon (0.55.5)
|
|
48
|
+
async
|
|
49
|
+
async-container (~> 0.20)
|
|
50
|
+
async-http (~> 0.75)
|
|
51
|
+
async-http-cache (~> 0.4)
|
|
52
|
+
async-service (~> 0.19)
|
|
53
|
+
async-utilization (~> 0.3)
|
|
54
|
+
bundler
|
|
55
|
+
localhost (~> 1.1)
|
|
56
|
+
openssl (>= 3.0)
|
|
57
|
+
protocol-http (~> 0.31)
|
|
58
|
+
protocol-rack (~> 0.7)
|
|
59
|
+
samovar (~> 2.3)
|
|
60
|
+
fiber-annotation (0.2.0)
|
|
61
|
+
fiber-local (1.1.0)
|
|
62
|
+
fiber-storage
|
|
63
|
+
fiber-storage (1.0.1)
|
|
64
|
+
io-endpoint (0.17.2)
|
|
65
|
+
io-event (1.19.1)
|
|
66
|
+
io-stream (0.13.1)
|
|
67
|
+
json (2.20.0)
|
|
68
|
+
localhost (1.8.0)
|
|
69
|
+
bake
|
|
70
|
+
logger (1.7.0)
|
|
71
|
+
metrics (0.15.0)
|
|
72
|
+
minitest (5.27.0)
|
|
73
|
+
mustermann (3.1.1)
|
|
74
|
+
nio4r (2.7.5)
|
|
75
|
+
openssl (4.0.2)
|
|
76
|
+
protocol-hpack (1.5.1)
|
|
77
|
+
protocol-http (0.62.2)
|
|
78
|
+
protocol-http1 (0.39.0)
|
|
79
|
+
protocol-http (~> 0.62)
|
|
80
|
+
protocol-http2 (0.26.0)
|
|
81
|
+
protocol-hpack (~> 1.4)
|
|
82
|
+
protocol-http (~> 0.62)
|
|
83
|
+
protocol-rack (0.22.1)
|
|
84
|
+
io-stream (>= 0.10)
|
|
85
|
+
protocol-http (~> 0.58)
|
|
86
|
+
rack (>= 1.0)
|
|
87
|
+
protocol-url (0.4.0)
|
|
88
|
+
puma (8.0.2)
|
|
89
|
+
nio4r (~> 2.0)
|
|
90
|
+
rack (3.2.6)
|
|
91
|
+
rack-protection (4.2.1)
|
|
92
|
+
base64 (>= 0.1.0)
|
|
93
|
+
logger (>= 1.6.0)
|
|
94
|
+
rack (>= 3.0.0, < 4)
|
|
95
|
+
rack-session (2.1.2)
|
|
96
|
+
base64 (>= 0.1.0)
|
|
97
|
+
rack (>= 3.0.0)
|
|
98
|
+
rake (13.4.2)
|
|
99
|
+
samovar (2.5.1)
|
|
100
|
+
console (~> 1.0)
|
|
101
|
+
sinatra (4.2.1)
|
|
102
|
+
logger (>= 1.6.0)
|
|
103
|
+
mustermann (~> 3.0)
|
|
104
|
+
rack (>= 3.0.0, < 4)
|
|
105
|
+
rack-protection (= 4.2.1)
|
|
106
|
+
rack-session (>= 2.0.0, < 3)
|
|
107
|
+
tilt (~> 2.0)
|
|
108
|
+
string-format (0.2.0)
|
|
109
|
+
tilt (2.8.0)
|
|
110
|
+
traces (0.18.2)
|
|
111
|
+
|
|
112
|
+
PLATFORMS
|
|
113
|
+
ruby
|
|
114
|
+
x86_64-linux
|
|
115
|
+
|
|
116
|
+
DEPENDENCIES
|
|
117
|
+
falcon
|
|
118
|
+
minitest (~> 5.0)
|
|
119
|
+
puma
|
|
120
|
+
rake (~> 13.0)
|
|
121
|
+
ratalada!
|
|
122
|
+
sinatra
|
|
123
|
+
|
|
124
|
+
BUNDLED WITH
|
|
125
|
+
2.6.9
|
data/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
data/README.md
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# ratalada
|
|
2
|
+
|
|
3
|
+
A DSL for running Rack servers as easily as you can in JavaScript.
|
|
4
|
+
|
|
5
|
+
```ruby
|
|
6
|
+
require "ratalada/puma"
|
|
7
|
+
|
|
8
|
+
Server.run do |request|
|
|
9
|
+
case request
|
|
10
|
+
in ["GET", "/"] then "hello\n"
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
That's a whole app. Run the file, and it's listening on `http://127.0.0.1:9292`.
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
gem install ratalada
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Ratalada has no runtime dependencies of its own — install whichever server you
|
|
24
|
+
want to run on (`puma`, `falcon`, or `sinatra` for that flavour of DSL).
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
Requiring a backend picks the server and defines the top-level `Server`
|
|
29
|
+
constant:
|
|
30
|
+
|
|
31
|
+
```ruby
|
|
32
|
+
require "ratalada/puma" # or
|
|
33
|
+
require "ratalada/falcon"
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
The `Server.run` block is a router: it receives each request and returns a
|
|
37
|
+
handler for it. A request pattern-matches as `[verb, path]` (or by keys:
|
|
38
|
+
`in {verb:, path:, query:}`), and a handler can be:
|
|
39
|
+
|
|
40
|
+
- a `String` — sent as a `200 text/plain` response
|
|
41
|
+
- a callable — called with the request, its result handled the same way
|
|
42
|
+
- a `[status, headers, body]` triplet — used as-is
|
|
43
|
+
- nothing (`nil` or a fall-through `case ... in`) — a `404`
|
|
44
|
+
|
|
45
|
+
```ruby
|
|
46
|
+
require "ratalada/falcon"
|
|
47
|
+
|
|
48
|
+
Server.run do |request|
|
|
49
|
+
case request
|
|
50
|
+
in ["GET", "/"] then "hello\n"
|
|
51
|
+
in ["GET", "/up"] then "ok\n"
|
|
52
|
+
in ["POST", "/echo"] then ->(req) { [200, { "content-type" => "text/plain" }, req.body] }
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Prefer Sinatra's routing? Swap the frontend and keep whichever backend you
|
|
58
|
+
required:
|
|
59
|
+
|
|
60
|
+
```ruby
|
|
61
|
+
require "ratalada/falcon"
|
|
62
|
+
require "ratalada/sinatra"
|
|
63
|
+
|
|
64
|
+
Server.run do
|
|
65
|
+
get "/" do
|
|
66
|
+
"hello\n"
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
The host and port default to `127.0.0.1:9292`, configurable via the `HOST` and
|
|
72
|
+
`PORT` environment variables or explicitly:
|
|
73
|
+
|
|
74
|
+
```ruby
|
|
75
|
+
Server.run(host: "0.0.0.0", port: 3000) do |request|
|
|
76
|
+
# ...
|
|
77
|
+
end
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Like node, one process is one event loop: plenty for IO-bound work, but only
|
|
81
|
+
one core of Ruby. To use more cores, `count:` (or the `COUNT` environment
|
|
82
|
+
variable) runs that many forked workers accepting from a shared socket — the
|
|
83
|
+
equivalent of node's `cluster` module, and with the same contract: each worker
|
|
84
|
+
has its own state, so anything shared between requests (sessions, caches)
|
|
85
|
+
needs an external store or `count: 1` (the default).
|
|
86
|
+
|
|
87
|
+
```ruby
|
|
88
|
+
Server.run(count: 4) do |request|
|
|
89
|
+
# ...
|
|
90
|
+
end
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Currently only the falcon backend forks workers; the puma backend warns and
|
|
94
|
+
ignores `count:`.
|
|
95
|
+
|
|
96
|
+
See [examples/](examples/) for complete runnable servers.
|
|
97
|
+
|
|
98
|
+
## Development
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
bin/setup # install dependencies
|
|
102
|
+
bin/test # run the tests
|
|
103
|
+
bin/console # interactive prompt
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## License
|
|
107
|
+
|
|
108
|
+
[MIT](LICENSE)
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require "erb"
|
|
5
|
+
require_relative "../lib/ratalada/version"
|
|
6
|
+
|
|
7
|
+
USAGE = <<~TEXT
|
|
8
|
+
Usage: bin/increment-version <major|minor|patch>
|
|
9
|
+
TEXT
|
|
10
|
+
|
|
11
|
+
segment = ARGV[0]
|
|
12
|
+
|
|
13
|
+
unless %w[major minor patch].include?(segment)
|
|
14
|
+
warn USAGE
|
|
15
|
+
exit 1
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
current = Ratalada::VERSION
|
|
19
|
+
major, minor, patch = current.split(".").map(&:to_i)
|
|
20
|
+
|
|
21
|
+
case segment
|
|
22
|
+
when "major"
|
|
23
|
+
major += 1
|
|
24
|
+
minor = 0
|
|
25
|
+
patch = 0
|
|
26
|
+
when "minor"
|
|
27
|
+
minor += 1
|
|
28
|
+
patch = 0
|
|
29
|
+
when "patch"
|
|
30
|
+
patch += 1
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
version = "#{major}.#{minor}.#{patch}"
|
|
34
|
+
|
|
35
|
+
template_path = File.expand_path("../lib/ratalada/version.rb.erb", __dir__)
|
|
36
|
+
output_path = File.expand_path("../lib/ratalada/version.rb", __dir__)
|
|
37
|
+
|
|
38
|
+
template = ERB.new(File.read(template_path))
|
|
39
|
+
result = template.result(binding)
|
|
40
|
+
|
|
41
|
+
File.write(output_path, result)
|
|
42
|
+
|
|
43
|
+
puts "#{current} -> #{version}"
|
|
44
|
+
|
data/bin/release-gem
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require_relative "../lib/ratalada/version"
|
|
5
|
+
|
|
6
|
+
local_version = Ratalada::VERSION
|
|
7
|
+
gem_name = "ratalada"
|
|
8
|
+
gemspec = "ratalada.gemspec"
|
|
9
|
+
|
|
10
|
+
puts "Local version: #{local_version}"
|
|
11
|
+
|
|
12
|
+
remote_output = `gem specification #{gem_name} version --remote 2>&1`
|
|
13
|
+
|
|
14
|
+
if $?.success?
|
|
15
|
+
remote_version = remote_output[/version: (.+)/, 1]&.strip
|
|
16
|
+
puts "Remote version: #{remote_version}"
|
|
17
|
+
|
|
18
|
+
if Gem::Version.new(local_version) <= Gem::Version.new(remote_version)
|
|
19
|
+
abort "ERROR: Local version (#{local_version}) has not been incremented past remote (#{remote_version})"
|
|
20
|
+
end
|
|
21
|
+
else
|
|
22
|
+
puts "Gem not yet published remotely, proceeding with first release"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
puts "Building #{gemspec}..."
|
|
26
|
+
system("gem build #{gemspec}") || abort("ERROR: gem build failed")
|
|
27
|
+
|
|
28
|
+
gem_file = "#{gem_name}-#{local_version}.gem"
|
|
29
|
+
puts "Pushing #{gem_file}..."
|
|
30
|
+
system("gem push #{gem_file}") || abort("ERROR: gem push failed")
|
|
31
|
+
|
|
32
|
+
puts "Released #{gem_name} #{local_version}"
|
data/bin/setup
ADDED
data/bin/tag-version
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require_relative "../lib/ratalada/version"
|
|
5
|
+
|
|
6
|
+
version = Ratalada::VERSION
|
|
7
|
+
tag = "v#{version}"
|
|
8
|
+
|
|
9
|
+
existing = `git tag -l #{tag}`.strip
|
|
10
|
+
unless existing.empty?
|
|
11
|
+
abort "ERROR: Tag #{tag} already exists"
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
system("git tag #{tag}") || abort("ERROR: Failed to create tag #{tag}")
|
|
15
|
+
|
|
16
|
+
puts "Tagged #{tag}"
|
|
17
|
+
puts ""
|
|
18
|
+
puts "To push the tag, run:"
|
|
19
|
+
puts " git push --tags"
|
data/bin/test
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
$LOAD_PATH.unshift File.expand_path("../test", __dir__)
|
|
5
|
+
|
|
6
|
+
require "bundler/setup"
|
|
7
|
+
|
|
8
|
+
Dir.chdir(File.expand_path("..", __dir__))
|
|
9
|
+
|
|
10
|
+
if ARGV.empty?
|
|
11
|
+
Dir.glob("test/**/*_test.rb").sort.each { |f| require_relative "../#{f}" }
|
|
12
|
+
else
|
|
13
|
+
ARGV.each { |f| require_relative "../#{f}" }
|
|
14
|
+
end
|
data/examples/README.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# Examples
|
|
2
|
+
|
|
3
|
+
Each example is a complete server. From the repo root:
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
bundle install
|
|
7
|
+
bundle exec ruby examples/puma.rb # built-in router on puma
|
|
8
|
+
bundle exec ruby examples/falcon.rb # built-in router on falcon
|
|
9
|
+
bundle exec ruby examples/sinatra.rb # sinatra DSL on falcon
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
One example brings its own dependencies (via an inline gemfile) instead of
|
|
13
|
+
the repo bundle — run it with plain `ruby`:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
ruby examples/brute.rb # a brute coding agent served over HTTP
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
It expects a [brute](https://github.com/general-intelligence-systems/brute) checkout at
|
|
20
|
+
`~/brute/brute` (override with `BRUTE_PATH`) and an LLM — a local Ollama by
|
|
21
|
+
default (`BRUTE_PROVIDER` / `BRUTE_MODEL` to change). Ask it things:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
curl -d 'What files are in the current directory?' http://localhost:9292/ask
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
All of them listen on `http://127.0.0.1:9292` (override with `HOST`/`PORT`
|
|
28
|
+
env vars, or `Server.run(host:, port:)`).
|
|
29
|
+
|
|
30
|
+
## How it works
|
|
31
|
+
|
|
32
|
+
`require "ratalada/puma"` or `require "ratalada/falcon"` picks the server
|
|
33
|
+
backend and defines the top-level `Server` constant.
|
|
34
|
+
|
|
35
|
+
With the built-in router, the `Server.run` block receives each request and
|
|
36
|
+
returns a handler for it. A request pattern-matches as `[verb, path]`, and a
|
|
37
|
+
handler can be:
|
|
38
|
+
|
|
39
|
+
- a `String` — sent as a `200 text/plain` response
|
|
40
|
+
- a callable — called with the request, its result handled the same way
|
|
41
|
+
- a `[status, headers, body]` triplet — used as-is
|
|
42
|
+
- nothing (`nil` or a fall-through `case ... in`) — a `404`
|
|
43
|
+
|
|
44
|
+
```ruby
|
|
45
|
+
require "ratalada/puma"
|
|
46
|
+
|
|
47
|
+
Server.run do |request|
|
|
48
|
+
case request
|
|
49
|
+
in ["GET", "/"] then "ok"
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
`require "ratalada/sinatra"` swaps the block DSL for Sinatra: the block is
|
|
55
|
+
class-evaluated into an anonymous Sinatra app, running on whichever backend
|
|
56
|
+
you required.
|
data/examples/brute.rb
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Serve a brute agent over HTTP.
|
|
4
|
+
#
|
|
5
|
+
# curl -d 'Say hi' http://localhost:9292/ask
|
|
6
|
+
|
|
7
|
+
require "ratalada/falcon"
|
|
8
|
+
require "ratalada/sinatra"
|
|
9
|
+
require "brute"
|
|
10
|
+
require "ruby_llm"
|
|
11
|
+
|
|
12
|
+
RubyLLM.configure do |config|
|
|
13
|
+
config.ollama_api_base = "http://localhost:11434/v1"
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
Server.run do
|
|
17
|
+
get "/ask" do
|
|
18
|
+
Brute.agent.run do |env|
|
|
19
|
+
RubyLLM.chat(model: "llama3.2", provider: :ollama).then do |chat|
|
|
20
|
+
chat.ask(env[:messages].last.content).content.then do |response|
|
|
21
|
+
env[:messages].assistant(response)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end.then do |agent|
|
|
25
|
+
agent.start(request.body.read)[:messages].last.content
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
data/examples/falcon.rb
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Run: bundle exec ruby examples/falcon.rb
|
|
4
|
+
# Try: curl http://localhost:9292/
|
|
5
|
+
# curl http://localhost:9292/up
|
|
6
|
+
# curl "http://localhost:9292/greet?name=world"
|
|
7
|
+
|
|
8
|
+
require "ratalada/falcon"
|
|
9
|
+
|
|
10
|
+
Server.run do |request|
|
|
11
|
+
case request
|
|
12
|
+
in ["GET", "/"] then "hello from falcon\n"
|
|
13
|
+
in ["GET", "/up"] then "ok\n"
|
|
14
|
+
in ["GET", "/greet"] then ->(req) { "hello #{req.query.delete_prefix("name=")}\n" }
|
|
15
|
+
end
|
|
16
|
+
end
|
data/examples/puma.rb
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Run: bundle exec ruby examples/puma.rb
|
|
4
|
+
# Try: curl http://localhost:9292/
|
|
5
|
+
# curl http://localhost:9292/up
|
|
6
|
+
# curl -d "hello" http://localhost:9292/echo
|
|
7
|
+
|
|
8
|
+
require "ratalada/puma"
|
|
9
|
+
|
|
10
|
+
Server.run do |request|
|
|
11
|
+
case request
|
|
12
|
+
in ["GET", "/"] then "hello from puma\n"
|
|
13
|
+
in ["GET", "/up"] then "ok\n"
|
|
14
|
+
in ["POST", "/echo"] then ->(req) { [200, { "content-type" => "text/plain" }, req.body] }
|
|
15
|
+
end
|
|
16
|
+
end
|
data/examples/sinatra.rb
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Run: bundle exec ruby examples/sinatra.rb
|
|
4
|
+
# Try: curl http://localhost:9292/
|
|
5
|
+
# curl http://localhost:9292/up
|
|
6
|
+
# curl http://localhost:9292/greet/world
|
|
7
|
+
|
|
8
|
+
require "ratalada/falcon"
|
|
9
|
+
require "ratalada/sinatra"
|
|
10
|
+
|
|
11
|
+
Server.run do
|
|
12
|
+
get "/" do
|
|
13
|
+
"hello from sinatra on falcon\n"
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
get "/up" do
|
|
17
|
+
"ok\n"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
get "/greet/:name" do
|
|
21
|
+
"hello #{params[:name]}\n"
|
|
22
|
+
end
|
|
23
|
+
end
|
data/exe/ratalada
ADDED
data/flake.lock
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"nodes": {
|
|
3
|
+
"nixpkgs": {
|
|
4
|
+
"locked": {
|
|
5
|
+
"lastModified": 1781577229,
|
|
6
|
+
"narHash": "sha256-lrp67w8AulE9Ks53n27I45ADSzbOCn4H+CNW1Ck8B+8=",
|
|
7
|
+
"owner": "NixOS",
|
|
8
|
+
"repo": "nixpkgs",
|
|
9
|
+
"rev": "567a49d1913ce81ac6e9582e3553dd90a955875f",
|
|
10
|
+
"type": "github"
|
|
11
|
+
},
|
|
12
|
+
"original": {
|
|
13
|
+
"owner": "NixOS",
|
|
14
|
+
"ref": "nixos-unstable",
|
|
15
|
+
"repo": "nixpkgs",
|
|
16
|
+
"type": "github"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"root": {
|
|
20
|
+
"inputs": {
|
|
21
|
+
"nixpkgs": "nixpkgs",
|
|
22
|
+
"utils": "utils"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"systems": {
|
|
26
|
+
"locked": {
|
|
27
|
+
"lastModified": 1681028828,
|
|
28
|
+
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
|
|
29
|
+
"owner": "nix-systems",
|
|
30
|
+
"repo": "default",
|
|
31
|
+
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
|
|
32
|
+
"type": "github"
|
|
33
|
+
},
|
|
34
|
+
"original": {
|
|
35
|
+
"owner": "nix-systems",
|
|
36
|
+
"repo": "default",
|
|
37
|
+
"type": "github"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"utils": {
|
|
41
|
+
"inputs": {
|
|
42
|
+
"systems": "systems"
|
|
43
|
+
},
|
|
44
|
+
"locked": {
|
|
45
|
+
"lastModified": 1731533236,
|
|
46
|
+
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
|
|
47
|
+
"owner": "numtide",
|
|
48
|
+
"repo": "flake-utils",
|
|
49
|
+
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
|
|
50
|
+
"type": "github"
|
|
51
|
+
},
|
|
52
|
+
"original": {
|
|
53
|
+
"owner": "numtide",
|
|
54
|
+
"repo": "flake-utils",
|
|
55
|
+
"type": "github"
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
"root": "root",
|
|
60
|
+
"version": 7
|
|
61
|
+
}
|
data/flake.nix
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
description = "Ruby gem flake";
|
|
3
|
+
|
|
4
|
+
inputs = {
|
|
5
|
+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
|
6
|
+
utils.url = "github:numtide/flake-utils";
|
|
7
|
+
};
|
|
8
|
+
outputs = { self, nixpkgs, utils }:
|
|
9
|
+
utils.lib.eachDefaultSystem (system:
|
|
10
|
+
let
|
|
11
|
+
pkgs = nixpkgs.legacyPackages.${system};
|
|
12
|
+
ruby = pkgs.ruby_3_4; # Specify version
|
|
13
|
+
in
|
|
14
|
+
{
|
|
15
|
+
devShells.default = pkgs.mkShell {
|
|
16
|
+
nativeBuildInputs = [
|
|
17
|
+
pkgs.pkg-config # native extension discovery
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
buildInputs = [
|
|
21
|
+
ruby
|
|
22
|
+
pkgs.libyaml # psych gem
|
|
23
|
+
pkgs.openssl # openssl gem
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
shellHook = ''
|
|
27
|
+
export GEM_HOME="$HOME/.gem-${ruby.version}"
|
|
28
|
+
export GEM_PATH="$GEM_HOME"
|
|
29
|
+
export PATH="$GEM_HOME/bin:$PATH"
|
|
30
|
+
export BUNDLE_GEMFILE="$PWD/Gemfile"
|
|
31
|
+
export BUNDLE_PATH="$GEM_HOME"
|
|
32
|
+
export BUNDLE_BIN="$GEM_HOME/bin"
|
|
33
|
+
'';
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "falcon"
|
|
4
|
+
require "falcon/environment/server"
|
|
5
|
+
require "async/service/configuration"
|
|
6
|
+
require "async/service/controller"
|
|
7
|
+
require "async/container"
|
|
8
|
+
require_relative "../ratalada"
|
|
9
|
+
|
|
10
|
+
module Ratalada
|
|
11
|
+
module Backends
|
|
12
|
+
module Falcon
|
|
13
|
+
module_function
|
|
14
|
+
|
|
15
|
+
# Runs the app the way `falcon host` does: declare a server environment,
|
|
16
|
+
# hand it to Async::Service::Controller. The controller binds the socket
|
|
17
|
+
# once in the parent, runs `count` supervised workers accepting from it
|
|
18
|
+
# (restarts, health checks, INT/TERM/HUP handling all come with it).
|
|
19
|
+
def run(app, host:, port:, count: 1)
|
|
20
|
+
middleware = ::Falcon::Server.middleware(app)
|
|
21
|
+
|
|
22
|
+
environment = Async::Service::Environment.new(::Falcon::Environment::Server).with(
|
|
23
|
+
name: "ratalada",
|
|
24
|
+
url: "http://#{host}:#{port}",
|
|
25
|
+
middleware: -> { middleware },
|
|
26
|
+
container_options: { count: count, restart: true },
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
configuration = Async::Service::Configuration.new
|
|
30
|
+
configuration.add(environment)
|
|
31
|
+
|
|
32
|
+
warn "ratalada: falcon listening on http://#{host}:#{port}#{" (#{count} workers)" if count > 1}"
|
|
33
|
+
Async::Service::Controller.run(configuration, container_class: Async::Container.best_container_class)
|
|
34
|
+
rescue Interrupt
|
|
35
|
+
# clean shutdown
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
self.backend = Backends::Falcon
|
|
41
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "puma"
|
|
4
|
+
require_relative "../ratalada"
|
|
5
|
+
|
|
6
|
+
module Ratalada
|
|
7
|
+
module Backends
|
|
8
|
+
module Puma
|
|
9
|
+
module_function
|
|
10
|
+
|
|
11
|
+
def run(app, host:, port:, count: 1)
|
|
12
|
+
warn "ratalada: puma backend ignores count: (not yet implemented)" if count > 1
|
|
13
|
+
server = ::Puma::Server.new(app)
|
|
14
|
+
server.add_tcp_listener(host, port)
|
|
15
|
+
warn "ratalada: puma listening on http://#{host}:#{port}"
|
|
16
|
+
server.run.join
|
|
17
|
+
rescue Interrupt
|
|
18
|
+
server.stop(true)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
self.backend = Backends::Puma
|
|
24
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "sinatra/base"
|
|
4
|
+
require_relative "../ratalada"
|
|
5
|
+
|
|
6
|
+
module Ratalada
|
|
7
|
+
module Frontends
|
|
8
|
+
# Sinatra-flavoured DSL: the Server.run block is class-evaluated into an
|
|
9
|
+
# anonymous Sinatra application, so `get "/" do ... end` etc. all work.
|
|
10
|
+
module Sinatra
|
|
11
|
+
def self.build(block)
|
|
12
|
+
app = Class.new(::Sinatra::Base)
|
|
13
|
+
app.class_eval(&block)
|
|
14
|
+
app.new
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
self.frontend = Frontends::Sinatra
|
|
20
|
+
end
|
data/lib/ratalada.rb
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "ratalada/version"
|
|
4
|
+
|
|
5
|
+
module Ratalada
|
|
6
|
+
DEFAULT_HOST = ENV.fetch("HOST", "127.0.0.1")
|
|
7
|
+
DEFAULT_PORT = Integer(ENV.fetch("PORT", "9292"))
|
|
8
|
+
DEFAULT_COUNT = Integer(ENV.fetch("COUNT", "1"))
|
|
9
|
+
|
|
10
|
+
class Error < StandardError; end
|
|
11
|
+
|
|
12
|
+
class NoBackendError < Error
|
|
13
|
+
def initialize(msg = nil)
|
|
14
|
+
super(msg || <<~MSG)
|
|
15
|
+
No server backend selected. Require one before calling Server.run:
|
|
16
|
+
|
|
17
|
+
require "ratalada/puma" # or
|
|
18
|
+
require "ratalada/falcon"
|
|
19
|
+
MSG
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
class << self
|
|
24
|
+
# The backend runs a rack app (set by requiring ratalada/puma or
|
|
25
|
+
# ratalada/falcon); the frontend turns the Server.run block into a rack
|
|
26
|
+
# app (the built-in router by default, ratalada/sinatra to swap it).
|
|
27
|
+
attr_writer :backend, :frontend
|
|
28
|
+
|
|
29
|
+
def backend
|
|
30
|
+
@backend or raise NoBackendError
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def frontend
|
|
34
|
+
@frontend ||= Frontends::Routes
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Wraps the Rack env with just enough sugar to pattern match on.
|
|
39
|
+
class Request
|
|
40
|
+
attr_reader :env
|
|
41
|
+
|
|
42
|
+
def initialize(env)
|
|
43
|
+
@env = env
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def verb = env["REQUEST_METHOD"]
|
|
47
|
+
def path = env["PATH_INFO"]
|
|
48
|
+
def query = env["QUERY_STRING"]
|
|
49
|
+
def body = @body ||= env["rack.input"]&.read
|
|
50
|
+
|
|
51
|
+
# Enables `in ["GET", "/"]`
|
|
52
|
+
def deconstruct = [verb, path]
|
|
53
|
+
|
|
54
|
+
# Enables `in {verb: "GET", path: "/users"}`
|
|
55
|
+
def deconstruct_keys(_keys) = { verb:, path:, query:, env: }
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
module Frontends
|
|
59
|
+
# The default DSL: the Server.run block is a router. It is called with
|
|
60
|
+
# each Request and returns a handler for it — a callable (called with the
|
|
61
|
+
# request), a response body String, or a full [status, headers, body]
|
|
62
|
+
# triplet. No match (nil, or a fall-through `case ... in`) means 404.
|
|
63
|
+
module Routes
|
|
64
|
+
def self.build(block)
|
|
65
|
+
App.new(block)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
class App
|
|
69
|
+
def initialize(router)
|
|
70
|
+
@router = router
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def call(env)
|
|
74
|
+
request = Request.new(env)
|
|
75
|
+
handler = begin
|
|
76
|
+
@router.call(request)
|
|
77
|
+
rescue NoMatchingPatternError
|
|
78
|
+
nil
|
|
79
|
+
end
|
|
80
|
+
respond(handler, request)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
private
|
|
84
|
+
|
|
85
|
+
def respond(handler, request)
|
|
86
|
+
case handler
|
|
87
|
+
when nil
|
|
88
|
+
[404, { "content-type" => "text/plain" }, ["not found"]]
|
|
89
|
+
when Proc, Method
|
|
90
|
+
respond(handler.call(request), request)
|
|
91
|
+
when String
|
|
92
|
+
[200, { "content-type" => "text/plain" }, [handler]]
|
|
93
|
+
when Array
|
|
94
|
+
status, headers, body = handler
|
|
95
|
+
[status, headers, body.is_a?(String) ? [body] : body]
|
|
96
|
+
else
|
|
97
|
+
handler
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
module Server
|
|
105
|
+
module_function
|
|
106
|
+
|
|
107
|
+
# count runs that many worker processes accepting from a shared socket,
|
|
108
|
+
# like node's cluster module. Each worker has its own state — anything
|
|
109
|
+
# shared (sessions, caches) needs an external store or count: 1.
|
|
110
|
+
def run(host: DEFAULT_HOST, port: DEFAULT_PORT, count: DEFAULT_COUNT, &block)
|
|
111
|
+
raise ArgumentError, "Server.run requires a block" unless block
|
|
112
|
+
raise ArgumentError, "count must be a positive Integer" unless count.is_a?(Integer) && count.positive?
|
|
113
|
+
|
|
114
|
+
app = Ratalada.frontend.build(block)
|
|
115
|
+
Ratalada.backend.run(app, host: host, port: port, count: count)
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# The whole point is a zero-ceremony top-level DSL.
|
|
121
|
+
Server = Ratalada::Server unless defined?(Server)
|
data/ratalada.gemspec
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "lib/ratalada/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = "ratalada"
|
|
7
|
+
spec.version = Ratalada::VERSION
|
|
8
|
+
spec.authors = ["Nathan K"]
|
|
9
|
+
spec.email = ["nathankidd@hey.com"]
|
|
10
|
+
|
|
11
|
+
spec.summary = "DSL for running rack servers as easily as you can in javascript."
|
|
12
|
+
|
|
13
|
+
spec.description = <<~DESC
|
|
14
|
+
DSL for running rack servers as easily as you can in javasript.
|
|
15
|
+
DESC
|
|
16
|
+
|
|
17
|
+
spec.homepage = "https://github.com/n-at-han-k/ratalada"
|
|
18
|
+
spec.license = "MIT"
|
|
19
|
+
spec.required_ruby_version = ">= 3.2.0"
|
|
20
|
+
|
|
21
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
22
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
|
23
|
+
spec.metadata["documentation_uri"] = spec.homepage
|
|
24
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
|
25
|
+
|
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|data|docs|\.github)/}) }
|
|
27
|
+
spec.bindir = "exe"
|
|
28
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
29
|
+
spec.require_paths = ["lib"]
|
|
30
|
+
|
|
31
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
|
32
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
|
33
|
+
|
|
34
|
+
# Servers and frameworks the adapters wrap; users install whichever they require.
|
|
35
|
+
spec.add_development_dependency "falcon"
|
|
36
|
+
spec.add_development_dependency "puma"
|
|
37
|
+
spec.add_development_dependency "sinatra"
|
|
38
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "lib/ratalada/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = "<%= gem_name %>"
|
|
7
|
+
spec.version = Ratalada::VERSION
|
|
8
|
+
spec.authors = [<%= authors.map { |a| %("#{a}") }.join(", ") %>]
|
|
9
|
+
spec.email = [<%= emails.map { |e| %("#{e}") }.join(", ") %>]
|
|
10
|
+
|
|
11
|
+
spec.summary = "<%= summary %>"
|
|
12
|
+
|
|
13
|
+
spec.description = <<~DESC
|
|
14
|
+
<%= description %>
|
|
15
|
+
DESC
|
|
16
|
+
|
|
17
|
+
spec.homepage = "<%= homepage %>"
|
|
18
|
+
spec.license = "<%= license %>"
|
|
19
|
+
spec.required_ruby_version = ">= <%= ruby_version %>"
|
|
20
|
+
|
|
21
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
22
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
|
23
|
+
spec.metadata["documentation_uri"] = spec.homepage
|
|
24
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
|
25
|
+
|
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|data)/}) }
|
|
27
|
+
spec.bindir = "exe"
|
|
28
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
29
|
+
spec.require_paths = ["lib"]
|
|
30
|
+
|
|
31
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
|
32
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
|
33
|
+
spec.add_development_dependency "rubocop", "~> 1.21"
|
|
34
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: ratalada
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.2.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Nathan K
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-01 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: minitest
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '5.0'
|
|
19
|
+
type: :development
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '5.0'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: rake
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - "~>"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '13.0'
|
|
33
|
+
type: :development
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '13.0'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: falcon
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0'
|
|
47
|
+
type: :development
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
54
|
+
- !ruby/object:Gem::Dependency
|
|
55
|
+
name: puma
|
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - ">="
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '0'
|
|
61
|
+
type: :development
|
|
62
|
+
prerelease: false
|
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '0'
|
|
68
|
+
- !ruby/object:Gem::Dependency
|
|
69
|
+
name: sinatra
|
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - ">="
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '0'
|
|
75
|
+
type: :development
|
|
76
|
+
prerelease: false
|
|
77
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
78
|
+
requirements:
|
|
79
|
+
- - ">="
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '0'
|
|
82
|
+
description: 'DSL for running rack servers as easily as you can in javasript.
|
|
83
|
+
|
|
84
|
+
'
|
|
85
|
+
email:
|
|
86
|
+
- nathankidd@hey.com
|
|
87
|
+
executables:
|
|
88
|
+
- ratalada
|
|
89
|
+
extensions: []
|
|
90
|
+
extra_rdoc_files: []
|
|
91
|
+
files:
|
|
92
|
+
- ".envrc"
|
|
93
|
+
- ".gitignore"
|
|
94
|
+
- Gemfile
|
|
95
|
+
- Gemfile.lock
|
|
96
|
+
- LICENSE
|
|
97
|
+
- README.md
|
|
98
|
+
- Rakefile
|
|
99
|
+
- bin/console
|
|
100
|
+
- bin/increment-version
|
|
101
|
+
- bin/release-gem
|
|
102
|
+
- bin/setup
|
|
103
|
+
- bin/tag-version
|
|
104
|
+
- bin/test
|
|
105
|
+
- examples/README.md
|
|
106
|
+
- examples/brute.rb
|
|
107
|
+
- examples/falcon.rb
|
|
108
|
+
- examples/puma.rb
|
|
109
|
+
- examples/sinatra.rb
|
|
110
|
+
- exe/ratalada
|
|
111
|
+
- flake.lock
|
|
112
|
+
- flake.nix
|
|
113
|
+
- lib/ratalada.rb
|
|
114
|
+
- lib/ratalada/falcon.rb
|
|
115
|
+
- lib/ratalada/puma.rb
|
|
116
|
+
- lib/ratalada/sinatra.rb
|
|
117
|
+
- lib/ratalada/version.rb
|
|
118
|
+
- lib/ratalada/version.rb.erb
|
|
119
|
+
- ratalada.gemspec
|
|
120
|
+
- ratalada.gemspec.erb
|
|
121
|
+
homepage: https://github.com/n-at-han-k/ratalada
|
|
122
|
+
licenses:
|
|
123
|
+
- MIT
|
|
124
|
+
metadata:
|
|
125
|
+
homepage_uri: https://github.com/n-at-han-k/ratalada
|
|
126
|
+
source_code_uri: https://github.com/n-at-han-k/ratalada
|
|
127
|
+
documentation_uri: https://github.com/n-at-han-k/ratalada
|
|
128
|
+
rubygems_mfa_required: 'true'
|
|
129
|
+
rdoc_options: []
|
|
130
|
+
require_paths:
|
|
131
|
+
- lib
|
|
132
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
133
|
+
requirements:
|
|
134
|
+
- - ">="
|
|
135
|
+
- !ruby/object:Gem::Version
|
|
136
|
+
version: 3.2.0
|
|
137
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
138
|
+
requirements:
|
|
139
|
+
- - ">="
|
|
140
|
+
- !ruby/object:Gem::Version
|
|
141
|
+
version: '0'
|
|
142
|
+
requirements: []
|
|
143
|
+
rubygems_version: 3.7.2
|
|
144
|
+
specification_version: 4
|
|
145
|
+
summary: DSL for running rack servers as easily as you can in javascript.
|
|
146
|
+
test_files: []
|