defmodule BrightWeb.Router do use BrightWeb, :router alias Bright.ShoppingCart pipeline :browser do plug :accepts, ["html", "json"] plug :fetch_session plug :fetch_live_flash plug :put_root_layout, html: {BrightWeb.Layouts, :root} plug :protect_from_forgery plug :put_secure_browser_headers plug :fetch_current_user plug :fetch_current_cart end defp fetch_current_user(conn, _) do if user_uuid = get_session(conn, :current_uuid) do assign(conn, :current_uuid, user_uuid) else new_uuid = Ecto.UUID.generate() conn |> assign(:current_uuid, new_uuid) |> put_session(:current_uuid, new_uuid) end end defp fetch_current_cart(conn, _opts) do if cart = ShoppingCart.get_cart_by_user_uuid(conn.assigns.current_uuid) do assign(conn, :cart, cart) else {:ok, new_cart} = ShoppingCart.create_cart(conn.assigns.current_uuid) assign(conn, :cart, new_cart) end end pipeline :api do plug :accepts, ["json"] end scope "/", BrightWeb do pipe_through :browser get "/", PageController, :home resources "/products", ProductController get "/patrons", PatronController, :index get "/about", PageController, :about get "/api", PageController, :api resources "/cart_items", CartItemController, only: [:create, :delete] get "/cart", CartController, :show put "/cart", CartController, :update resources "/orders", OrderController, only: [:create, :show] resources "/archive", StreamController resources "/streams", StreamController resources "/vods", VodController resources "/vtubers", VtuberController resources "/vt", VtuberController do resources "/vods", VodController end # resources "/users", UserController resources "/tags", TagController ## @todo DANGER, platforms must only be writable by admins, (unless we implement SVG sanitizing) resources "/platforms", PlatformController get "/hello", HelloController, :index get "/hello/:messenger", HelloController, :show live "/posts", PostLive.Index, :index live "/posts/new", PostLive.Index, :new live "/posts/:id/edit", PostLive.Index, :edit live "/posts/:id", PostLive.Show, :show live "/posts/:id/show/edit", PostLive.Show, :edit end # Other scopes may use custom stacks. scope "/api", BrightWeb do pipe_through :api resources "/urls", UrlController, except: [:new, :edit] end # Enable LiveDashboard and Swoosh mailbox preview in development if Application.compile_env(:bright, :dev_routes) do # If you want to use the LiveDashboard in production, you should put # it behind authentication and allow only admins to access it. # If your application does not have an admins-only section yet, # you can use Plug.BasicAuth to set up some basic authentication # as long as you are also using SSL (which you should anyway). import Phoenix.LiveDashboard.Router scope "/dev" do pipe_through :browser live_dashboard "/dashboard", metrics: BrightWeb.Telemetry forward "/mailbox", Plug.Swoosh.MailboxPreview end end end