tsurezure 0.0.3 → 0.0.31
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/tsurezure.rb +18 -0
- data/readme.md +150 -149
- 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: a3f470685236ef86ac858933ef4c913a99f21a4e0d42572971518781056c68f1
|
4
|
+
data.tar.gz: b6aede8db5d91dc1be40ddca3fc5011e1494084eb5e38f7df30edd8a426a316d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 73dc499f770f090d7c5ad90ebfd3cf59922c105e84bb4a65713694b0d802c79ecc1e1a36191de3dd6e90984df689319bc4d3cdfb9e89de3e87891448bf543c91
|
7
|
+
data.tar.gz: 4e9f7c5e21fa6b3f9d422c21026a9bf07a1c02269fad26c1a16f36f155bb8a17f48c045a22b637fdd3ed31e6f0dde8b4b70d11449c0ff6b0fc7a0eadd8cd091f
|
data/lib/tsurezure.rb
CHANGED
@@ -14,6 +14,9 @@ $TRZR_PROCESS_MODE = nil
|
|
14
14
|
$TRZR_LOG = true
|
15
15
|
TRZR_STARTED_AT = Time.now.to_i
|
16
16
|
|
17
|
+
INVALID_RESPONSE_FORMAT = "if responding from a middleware, \
|
18
|
+
you must return a hash that includes a :message property."
|
19
|
+
|
17
20
|
ARGV.each do |arg|
|
18
21
|
$TRZR_PROCESS_MODE = 'development' if arg == '--development'
|
19
22
|
$TRZR_PROCESS_MODE = 'production' if arg == '--production'
|
@@ -103,9 +106,24 @@ class Tsurezure
|
|
103
106
|
request
|
104
107
|
end
|
105
108
|
|
109
|
+
def respond_with_error(error)
|
110
|
+
Logbook::Dev.log(error)
|
111
|
+
|
112
|
+
message = { error: error }.to_json
|
113
|
+
|
114
|
+
responder = HTTPUtils::ServerResponse.new(
|
115
|
+
@session,
|
116
|
+
message.bytesize
|
117
|
+
)
|
118
|
+
|
119
|
+
responder.respond message, {}, 500, 'application/json'
|
120
|
+
end
|
121
|
+
|
106
122
|
def send_middleware_response(req, resp, type)
|
107
123
|
res = resp.merge req
|
108
124
|
|
125
|
+
return respond_with_error INVALID_RESPONSE_FORMAT if res[:message].nil?
|
126
|
+
|
109
127
|
responder = HTTPUtils::ServerResponse.new(
|
110
128
|
@session,
|
111
129
|
res[:message].bytesize
|
data/readme.md
CHANGED
@@ -1,149 +1,150 @@
|
|
1
|
-
# tsurezure
|
2
|
-
|
3
|
-
this is a simple web server framework written in ruby. mainly made as a way for me to quickly put together rest apis in my favorite language.
|
4
|
-
|
5
|
-
it can be used in a very similar manner to the javascript framework express.
|
6
|
-
|
7
|
-
* * *
|
8
|
-
|
9
|
-
## usage
|
10
|
-
|
11
|
-
### installing (from rubygems)
|
12
|
-
|
13
|
-
just run `gem install tsurezure` and you'll have whatever the latest version is that I've put up.
|
14
|
-
|
15
|
-
### installing (from source):
|
16
|
-
|
17
|
-
requires:
|
18
|
-
|
19
|
-
- ruby
|
20
|
-
- nodejs + nodemon (**only** for hot reloading server in development mode, not necessarily required)
|
21
|
-
|
22
|
-
after cloning this repo, from the root project directory, just run `rake start` to start in production mode, or `rake dev` to run in development mode, which adds hot reloading with nodemon. gem dependencies will install automatically.
|
23
|
-
|
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
|
-
|
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
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
}
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
#
|
66
|
-
#
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
}
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
- `
|
123
|
-
- `
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
1
|
+
# tsurezure
|
2
|
+
|
3
|
+
this is a simple web server framework written in ruby. mainly made as a way for me to quickly put together rest apis in my favorite language.
|
4
|
+
|
5
|
+
it can be used in a very similar manner to the javascript framework express.
|
6
|
+
|
7
|
+
* * *
|
8
|
+
|
9
|
+
## usage
|
10
|
+
|
11
|
+
### installing (from rubygems)
|
12
|
+
|
13
|
+
just run `gem install tsurezure` and you'll have whatever the latest version is that I've put up.
|
14
|
+
|
15
|
+
### installing (from source):
|
16
|
+
|
17
|
+
requires:
|
18
|
+
|
19
|
+
- ruby
|
20
|
+
- nodejs + nodemon (**only** for hot reloading server in development mode, not necessarily required)
|
21
|
+
|
22
|
+
after cloning this repo, from the root project directory, just run `rake start` to start in production mode, or `rake dev` to run in development mode, which adds hot reloading with nodemon. gem dependencies will install automatically.
|
23
|
+
|
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
|
+
|
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
|
+
- `rake build` will build a `.gem` file based on `tsurezure.gemspec`
|
34
|
+
|
35
|
+
### actually using tsurezure:
|
36
|
+
|
37
|
+
as for how to use tsurezure, here's a simple script to get started:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
require 'tsurezure'
|
41
|
+
|
42
|
+
# create an instance of tsurezure
|
43
|
+
server = Tsurezure.new(8888)
|
44
|
+
|
45
|
+
# url: http://localhost:8888/user/1
|
46
|
+
|
47
|
+
# create an endpoint
|
48
|
+
server.register 'get', '/user/:id', lambda { |req|
|
49
|
+
url_vars = req[:vars] # { "id" => "1" }
|
50
|
+
params = req[:params] # {}
|
51
|
+
|
52
|
+
# create a respsonse for the endpoint
|
53
|
+
{
|
54
|
+
status: 200,
|
55
|
+
message: {
|
56
|
+
message: "hello user ##{url_vars['id']}!"
|
57
|
+
}.to_json
|
58
|
+
}
|
59
|
+
}, content_type: 'application/json' # options hash
|
60
|
+
|
61
|
+
# throw in some middleware
|
62
|
+
server.add_middleware '/user/:id', lambda { |req|
|
63
|
+
url_vars = req[:vars]
|
64
|
+
|
65
|
+
# show a different response based on the request itself.
|
66
|
+
# if you return from middleware, the return value will
|
67
|
+
# be sent as the final response.
|
68
|
+
if req[:vars]['id'] == '1'
|
69
|
+
return {
|
70
|
+
status: 200, message: {
|
71
|
+
message: "hey user #1! you're the first one here!"
|
72
|
+
}.to_json
|
73
|
+
}
|
74
|
+
|
75
|
+
end
|
76
|
+
}, content_type: 'application/json'
|
77
|
+
|
78
|
+
#listen for connections
|
79
|
+
server.listen
|
80
|
+
```
|
81
|
+
|
82
|
+
after you run this file, open up your browser or whatever and go to `http://localhost:8888/user/1`. you should see a json response that looks like this:
|
83
|
+
|
84
|
+
```json
|
85
|
+
{
|
86
|
+
"message": "hey user #1! you're the first one here!"
|
87
|
+
}
|
88
|
+
```
|
89
|
+
|
90
|
+
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:
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
{
|
94
|
+
port, # port that tsurezure is running on
|
95
|
+
endpoints, # endpoints object containing the endpoints you've added
|
96
|
+
middleware # middleware object containing the middleware you've added
|
97
|
+
}
|
98
|
+
```
|
99
|
+
|
100
|
+
simple example of usage:
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
server.listen lambda { |opts|
|
104
|
+
puts "listening on port #{opts[:port]}!"
|
105
|
+
}
|
106
|
+
```
|
107
|
+
|
108
|
+
the registration function for creating endpoints is very simple:
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
register http_method, path, callback, options
|
112
|
+
```
|
113
|
+
|
114
|
+
`http_method` is the method to access the endpoint with. `path` is just the url.
|
115
|
+
|
116
|
+
`path` can be a path that contains variables (such as `/user/:id`). see the example above to see how it works.
|
117
|
+
|
118
|
+
`callback` is a lambda that contains the logic used to send a response. it will recieve one argument: the request that was sent to that endpoint. whatever is returned from the proc will be sent as the response from that endpoint.
|
119
|
+
|
120
|
+
`options` is a hash containing various options to somehow modify the response. valid options:
|
121
|
+
|
122
|
+
- `content_type (default: text/plain)` - determines the mime type of the response
|
123
|
+
- `location` - if a location header is required (301, etc), this is used to provide it.
|
124
|
+
- `method` - if an allow header is required (405), this is used to provide it.
|
125
|
+
|
126
|
+
for middleware, it's much the same:
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
add_middleware path, callback, options
|
130
|
+
```
|
131
|
+
|
132
|
+
`path` can be a path that contains variables. used in the same way as the `path` for endpoints.
|
133
|
+
|
134
|
+
`callback` is a lambda that you can use to intercept and pre-process responses. if you return from a callback in middleware, then that return value will be sent as the final response.
|
135
|
+
|
136
|
+
`options` for middleware are the same as the `options` for endpoints.
|
137
|
+
|
138
|
+
* * *
|
139
|
+
|
140
|
+
## todo
|
141
|
+
|
142
|
+
- [ ] make it so registered uris can only be accessed with the specified method, and everything else returns a 405 (maybe make this an option??)
|
143
|
+
|
144
|
+
- [ ] give the user an option to add middleware specifically for catching errors
|
145
|
+
|
146
|
+
## misc
|
147
|
+
|
148
|
+
disclaimer: I don't know ruby, and this is my first time using it to make something.
|
149
|
+
|
150
|
+
the name comes from yukueshirezutsurezure, one of my favorite bands. it's pronounced 'tsɯ-ɾe-dzɯ-ɾe.'
|