Sinatra app that provides counters to glitchwrks.com
This is a little Sinatra app that handles hit counters (or 'traffic analytics,' if hit counters are too 90's) for glitchwrks.com . This is currently deployed on an OpenBSD 7.6 host.
- Sinatra
- ActiveRecord
- Capistrano 3 for deployments
- Puma for app serving
Currently using MariaDB for the database, but it uses nothing specific to MariaDB and works fine with even SQLite. It can be run as a Puma application, or directly via ruby counters.rb
.
This feature is broken into two main DB models: Counter and Hit. Counters are parent objects to Hits; that is, Counters have many Hits. They also contain an IPv4 preload and IPv6 preload. Preloads allow for nonzero starting points, which also allows for consolidating hits. Additionally, SitewideCounters aggregate all unique hits for all counters. Hits store the IP address, first appearance, most recent appearance, and whether the IP was IPv6 or not.
The rake task counter:consolidate_hits
allows for keeping the DB small by consolidating all hits for a counter into the counter's preload value. The current hits are counted, added to the counter's preload, and then deleted.
Doing a HTTP GET to /:name
will return a bit of JavaScript to write the current hit count to the document. Including ?ipv6=true
in the query params will cause the IPv6 count to be displayed, too.
Configure your development database in config/database.yml
and run the following:
RACK_ENV=development rake db:reset
rake db:migrate
rake db:seed
rake counter:create NAME=test
ruby counters.rb
After the above, you should be able to visit http://localhost:8080/test
This application uses RSpec. To run the test suite on a new workstation, configure your test database in config/database.yml
and do:
RACK_ENV=test rake db:test:prepare
rspec
SimpleCov provides code coverage reporting.
To manage the Puma process on the application server, a custom Capistrano task, puma:restart
has been defined. This task uses OpenBSD's doas
to invoke the rc-script that controls the Puma process.
The following features were once part of this application, when it was called site_services
, but have since been moved to rails_services
. The final versions of these features are available at the following SHAs: