tsurezure 0.0.1 → 0.0.2
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 +4 -4
- data/lib/tsurezure.rb +29 -6
- data/readme.md +28 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1dd0633006bc7ba5837c37817e78d1f7a1fdc38520557c10a528fd40acff2573
|
4
|
+
data.tar.gz: 420c2cc0bd0387c24cbe5d12b40aad574edd1e4e0f2a4bbf6b39eeb323ccdaec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 358fbcfcd4504ea64c5afad8b80f5600a4bab693e48bcf536700315ce6766b54988bf4b25e8e6aeeb71facf7773515bdf0bb0bf4c2c1fc27687f984562af0ad1
|
7
|
+
data.tar.gz: 6a581bcabeb9556e2649c6893b9cfdf7553e27726f86446e35feaaba48f19126b124f0019ad45090d095704102a00e3b5834908e3ab045c70c1d11913a021636
|
data/lib/tsurezure.rb
CHANGED
@@ -12,6 +12,7 @@ require_relative 'utils/response' # handles request and generates responses.
|
|
12
12
|
|
13
13
|
$TRZR_PROCESS_MODE = nil
|
14
14
|
$TRZR_LOG = true
|
15
|
+
TRZR_STARTED_AT = Time.now.to_i
|
15
16
|
|
16
17
|
ARGV.each do |arg|
|
17
18
|
$TRZR_PROCESS_MODE = 'development' if arg == '--development'
|
@@ -205,8 +206,14 @@ class Tsurezure
|
|
205
206
|
|
206
207
|
##
|
207
208
|
# run when the server is prepared to accept requests.
|
208
|
-
def listen
|
209
|
-
|
209
|
+
def listen(callback = nil)
|
210
|
+
if $TRZR_PROCESS_MODE == 'development'
|
211
|
+
puts "[trzr_dev] running on port #{@port}!"
|
212
|
+
end
|
213
|
+
|
214
|
+
# call the callback if there's one provided
|
215
|
+
callback.call server_opts if callback.is_a? Proc
|
216
|
+
|
210
217
|
# create a new thread for handle each incoming request
|
211
218
|
loop do
|
212
219
|
Thread.start(@server.accept) do |client|
|
@@ -215,8 +222,20 @@ class Tsurezure
|
|
215
222
|
end
|
216
223
|
end
|
217
224
|
|
225
|
+
def kill
|
226
|
+
abort
|
227
|
+
end
|
228
|
+
|
218
229
|
private
|
219
230
|
|
231
|
+
def server_opts
|
232
|
+
{
|
233
|
+
port: @port,
|
234
|
+
endpoints: @endpoints,
|
235
|
+
middleware: @middleware
|
236
|
+
}
|
237
|
+
end
|
238
|
+
|
220
239
|
# ----------------------------------------
|
221
240
|
# :section: registration of endpoints and
|
222
241
|
# all endpoint management methods follow.
|
@@ -272,10 +291,14 @@ class Tsurezure
|
|
272
291
|
# add endpoint to list of registered endpoints
|
273
292
|
@endpoints[method][endpoint[:path]] = endpoint
|
274
293
|
end
|
294
|
+
end
|
275
295
|
|
276
|
-
|
277
|
-
|
296
|
+
at_exit do
|
297
|
+
if $TRZR_PROCESS_MODE == 'development' && $TRZR_LOG.true?
|
298
|
+
time = Time.now.to_i - TRZR_STARTED_AT
|
299
|
+
puts
|
300
|
+
puts '[trzr_dev] shutting down. goodbye...'
|
301
|
+
puts "[trzr_dev] shut down after #{Time.at(time).utc.strftime('%H:%M:%S')}."
|
302
|
+
puts
|
278
303
|
end
|
279
304
|
end
|
280
|
-
|
281
|
-
at_exit { puts 'shutting down. goodbye...' }
|
data/readme.md
CHANGED
@@ -23,9 +23,17 @@ after cloning this repo, from the root project directory, just run `rake start`
|
|
23
23
|
|
24
24
|
to build the gem: run `gem build tsurezure.gemspec`. then, install using `gem install tsurezure-version-number`. `version-number` is whatever version is installed based on the `.gemspec` file.
|
25
25
|
|
26
|
+
### commands
|
27
|
+
|
28
|
+
- `rake install` will install dependencies
|
29
|
+
- `rake check_deps` will install dependencies if not installed
|
30
|
+
- `rake start` will run the server in production mode
|
31
|
+
- `rake dev` will run the server in development mode
|
32
|
+
- `rake dev_silent` will run the server in development mode with no logs
|
33
|
+
|
26
34
|
### actually using tsurezure:
|
27
35
|
|
28
|
-
as for how to use tsurezure, here's a simple
|
36
|
+
as for how to use tsurezure, here's a simple script to get started:
|
29
37
|
|
30
38
|
```ruby
|
31
39
|
require 'tsurezure'
|
@@ -78,6 +86,24 @@ after you run this file, open up your browser or whatever and go to `http://loca
|
|
78
86
|
}
|
79
87
|
```
|
80
88
|
|
89
|
+
the `listen` method can be called with no arguments to just start the server. you can also pass in a lambda or proc that will run when the server has started. the only argument that will be passed to that proc is a hash called `server_opts`. it contains some information about the current configuration:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
{
|
93
|
+
port, # port that tsurezure is running on
|
94
|
+
endpoints, # endpoints object containing the endpoints you've added
|
95
|
+
middleware # middleware object containing the middleware you've added
|
96
|
+
}
|
97
|
+
```
|
98
|
+
|
99
|
+
simple example of usage:
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
server.listen lambda { |opts|
|
103
|
+
puts "listening on port #{opts[:port]}!"
|
104
|
+
}
|
105
|
+
```
|
106
|
+
|
81
107
|
the registration function for creating endpoints is very simple:
|
82
108
|
|
83
109
|
```ruby
|
@@ -92,7 +118,7 @@ register http_method, path, callback, options
|
|
92
118
|
|
93
119
|
`options` is a hash containing various options to somehow modify the response. valid options:
|
94
120
|
|
95
|
-
- `content_type` - determines the mime type of the response
|
121
|
+
- `content_type (default: text/plain)` - determines the mime type of the response
|
96
122
|
- `location` - if a location header is required (301, etc), this is used to provide it.
|
97
123
|
- `method` - if an allow header is required (405), this is used to provide it.
|
98
124
|
|