Showing posts with label web. Show all posts
Showing posts with label web. Show all posts

20140605

pow and byebug - staying in the web app dev env heaven: an update for ruby 2.1

The previous version (for ruby 1.9) is here.

It seems ruby-debug has fallen out of favour with the community and the replacement is byebug.
So here's my follow-up on the old post.

The steps to enable byebugging in your app dev env:
  1. echo export POW_WORKERS=1 >> ~/.powconfig  # make sure pow runs only one app process — globally
  2. cd <your project directory>
  3. Add byebug to your bundle (and remove ruby-debug if exists).
  4. Paste the following snippet at the end of your config/environments/development.rb — for rails, or in the appropriate development environment initialisation module — for the platform of your choice:
  5. echo export RUBY_DEBUG_PORT=10007 >> .powenv  # make sure we enable debugging with a machine-unique port, e.g. 10007 (you should pick different ports for different projects)
  6. touch tmp/restart.txtThen make any request to actually restart your server.
  7. byebug -R localhost:10007 -d  # you're back in business, go wild with debugger's in your code

20121119

subdomains in rails apps: a current brief

Subdomains (or second level domains) are still a nice way to present separate interfaces to a single web app — commonly to give a certain kind of customers the feel-n-touch of a dedicated app install.

Here are most of the related aspects based on a particular Rails3 project:

  • some model Group, representing a customer, or rather a group of users, has a string column domain (migration not shown, but don’t forget to index by that column)

    in app/models/group.rb:

    has_many :users
    before_validation :downcase_domain, :if => :domain_changed?
    validates :domain, presence: true, uniqueness: true, length: {maximum: 255}, format: /^[0-9a-z-]+$/
    
    
    def host
      "#{domain}.#{ROOT_DOMAIN}"
    end
    
    
    protected
    def downcase_domain
      self.domain = domain.to_s.downcase
    end
    
  • under assumption that you’re using devise and your (group’s) user identity model is User (updated for everchanging devise 2.0.4)

    in app/models/user.rb:

    belongs_to :group
    devise :database_authenticatable, ..., :request_keys => [:app_subdomain]
    
    
    def self.find_for_authentication(conditions={})
      group = Group.find_by_domain(conditions.delete(:app_subdomain))
      return nil unless group.present?
      conditions[:group_id] = group.id
      super
    end
    
  • you’ll have some admin interface where the groups are managed (e.g. active_admin, not recommended)

    in app/admin/groups.rb:

    link_to group.domain, root_url(host: group.host)
    
  • you will certainly want to do something specific in config/routes.rb

  • you’ll want some handy helper method, to know which guvnor you’re serving

    in app/controllers/application_controller.rb:

    helper_method :current_group
    def current_group
      return @current_group if defined? @current_group
      @current_group = request.app_subdomain && Group.find_by_domain(request.app_subdomain)
    end
    
  • you’ll need an initializer of some sort to set a constant and monkey-patch the request class, so…

    in config/initializers/subdomain.rb:

    ROOT_DOMAIN ||= ENV['ROOT_DOMAIN'] or raise "ROOT_DOMAIN must be set"
    
    
    # (): paranoid monkey patching :()
    class ActionDispatch::Request
      def app_subdomain
        return @app_subdomain if defined? @app_subdomain
        @@app_hostname_regex ||= /^(?:([0-9a-z-]+).)?#{Regexp.escape(ROOT_DOMAIN)}$/
        raise 'Wrong domain' unless host.downcase =~ @@app_hostname_regex
        @app_subdomain = $1
      end
    end
    
  • then, for your production (and staging) environment on, say, heroku, you’ll have to setup your lovely app domain name (with wildcard subdomains) and set the environment variable ROOT_DOMAIN to it

  • for test environment, which is also good for the handy circleci

    in config/environments/test.rb:

    ROOT_DOMAIN = 'lvh.me'  # yes, it's a magic domain for 127.0.0.1 //smackaho.st RIP
    
  • for other environment cases, be sure to set either ROOT_DOMAIN or ENV['ROOT_DOMAIN'] as appropriate

  • if you use factories (and girls, factory_girl)

    in spec/factories.rb:

    factory :group do
      sequence(:domain) {|n| "dom-#{n}"}  # or better still, use `forgery` with some smart randomness
      ...
    end
    
  • if you use capybara (2.0.0 at least, recommended) and rspec (rspec-rails 2.12.0 at least)

    in spec/spec_helper.rb: (in Spork.prefork block if you use spork, recommended)

    Capybara.always_include_port = true  # unless you `visit` external sites in your feature specs
    

    and then in some spec/features/..._spec.rb:

    visit("http://some_domain.#{ROOT_DOMAIN}/some_path")
    # or
    visit(some_url host: @group.host)  # if you're playing dirty, using pre-fabricated data and route helpers, recommended
    
  • in some spec/controllers/..._spec.rb you’ll have to include something like this:

    before(:each) do
      request.host = @group.host
    end
    
  • don’t forget the specs for domains in spec/models/group_spec.rb and other relevant places

