{"id":18018900,"url":"https://github.com/ikeikeikeike/sitesx","last_synced_at":"2025-03-26T20:30:58.755Z","repository":{"id":57548635,"uuid":"79349473","full_name":"ikeikeikeike/sitesx","owner":"ikeikeikeike","description":"A Phoenix SubDomainer which makes subdomain using DigitalOcean, Cloudflare, etc. API and contains convenient view helper interface along with Plug and Ecto.","archived":false,"fork":false,"pushed_at":"2017-07-06T04:02:55.000Z","size":69,"stargazers_count":10,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-22T10:24:07.083Z","etag":null,"topics":["cloudflare","digitalocean","ecto","helper","phoenix","plug","subdomain"],"latest_commit_sha":null,"homepage":"","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ikeikeikeike.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-01-18T14:46:10.000Z","updated_at":"2024-07-02T10:39:09.000Z","dependencies_parsed_at":"2022-08-28T11:31:08.830Z","dependency_job_id":null,"html_url":"https://github.com/ikeikeikeike/sitesx","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ikeikeikeike%2Fsitesx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ikeikeikeike%2Fsitesx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ikeikeikeike%2Fsitesx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ikeikeikeike%2Fsitesx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ikeikeikeike","download_url":"https://codeload.github.com/ikeikeikeike/sitesx/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245731269,"owners_count":20663147,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cloudflare","digitalocean","ecto","helper","phoenix","plug","subdomain"],"created_at":"2024-10-30T05:06:41.055Z","updated_at":"2025-03-26T20:30:58.444Z","avatar_url":"https://github.com/ikeikeikeike.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Sitesx\n\n[![Build Status](http://img.shields.io/travis/ikeikeikeike/sitesx.svg?style=flat-square)](http://travis-ci.org/ikeikeikeike/sitesx)\n[![Ebert](https://ebertapp.io/github/ikeikeikeike/sitesx.svg)](https://ebertapp.io/github/ikeikeikeike/sitesx)\n[![Hex version](https://img.shields.io/hexpm/v/sitesx.svg \"Hex version\")](https://hex.pm/packages/sitesx)\n[![Inline docs](https://inch-ci.org/github/ikeikeikeike/sitesx.svg)](http://inch-ci.org/github/ikeikeikeike/sitesx)\n[![Lisence](https://img.shields.io/hexpm/l/ltsv.svg)](https://github.com/ikeikeikeike/sitesx/blob/master/LICENSE)\n\nA Phoenix SubDomainer which makes subdomain using DigitalOcean, Cloudflare, etc. API and contains convenient view helper interface along with Plug and Ecto\n\n## Installation\n\nIf [available in Hex](https://hex.pm/docs/publish), the package can be installed as:\n\n1. Add `sitesx` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [{:sitesx, \"~\u003e 0.10\"}]\nend\n```\n\n2. Ensure `sitesx` is started before your application:\n\n```elixir\ndef application do\n  [applications: [:sitesx]]\nend\n```\n\n3. Sitesx needs to add a migration:\n\n```elixir\ndefmodule MyApp.Repo.Migrations.CreateSite do\n  use Ecto.Migration\n\n  def change do\n    create table(:sites) do\n      add :name, :string\n      add :dns, :boolean, default: false\n\n      timestamps()\n    end\n    create index(:sites, [:name], unique: true)\n    create index(:sites, [:dns])\n\n  end\nend\n```\n\n4. After definition, needs to add a model:\n\n```elixir\ndefmodule MyApp.Site do\n  use MyApp.Web, :model\n\n  schema \"sites\" do\n    field :name, :string\n    field :dns, :boolean\n\n    timestamps()\n  end\n\n  def changeset(struct, params \\\\ %{}) do\n    struct\n    |\u003e cast(params, [:name dns])\n    |\u003e validate_required([:name])\n    |\u003e unique_constraint(:name)\n  end\nend\n```\n\n~~Mix commands `mix sitesx.gen.model` or `mix sitesx.gen.schema` will be able to generate No.3 and No.4 instead of define manually.~~\n\n```shell\n$ mix sitesx.gen.model   # Until phoenix v1.2.x\n$ mix sitesx.gen.schema  # Phoenix v1.3.x\n```\n\n5. Stores Sitesx model into private on Plug struct.\n\nA `Sitesx.Plug` module extracts domain information from request URL or `sub` queryparameter.\n\nPlug in Router\n\n```elixir\ndefmodule MyApp.Router do\n  use MyApp.Web, :router\n\n  pipeline :browser do\n    plug :accepts, [\"html\"]\n    plug :fetch_session\n    plug :fetch_flash\n    plug :protect_from_forgery\n    plug :put_secure_browser_headers\n\n    plug Sitesx.Plug\n  end\nend\n```\n\nor\n\nPlug in Controller\n\n```elixir\ndefmodule MyApp.MyController do\n  use MyApp.Web, :controller\n\n  plug Sitesx.Plug\nend\n```\n\n6. Configuration\n\n`Cloudflare`\n\n```elixir\nconfig :sitesx,\n  otp_app: MyApp,\n  domain: \"example.com\",\n  ensure_domain_interval: 300,\n  dns: [\n    provider: :cloudflare,\n    auth_email: \"mail@example.com\",\n    auth_key: \"hash-key1\",\n    zone_identifier: \"hash-key1\",\n  ]\n```\n\n`Digitalocean`\n\n```elixir\nconfig :sitesx,\n  otp_app: MyApp,\n  domain: \"example.com\",\n  request_options: [hackney: [pool: :cloudflare]],\n  dns: :digitalocean\n```\n\n###### `Sitesx.App`, Configuration from mix config and environment variable.\n\nhexdocs: https://hexdocs.pm/sitesx/Sitesx.App.html\n\n## Customization\n\nObviously, those definition are still not enough to work on development. Therefore it will be changed is better that have `1:N` relation between one of a model and `Site` model.\n\n```elixir\ndefmodule MyApp.Entry do\n  use MyApp.Web, :model\n\n  schema \"entries\" do\n    belongs_to :site, MyApp.Site\n    field :title, :string\n    field :content, :string\n\n    timestamps()\n  end\nend\n```\n\nAnd then\n\n```elixir\ndefmodule MyApp.Site do\n  use MyApp.Web, :model\n\n  schema \"sites\" do\n    has_many :entries, MyApp.Entry\n    field :name, :string\n    field :dns, :boolean\n\n    timestamps()\n  end\nend\n```\n\n## Usage\n\n#### Create a record\n\nCreate `subdomain1` as `CNAME record` to DNS Provider which will be stored on Cloudflare or Digitalocean. And also will be stores that into database, too.\n\n```elixir\n{_, site} = Sitesx.Q.get_or_create(\"subdomain1\")\nRepo.insert_or_update(entry, %{site_id: site.id})\n```\n\n#### Generate a URL\n\nImport module\n\n```elixir\n# web/web.ex\ndefmodule MyApp.Web do\n  def view do\n    quote do\n      use Phoenix.View, root: \"web/templates\"\n      ...\n      ...\n      ...\n      import Sitesx.Helpers\n    end\n  end\nend\n```\n\nThen call function\n\n```elixir\nsubdomain_url(@conn, \"entry.latest\")\n#-\u003e Ensured domain: http://subdomain1.example.com/entries/latest\n#-\u003e Not ensured domain: http://example.com/entries/latest?sub=subdomain1\n\nsubdomain_url(\"subdomain2\", @conn, \"page.index\")\n#-\u003e Ensured domain: http://subdomain2.example.com/entries/latest\n#-\u003e Not ensured domain: http://example.com/entries/latest?sub=subdomain2\n```\n\n\n## Documentation\n\n#### `Sitesx.Domain` manages DNS record which has `detect`, `extract`, `create` method.\n\nhexdocs: https://hexdocs.pm/sitesx/Sitesx.Domain.html\n\n#### `Sitesx.Helpers`. Generate URL with subdomain for controller or templates along with [Phoenix.HTML.SimplifiedHelpers.URL](https://hexdocs.pm/phoenix_html_simplified_helpers/1.1.1/Phoenix.HTML.SimplifiedHelpers.URL.html)\n\nhexdocs: https://hexdocs.pm/sitesx/Sitesx.Helpers.html\n\n#### `Sitesx.Plug`. Stores Sitesx model into private on Plug struct.\n\nhexdocs: https://hexdocs.pm/sitesx/Sitesx.Plug.html\n\n#### `Sitesx.Q`. Ecto Query Helper along with Sitesx.Plug\n\nhexdocs: https://hexdocs.pm/sitesx/Sitesx.Q.html\n\n#### `Sitesx.App`, Configuration from mix config and environment variable.\n\nhexdocs: https://hexdocs.pm/sitesx/Sitesx.App.html\n\n#### Documentation\n\nhexdocs: https://hexdocs.pm/sitesx\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fikeikeikeike%2Fsitesx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fikeikeikeike%2Fsitesx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fikeikeikeike%2Fsitesx/lists"}