aboutsummaryrefslogblamecommitdiff
path: root/shardweb/lib/router.ex
blob: 2c69b8c8bb4b1babfd177782a9a4b8cb76d54f76 (plain) (tree)
1
2
3
4
5
6
7
8
9
10









                                             
                  








                                                         
                                       
                                               
                                                 
 
                                            
                                                
                                              


                                                        
 

                                                     





                                       













                                                                
                          


                                                             
   
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, :peer_list
    post "/peer/add", PageController, :add_peer
    get "/dashboard", PageController, :shard_list

    get "/people", IdentityController, :list
    get "/people/:pk", IdentityController, :view
    get "/identity", IdentityController, :self
    post "/identity", IdentityController, :update
    post "/identity/switch", IdentityController, :switch
    post "/identity/create", IdentityController, :create

    get "/chat/:chan", ChatController, :chat
    get "/pm/:people_list", ChatController, :privchat
  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)
    |> assign(:shard, nil)
    |> PhoenixGon.Controller.put_gon(pk: (pk|>Base.encode16))
  end

end