lucky_subdomain

Version, currently master branch1 version
  • master branchlatestJul 9, 2021

github.com/matthewmcgarvey/lucky_subdomain

No description declared in shard.yml.

1 stars
0 dependents
License: MIT

Installation

# Add this to your shard.yml
dependencies:
  lucky_subdomain:
    github: matthewmcgarvey/lucky_subdomain
    branch: master

master is a branch, not a release, so this tracks it rather than pinning a version.

Then run:

shards install

shard.yml

Crystal
1.0.0
License
MIT
Author
matthewmcgarvey

Dependencies

Runtime Dependencies

  • lucky~> 0.27.0github: luckyframework/lucky
  • habitat~> 0.4.7github: luckyframework/habitat

Development Dependencies

  • ameba~> 0.14github: crystal-ameba/amebadev

README

LuckySubdomain

Subdomain support for Lucky apps.

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      lucky_subdomain:
        github: matthewmcgarvey/lucky_subdomain
  2. Run shards install

Usage

require "lucky_subdomain"

Include the module in any action you want subdomain support for.

class BrowserAction < Lucky::Action
  include LuckySubdomain
end

Define your subdomain restriction.


class BrowserAction < Lucky::Action
  include LuckySubdomain
  
  register_subdomain # subdomain required but any will do
  register_subdomain "admin" # subdomain must equal "admin" (admin.example.com)
  register_subdomain /(prod|production)/ # subdomain must match regex
  register_subdomain ["tenant1", "tenant2", /tenant\d/] # subdomain must match one of the items
end

After defining the subdomain restriction, you can access the subdomain value from your route block

get "/users" do
  tenant = Tenant.new.name(subdomain).first
  users = UserQuery.new.tenant(tenant)
  html IndexPage, users: users
end

If you would like to skip the subdomain requirement, you can add this to the top of your action class

skip _match_subdomain

Local Development

If you try to register a subdomain and access it locally using subdomain.127.0.0.1:3000 you will receive an error. To get around this you can do one of two things:

  1. Always use subdomain.localhost:3000 and set tld_length to 0 in development (make sure it is not set to 0 when deployed)
  2. Update your /etc/hosts file with a development url

Contributing

  1. Fork it (https://github.com/matthewmcgarvey/lucky_subdomain/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors