Skip to content

Instantly share code, notes, and snippets.

@alfredbaudisch
Forked from sorentwo/start_oban_web.livemd
Created July 29, 2022 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alfredbaudisch/78c031fdddfa7d3b9d214a0deeb7073d to your computer and use it in GitHub Desktop.
Save alfredbaudisch/78c031fdddfa7d3b9d214a0deeb7073d to your computer and use it in GitHub Desktop.
Single Page Oban Web

Single Page Oban Web

Application.put_env(:sample, Sample.Repo, database: "oban_dev")

Application.put_env(:phoenix, :json_library, Jason)

Application.put_env(:sample, Sample.Endpoint,
  http: [ip: {127, 0, 0, 1}, port: 5001],
  server: true,
  live_view: [signing_salt: "aaaaaaaa"],
  secret_key_base: String.duplicate("a", 64)
)

Mix.install([
  {:plug_cowboy, "~> 2.5"},
  {:jason, "~> 1.0"},
  {:oban, "~> 2.13"},
  {:oban_web, "~> 2.9", repo: :oban},
  {:phoenix, "~> 1.6"},
  {:phoenix_live_view, "~> 0.17"},
  {:postgrex, "~> 0.16"}
])

Phoenix LiveView with Oban Web

defmodule Sample.Repo do
  use Ecto.Repo, adapter: Ecto.Adapters.Postgres, otp_app: :sample
end

defmodule Sample.ErrorView do
  use Phoenix.View, root: ""

  def render(_, _), do: "error"
end

defmodule Router do
  use Phoenix.Router
  import Phoenix.LiveView.Router
  import Oban.Web.Router

  pipeline :browser do
    plug(:accepts, ["html"])
    plug(:fetch_session)
    plug(:fetch_live_flash)
    plug(:protect_from_forgery)
    plug(:put_secure_browser_headers)
  end

  scope "/" do
    pipe_through(:browser)

    oban_dashboard("/oban")
  end
end

defmodule Sample.Endpoint do
  use Phoenix.Endpoint, otp_app: :sample

  @session_options store: :cookie, key: "_sample_key", signing_salt: "aaaaaaaa"

  socket("/live", Phoenix.LiveView.Socket, websocket: [connect_info: [session: @session_options]])

  plug(Plug.Session, @session_options)
  plug(Router)
end

defmodule Main do
  def call do
    children = [
      Sample.Repo,
      {Oban, repo: Sample.Repo, plugins: [Oban.Web.Plugins.Stats]},
      Sample.Endpoint
    ]

    {:ok, _} = Supervisor.start_link(children, strategy: :one_for_one)
  end
end

Main.call()
System.no_halt(true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment