aboutsummaryrefslogtreecommitdiff
path: root/shardweb/lib/shard_web/router.ex
blob: 4311a2924292b87146c38c6b7713b48e7ecd75b1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
defmodule ShardWeb.Router do
  use ShardWeb, :router

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_flash
    plug :protect_from_forgery
    plug :put_secure_browser_headers
    plug Plug.Parsers, parsers: [:urlencoded]
    plug :check_pk
  end

  pipeline :api do
    plug :accepts, ["json"]
  end

  scope "/", ShardWeb do
    pipe_through :browser # Use the default browser stack

    get "/", PageController, :index
    post "/peer/add", PageController, :add_peer

    get "/identity", IdentityController, :view
    post "/identity", IdentityController, :update
    post "/identity/switch", IdentityController, :switch
    post "/identity/create", IdentityController, :create

    get "/room/:room", RoomController, :show
  end

  # Other scopes may use custom stacks.
  # scope "/api", ShardWeb do
  #   pipe_through :api
  # end

  def check_pk(conn, _kv) do
    pk1 = get_session(conn, :pk)
    {conn, pk} = if pk1 == nil || not Shard.Keys.have_sk? pk1 do
      pk = Shard.Keys.get_any_identity
      conn = put_session(conn, :pk, pk)
      {conn, pk}
    else
      {conn, pk1}
    end
    nick = SApp.Identity.get_nick pk
    conn
    |> assign(:pk, pk)
    |> assign(:nick, nick)
    |> PhoenixGon.Controller.put_gon(pk: (pk|>Base.encode16))
  end

end