May your sub-domains be obedient to their master.

20120608

app cache manifest should be public

As a rule of thumb, as of today, if you don't want trouble, make your HTML5 application cache manifest publicly accessible.

20120403

pow, guard and rdebug - staying in the web app dev env heaven: for ruby 1.9 only

The new version (for ruby 2.1) is here.

If you, like me, use pow and guard (with spork of Rails 3 standard setup) for the perfect web app development environment, you might have stumbled upon a problem of debugging the server with rdebug -c which tends to connect to a wrong process even when working on just one project (and that's because spork itself starts the remote debug server by default).

So, firstly, you will probably want to limit your server instances run by pow to 1 by
echo export POW_WORKERS=1 >> ~/.powconfig

Then, to actually enable remote debugging you should place the following in your ./config/environments/development.rb:


And finally, to set the port of your choice for the project,
echo export RUBY_DEBUG_PORT=10007 >> ./.powenv

Now, you are welcome to
touch tmp/restart.txt
and (after a bunch of your CPU's cycles)
rdebug -c -p 10007

You're back in heaven, have a happy stay!

20090719

in-cwd web serving

I've come across a simple need to serve some (static) files temporarily, for the development. I didn't find anything on the surface for this purpose, though the need may be quite common, hence the note.
The obvious choice for the server is lighttpd, and the scripting goes along the lines of lighthere.sh.

If anyone is aware of another known-good solution, please share.

20090529

Sinatra is really good just for one-filers.

p.s. But merb's flat app is pretty much in its diapers, and they're not fresh clean.

20090127

jobseek.whatever (this far)

I am kinda frustrated: they say "Israel has the largest number of engineers per capita" (as appears on one of the funds' sites I've visited, see below), and they are probably right, but when it comes to the web presence... [i'm cool]... well, it just doesn't feel like this.

Here's the plan:
  • Collect (and keep collecting) "jobs/careers/hiring" web resources of the companies (screw those that don't have it)
  • Wrap them with Atom/RSS streams enriched with the relevant metadata: geographic location, industry segment, etc.
    • Note that the metadata may be constant per stream or overridden per post
    • A corresponding stream directory is appropriate — organised by the same metadata accumulated
  • Aggregate all the streams into one huge pipe (this may span worldwide, not a problem since each stream is of very low volume and there's a limited number of them)
  • Provide individually metadata-filtered streams on demand
  • Watch hitech headhunters and placement companies die
For now, I'm browsing through the Venture Capital Funds in Israel and adding the funded companies of my interest to my predeliciousss with tags "israel", "jobs", "software" plus others.
I'm asking from everyone to do the same towards execution of the plan above.

Unfortunately, there are no other useful resources found, those two are close to silly jokes: d&a, Go2Web20.net - The complete Web 2.0 sites directory (you should select what you want there and then laugh).

20090117

*nice* document-as-a-web-page hosting wanted

I have a domain. that's mouldwarp.com
I have a small HTML+CSS document and a couple of its copies in different formats. that's my fancy resume
I want to make it simply and reliably hosted on a subdomain. that's on resume.mouldwarp.com or something
I don't want (I really don't) too much trouble or any money spent to make this happen. money is trouble
I use Google Apps BETA;) Standard Edition for my domain and I also use Yahoo! mail&stuff. read on this below
WTF am I doing wrong? Do I want too much? Where's that button? hello, which year is this?

Google are nazis sophisticated bastards (very frustrating):
  • There is no straightforward option to upload a CSSed HTML: neither on Docs nor on Sites
    • You can, however, go through a trouble of entering HTML and CSS separately through the menu in a Doc (watch for pitfalls)
  • A published doc URL is ugly, has nothing to do with your domain, and you can't change it
  • You can't use your published doc webpage with your Google Site in any decent way (let alone simply mapping the site page to it)
  • And also a regular *FREE* user's Docs have much _more_ functionality to them than those of GApps (SE?) including some blogging engine integration
Yahoo! are more stupid and thus better: I've uploaded my resume at http://geocities.com/constantine_shapiro/ for ads, but they don't provide custom domain option for the same money, so I've made a redirection from http://resume.mouldwarp.com/ (with my GoDaddy) for the time being. does it look nice at least?
I haven't yet found an easy and free way to get what I want, so once I had enough of the current setup, I'll go checking Free Webspace and Free Web Hosting Services. any other ideas?

p.s. I know of the option of hosting all the stuff like this at home or at any other admin-accessible web server. Been there, of course, but now I strive to "minimalise" my life, i.e. to focus on things of my greatest interest, gain and joy - and routine web server administration is nowhere near them, sorry.