simplepub 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/CHANGELOG.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/LICENSE.txt +22 -0
- data/README.md +146 -0
- data/Rakefile +8 -0
- data/app/assets/javascripts/simplepub.js +97 -0
- data/bin/simplepub +5 -0
- data/lib/generators/simplepub/install_generator.rb +17 -0
- data/lib/simplepub/cli/app.rb +13 -0
- data/lib/simplepub/cli/config.rb +33 -0
- data/lib/simplepub/cli/server.rb +64 -0
- data/lib/simplepub/cli.rb +7 -0
- data/lib/simplepub/configuration.rb +48 -0
- data/lib/simplepub/engine.rb +16 -0
- data/lib/simplepub/error.rb +7 -0
- data/lib/simplepub/faye_extension.rb +43 -0
- data/lib/simplepub/interface_methods.rb +61 -0
- data/lib/simplepub/rack_config.ru +10 -0
- data/lib/simplepub/version.rb +3 -0
- data/lib/simplepub/view_helpers.rb +22 -0
- data/lib/simplepub.rb +24 -0
- data/lib/templates/simplepub.ru +10 -0
- data/lib/templates/simplepub.yml +10 -0
- data/simplepub.gemspec +29 -0
- data/spec/fixtures/simplepub.yml +8 -0
- data/spec/javascripts/helpers/SpecHelper.js +15 -0
- data/spec/javascripts/simplepub_spec.js +165 -0
- data/spec/javascripts/support/jasmine.yml +76 -0
- data/spec/javascripts/support/jasmine_config.rb +23 -0
- data/spec/javascripts/support/jasmine_helper.rb +11 -0
- data/spec/javascripts/support/jasmine_runner.rb +32 -0
- data/spec/simplepub/faye_extension_spec.rb +67 -0
- data/spec/simplepub_spec.rb +148 -0
- data/spec/spec_helper.rb +7 -0
- metadata +208 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 621c275bc4cd10a8abfafb4311ffe9a0ccd7a424
|
4
|
+
data.tar.gz: 72bfe967a904824287a8c9fe3da8e56932087db2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fc96c8f2ef50018eb30d6e383008292f7c8c158b1b8923186e7b0df7c11d5886eb090f608d78467cf49a34a18920c6bb804fa630c252cf6b9bfffb0b81b32a23
|
7
|
+
data.tar.gz: bc9122517ca3a60fa88018d3881e2c66a60e950e82f434f4fd823afb846a1ef3cc1384a6caba1f484fa5153cc28f18d7d918e6f57acbd84790c2b4e3a95fb11c
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
## 1.0.3 (August 20, 2012)
|
2
|
+
|
3
|
+
* fixed Faye startup error (thanks gitt) - issue #40
|
4
|
+
|
5
|
+
|
6
|
+
## 1.0.2 (August 20, 2012)
|
7
|
+
|
8
|
+
* added HTTPS support (thanks vanne)
|
9
|
+
|
10
|
+
|
11
|
+
## 1.0.1 (January 25, 2012)
|
12
|
+
|
13
|
+
* Rails 3.2 compatibility with SecureRandom fix (thanks windigo) - issue #26
|
14
|
+
|
15
|
+
|
16
|
+
## 1.0.0 (January 15, 2012)
|
17
|
+
|
18
|
+
* setting config defaults to nil so everything must be set in `simplepub.yml`
|
19
|
+
|
20
|
+
* Documentation improvements
|
21
|
+
|
22
|
+
|
23
|
+
## 0.3.0 (January 14, 2012)
|
24
|
+
|
25
|
+
* adding `Simplepub.publish_to` method for publishing from anywhere - issue #15
|
26
|
+
|
27
|
+
* rewriting `simplepub.js` so it is framework agnostic
|
28
|
+
|
29
|
+
* Rails 3.1 compatibility (thanks BinaryMuse) - issue #25
|
30
|
+
|
31
|
+
* adding faye gem dependency so it doesn't need to be installed separately
|
32
|
+
|
33
|
+
* renaming `faye.ru` to `simplepub.ru`
|
34
|
+
|
35
|
+
* truncate token for client for security (thanks jameshuynh) - issue #19
|
36
|
+
|
37
|
+
|
38
|
+
## 0.2.0 (April 7, 2011)
|
39
|
+
|
40
|
+
* switched to YAML file for config. BACKWARDS INCOMPATIBLE: you will need to remove config/initializers/simplepub.rb
|
41
|
+
|
42
|
+
* moved view helpers into Railtie so helper file is no longer generated
|
43
|
+
|
44
|
+
* error out when signature has expired
|
45
|
+
|
46
|
+
|
47
|
+
## 0.1.0 (April 4, 2011)
|
48
|
+
|
49
|
+
* initial release
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Ryan Bates
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jorge Calás Lozano
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
# Simplepub
|
2
|
+
|
3
|
+
Simplepub is a Ruby gem for use with Rails to publish and subscribe to messages through [Faye](http://faye.jcoglan.com/). It allows you to easily provide real-time updates through an open socket without tying up a Rails process. All channels are private so users can only listen to events you subscribe them to.
|
4
|
+
|
5
|
+
Watch [RailsCasts Episode 316](http://railscasts.com/episodes/316-private-pub) for a demonstration of Simplepub.
|
6
|
+
|
7
|
+
|
8
|
+
## Setup
|
9
|
+
|
10
|
+
Add the gem to your Gemfile and run the `bundle` command to install it. You'll probably want to add "thin" to your Gemfile as well to serve Faye.
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem "simplepub"
|
14
|
+
gem "thin"
|
15
|
+
```
|
16
|
+
|
17
|
+
Run the generator to create the initial files.
|
18
|
+
|
19
|
+
```
|
20
|
+
rails g simplepub:install
|
21
|
+
```
|
22
|
+
|
23
|
+
Next, start up Faye using the rackup file that was generated.
|
24
|
+
|
25
|
+
```
|
26
|
+
rackup simplepub.ru -s thin -E production
|
27
|
+
```
|
28
|
+
|
29
|
+
**In Rails 3.1** add the JavaScript file to your application.js file manifest.
|
30
|
+
|
31
|
+
```javascript
|
32
|
+
//= require simplepub
|
33
|
+
```
|
34
|
+
|
35
|
+
**In Rails 3.0** add the generated simplepub.js file to your layout.
|
36
|
+
|
37
|
+
```rhtml
|
38
|
+
<%= javascript_include_tag "simplepub" %>
|
39
|
+
```
|
40
|
+
|
41
|
+
It's not necessary to include faye.js since that will be handled automatically for you.
|
42
|
+
|
43
|
+
|
44
|
+
## Usage
|
45
|
+
|
46
|
+
Use the `subscribe_to` helper method on any page to subscribe to a channel.
|
47
|
+
|
48
|
+
```rhtml
|
49
|
+
<%= subscribe_to "/messages/new" %>
|
50
|
+
```
|
51
|
+
|
52
|
+
Use the `publish_to` helper method to send JavaScript to that channel. This is usually done in a JavaScript AJAX template (such as a create.js.erb file).
|
53
|
+
|
54
|
+
```rhtml
|
55
|
+
<% publish_to "/messages/new" do %>
|
56
|
+
$("#chat").append("<%= j render(@messages) %>");
|
57
|
+
<% end %>
|
58
|
+
```
|
59
|
+
|
60
|
+
This JavaScript will be immediately evaluated on all clients who have subscribed to that channel. In this example they will see the new chat message appear in real-time without reloading the browser.
|
61
|
+
|
62
|
+
|
63
|
+
## Alternative Usage
|
64
|
+
|
65
|
+
If you prefer to work through JSON instead of `.js.erb` templates, you can pass a hash to `publish_to` instead of a block and it will be converted `to_json` behind the scenes. This can be done anywhere (such as the controller).
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
Simplepub.publish_to "/messages/new", :chat_message => "Hello, world!"
|
69
|
+
```
|
70
|
+
|
71
|
+
And then handle this through JavaScript on the client side.
|
72
|
+
|
73
|
+
```javascript
|
74
|
+
Simplepub.subscribe("/messages/new", function(data, channel) {
|
75
|
+
$("#chat").append(data.chat_message);
|
76
|
+
});
|
77
|
+
```
|
78
|
+
|
79
|
+
The Ruby `subscribe_to` helper call is still necessary with this approach to grant the user access to the channel. The JavaScript is just a callback for any custom behavior.
|
80
|
+
|
81
|
+
|
82
|
+
## Configuration
|
83
|
+
|
84
|
+
The configuration is set separately for each environment in the generated `config/simplepub.yml` file. Here are the options.
|
85
|
+
|
86
|
+
* `server`: The URL to use for the Faye server such as `http://localhost:9292/faye`.
|
87
|
+
* `secret_token`: A secret hash to secure the server. Can be any string.
|
88
|
+
* `signature_expiration`: The length of time in seconds before a subscription signature expires. If this is not set there is no expiration. Note: if Faye is on a separate server from the Rails app, the system clocks must be in sync for the expiration to work properly.
|
89
|
+
|
90
|
+
|
91
|
+
## How It Works
|
92
|
+
|
93
|
+
The `subscribe_to` helper will output the following script which subscribes the user to a specific channel and server.
|
94
|
+
|
95
|
+
```html
|
96
|
+
<script type="text/javascript">
|
97
|
+
Simplepub.sign({
|
98
|
+
channel: "/messages/new",
|
99
|
+
timestamp: 1302306682972,
|
100
|
+
signature: "dc1c71d3e959ebb6f49aa6af0c86304a0740088d",
|
101
|
+
server: "http://localhost:9292/faye"
|
102
|
+
});
|
103
|
+
</script>
|
104
|
+
```
|
105
|
+
|
106
|
+
The signature and timestamp checked on the Faye server to ensure users are only able to access channels you subscribe them to. The signature will automatically expire after the time specified in the configuration.
|
107
|
+
|
108
|
+
The `publish_to` method will send a post request to the Faye server (using `Net::HTTP`) instructing it to send the given data back to the browser.
|
109
|
+
|
110
|
+
|
111
|
+
## Serving Faye over HTTPS (with Thin)
|
112
|
+
|
113
|
+
To server Faye over HTTPS you could create a thin configuration file `config/simplepub_thin.yml` similar to the following:
|
114
|
+
|
115
|
+
```yaml
|
116
|
+
---
|
117
|
+
port: 4443
|
118
|
+
ssl: true
|
119
|
+
ssl_key_file: /path/to/server.pem
|
120
|
+
ssl_cert_file: /path/to/certificate_chain.pem
|
121
|
+
environment: production
|
122
|
+
rackup: simplepub.ru
|
123
|
+
```
|
124
|
+
|
125
|
+
The `certificate_chain.pem` file should contain your signed certificate, followed by intermediate certificates (if any) and the root certificate of the CA that signed the key.
|
126
|
+
|
127
|
+
Next reconfigure the URL in `config/simplepub.yml` to look like `https://your.hostname.com:4443/faye`
|
128
|
+
|
129
|
+
Finally start up Thin from the project root.
|
130
|
+
|
131
|
+
```
|
132
|
+
thin -C config/simplepub_thin.yml start
|
133
|
+
```
|
134
|
+
|
135
|
+
|
136
|
+
## Contributing
|
137
|
+
|
138
|
+
1. Fork it
|
139
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
140
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
141
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
142
|
+
5. Create new Pull Request
|
143
|
+
|
144
|
+
## Development & Feedback
|
145
|
+
|
146
|
+
Questions or comments? Please use the [issue tracker](https://github.com/speedyrails/simplepub/issues). Tests can be run with `bundle` and `rake` commands.
|
data/Rakefile
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
function buildSimplepub(doc) {
|
2
|
+
var self = {
|
3
|
+
connecting: false,
|
4
|
+
fayeClient: null,
|
5
|
+
fayeCallbacks: [],
|
6
|
+
subscriptions: {},
|
7
|
+
subscriptionObjects: {},
|
8
|
+
subscriptionCallbacks: {},
|
9
|
+
|
10
|
+
faye: function(callback) {
|
11
|
+
if (self.fayeClient) {
|
12
|
+
callback(self.fayeClient);
|
13
|
+
} else {
|
14
|
+
self.fayeCallbacks.push(callback);
|
15
|
+
if (self.subscriptions.server && !self.connecting) {
|
16
|
+
self.connecting = true;
|
17
|
+
var script = doc.createElement("script");
|
18
|
+
script.type = "text/javascript";
|
19
|
+
script.src = self.subscriptions.server + ".js";
|
20
|
+
script.onload = self.connectToFaye;
|
21
|
+
doc.documentElement.appendChild(script);
|
22
|
+
}
|
23
|
+
}
|
24
|
+
},
|
25
|
+
|
26
|
+
connectToFaye: function() {
|
27
|
+
self.fayeClient = new Faye.Client(self.subscriptions.server);
|
28
|
+
self.fayeClient.addExtension(self.fayeExtension);
|
29
|
+
for (var i=0; i < self.fayeCallbacks.length; i++) {
|
30
|
+
self.fayeCallbacks[i](self.fayeClient);
|
31
|
+
};
|
32
|
+
},
|
33
|
+
|
34
|
+
fayeExtension: {
|
35
|
+
outgoing: function(message, callback) {
|
36
|
+
if (message.channel == "/meta/subscribe") {
|
37
|
+
// Attach the signature and timestamp to subscription messages
|
38
|
+
var subscription = self.subscriptions[message.subscription];
|
39
|
+
if (!message.ext) message.ext = {};
|
40
|
+
message.ext.simplepub_signature = subscription.signature;
|
41
|
+
message.ext.simplepub_timestamp = subscription.timestamp;
|
42
|
+
}
|
43
|
+
callback(message);
|
44
|
+
}
|
45
|
+
},
|
46
|
+
|
47
|
+
sign: function(options) {
|
48
|
+
if (!self.subscriptions.server) {
|
49
|
+
self.subscriptions.server = options.server;
|
50
|
+
}
|
51
|
+
self.subscriptions[options.channel] = options;
|
52
|
+
self.faye(function(faye) {
|
53
|
+
var sub = faye.subscribe(options.channel, self.handleResponse);
|
54
|
+
self.subscriptionObjects[options.channel] = sub;
|
55
|
+
if (options.subscription) {
|
56
|
+
options.subscription(sub);
|
57
|
+
}
|
58
|
+
});
|
59
|
+
},
|
60
|
+
|
61
|
+
handleResponse: function(message) {
|
62
|
+
if (message.eval) {
|
63
|
+
eval(message.eval);
|
64
|
+
}
|
65
|
+
if (callback = self.subscriptionCallbacks[message.channel]) {
|
66
|
+
callback(message.data, message.channel);
|
67
|
+
}
|
68
|
+
},
|
69
|
+
|
70
|
+
subscription: function(channel) {
|
71
|
+
return self.subscriptionObjects[channel];
|
72
|
+
},
|
73
|
+
|
74
|
+
unsubscribeAll: function() {
|
75
|
+
for (var i in self.subscriptionObjects) {
|
76
|
+
if ( self.subscriptionObjects.hasOwnProperty(i) ) {
|
77
|
+
self.unsubscribe(i);
|
78
|
+
}
|
79
|
+
}
|
80
|
+
},
|
81
|
+
|
82
|
+
unsubscribe: function(channel) {
|
83
|
+
var sub = self.subscription(channel);
|
84
|
+
if (sub) {
|
85
|
+
sub.cancel();
|
86
|
+
delete self.subscriptionObjects[channel];
|
87
|
+
}
|
88
|
+
},
|
89
|
+
|
90
|
+
subscribe: function(channel, callback) {
|
91
|
+
self.subscriptionCallbacks[channel] = callback;
|
92
|
+
}
|
93
|
+
};
|
94
|
+
return self;
|
95
|
+
}
|
96
|
+
|
97
|
+
var Simplepub = buildSimplepub(document);
|
data/bin/simplepub
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
module Simplepub
|
2
|
+
module Generators
|
3
|
+
class InstallGenerator < Rails::Generators::Base
|
4
|
+
def self.source_root
|
5
|
+
Simplepub.templates_root
|
6
|
+
end
|
7
|
+
|
8
|
+
def copy_files
|
9
|
+
template "simplepub.yml", "config/simplepub.yml"
|
10
|
+
if ::Rails.version < "3.1"
|
11
|
+
copy_file "../../../../app/assets/javascripts/simplepub.js", "public/javascripts/simplepub.js"
|
12
|
+
end
|
13
|
+
# copy_file "simplepub.ru", "simplepub.ru"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Simplepub
|
2
|
+
module Cli
|
3
|
+
class Config < Thor
|
4
|
+
include Thor::Actions
|
5
|
+
|
6
|
+
desc "install", "Install configuration file"
|
7
|
+
def install
|
8
|
+
if Process.uid == 0
|
9
|
+
install_as_root
|
10
|
+
else
|
11
|
+
install_as_user
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.source_root
|
16
|
+
Simplepub.templates_root
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def install_as_root
|
22
|
+
say "Installing system wide as root", :green
|
23
|
+
directory "/etc/simplepub"
|
24
|
+
template "simplepub.yml", "/etc/simplepub/config.yml"
|
25
|
+
end
|
26
|
+
|
27
|
+
def install_as_user
|
28
|
+
say "Installing locally", :green
|
29
|
+
template "simplepub.yml", "#{ENV['HOME']}/.simplepub.yml"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'thin'
|
2
|
+
|
3
|
+
module Simplepub
|
4
|
+
module Cli
|
5
|
+
class Server < Thor
|
6
|
+
desc "start", "Start simplepub server"
|
7
|
+
option :rackup_file, desc: "Rack configuration file", default: Simplepub.rackup_file, aliases: "-R"
|
8
|
+
option :environment, desc: "Simplepub environment", default: "development", aliases: "-E"
|
9
|
+
option :rack_environment, desc: "Rack environment", default: "production", aliases: "-e"
|
10
|
+
option :port, desc: "Rack port", default: "4443", aliases: "-p"
|
11
|
+
option :address, desc: "Rack address", default: "127.0.0.1", aliases: "-a"
|
12
|
+
option :logfile, desc: "Rack server log file", default: "/tmp/simplepub.log", aliases: "-l"
|
13
|
+
option :pidfile, desc: "Rack server PID file", default: "/tmp/simplepub.pid", aliases: "-P"
|
14
|
+
option :no_daemon, desc: "Don't daemonize rack server", type: :boolean, default: false
|
15
|
+
def start
|
16
|
+
Simplepub.load_config
|
17
|
+
Simplepub.config.environment = options.environment
|
18
|
+
argv = [
|
19
|
+
"start",
|
20
|
+
"-R", options.rackup_file,
|
21
|
+
"-e", options.rack_environment,
|
22
|
+
"-p", options.port,
|
23
|
+
"-a", options.address,
|
24
|
+
"-l", options.logfile,
|
25
|
+
"-P", options.pidfile
|
26
|
+
]
|
27
|
+
if Simplepub.config.ssl_.certificate?
|
28
|
+
argv << "--ssl"
|
29
|
+
argv << "--ssl-verify"
|
30
|
+
argv << "--ssl-cert-file"
|
31
|
+
argv << Simplepub.config.ssl.certificate
|
32
|
+
if Simplepub.config.ssl.keyfile?
|
33
|
+
argv << "--ssl-key-file"
|
34
|
+
argv << Simplepub.config.ssl.keyfile
|
35
|
+
end
|
36
|
+
end
|
37
|
+
argv << "-d" unless options.no_daemon?
|
38
|
+
say "Loading server with #{argv.inspect}", :yellow
|
39
|
+
Thin::Runner.new(argv.flatten).run!
|
40
|
+
end
|
41
|
+
|
42
|
+
desc "stop", "Start simplepub server"
|
43
|
+
option :rackup_file, desc: "Rack configuration file", default: Simplepub.rackup_file, aliases: "-R"
|
44
|
+
option :rack_environment, desc: "Rack environment", default: "production", aliases: "-e"
|
45
|
+
option :port, desc: "Rack port", default: "9292", aliases: "-p"
|
46
|
+
option :address, desc: "Rack address", default: "127.0.0.1", aliases: "-a"
|
47
|
+
option :logfile, desc: "Rack server log file", default: "/tmp/simplepub.log", aliases: "-l"
|
48
|
+
option :pidfile, desc: "Rack server PID file", default: "/tmp/simplepub.pid", aliases: "-P"
|
49
|
+
def stop
|
50
|
+
Simplepub.load_config
|
51
|
+
argv = [
|
52
|
+
"stop",
|
53
|
+
"-R", options.rackup_file,
|
54
|
+
"-e", options.rack_environment,
|
55
|
+
"-p", options.port,
|
56
|
+
"-a", options.address,
|
57
|
+
"-l", options.logfile,
|
58
|
+
"-P", options.pidfile
|
59
|
+
]
|
60
|
+
Thin::Runner.new(argv.flatten).run!
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'hashie'
|
3
|
+
|
4
|
+
module Simplepub
|
5
|
+
module Configuration
|
6
|
+
attr_reader :config
|
7
|
+
|
8
|
+
def configure(&block)
|
9
|
+
yield(config)
|
10
|
+
end
|
11
|
+
|
12
|
+
# Resets the configuration to the default (empty hash)
|
13
|
+
def reset_config
|
14
|
+
@config = Hashie::Mash.new
|
15
|
+
end
|
16
|
+
|
17
|
+
def default_config_file
|
18
|
+
return config.config_file if config.config_file?
|
19
|
+
config_file = config_file_paths.detect { |path| File.exists?(path) }
|
20
|
+
config_file
|
21
|
+
end
|
22
|
+
|
23
|
+
def default_environment
|
24
|
+
config.environment? ? config.environment : "development"
|
25
|
+
end
|
26
|
+
|
27
|
+
# Loads the configuration from a given YAML file and environment (such as production)
|
28
|
+
def load_config(filename=default_config_file, environment=default_environment)
|
29
|
+
raise ConfigurationFileError, "Configuration file not found. Available paths: #{config_file_paths.inspect}" if filename.nil?
|
30
|
+
|
31
|
+
yaml = YAML.load_file(filename)[environment.to_s]
|
32
|
+
|
33
|
+
raise ArgumentError, "The #{environment} environment does not exist in #{filename}" if yaml.nil?
|
34
|
+
|
35
|
+
config.deep_merge!(yaml)
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def config_file_paths
|
41
|
+
[
|
42
|
+
"#{ENV['HOME']}/.simplepub.yml",
|
43
|
+
"/etc/simplepub/config.yml",
|
44
|
+
"/etc/simplepub.yml"
|
45
|
+
]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require "simplepub/view_helpers"
|
2
|
+
|
3
|
+
module Simplepub
|
4
|
+
class Engine < Rails::Engine
|
5
|
+
# Loads the simplepub.yml file if it exists.
|
6
|
+
initializer "simplepub.config" do
|
7
|
+
path = Rails.root.join("config/simplepub.yml")
|
8
|
+
Simplepub.load_config(path, Rails.env) if path.exist?
|
9
|
+
end
|
10
|
+
|
11
|
+
# Adds the ViewHelpers into ActionView::Base
|
12
|
+
initializer "simplepub.view_helpers" do
|
13
|
+
ActionView::Base.send :include, ViewHelpers
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Simplepub
|
2
|
+
# This class is an extension for the Faye::RackAdapter.
|
3
|
+
# It is used inside of Simplepub.faye_app.
|
4
|
+
class FayeExtension
|
5
|
+
# Callback to handle incoming Faye messages. This authenticates both
|
6
|
+
# subscribe and publish calls.
|
7
|
+
def incoming(message, callback)
|
8
|
+
if message["channel"] == "/meta/subscribe"
|
9
|
+
authenticate_subscribe(message)
|
10
|
+
elsif message["channel"] !~ %r{^/meta/}
|
11
|
+
authenticate_publish(message)
|
12
|
+
end
|
13
|
+
callback.call(message)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
# Ensure the subscription signature is correct and that it has not expired.
|
19
|
+
def authenticate_subscribe(message)
|
20
|
+
subscription = Simplepub.subscription({
|
21
|
+
:channel => message["subscription"],
|
22
|
+
:timestamp => message["ext"]["simplepub_timestamp"]
|
23
|
+
})
|
24
|
+
|
25
|
+
if message["ext"]["simplepub_signature"] != subscription[:signature]
|
26
|
+
message["error"] = "Incorrect signature."
|
27
|
+
elsif Simplepub.signature_expired? message["ext"]["simplepub_timestamp"].to_i
|
28
|
+
message["error"] = "Signature has expired."
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Ensures the secret token is correct before publishing.
|
33
|
+
def authenticate_publish(message)
|
34
|
+
if Simplepub.config[:secret_token].nil?
|
35
|
+
raise Error, "No secret_token config set, ensure simplepub.yml is loaded properly."
|
36
|
+
elsif message["ext"]["simplepub_token"] != Simplepub.config[:secret_token]
|
37
|
+
message["error"] = "Incorrect token."
|
38
|
+
else
|
39
|
+
message["ext"]["simplepub_token"] = nil
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|