1
0
mirror of https://github.com/mgerb/classic-wow-forums synced 2026-01-11 17:42:48 +00:00
This commit is contained in:
2017-12-31 20:19:44 -06:00
commit e856cc5172
37 changed files with 1196 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
defmodule MyAppWeb.UserSocket do
use Phoenix.Socket
## Channels
# channel "room:*", MyAppWeb.RoomChannel
## Transports
transport :websocket, Phoenix.Transports.WebSocket
# transport :longpoll, Phoenix.Transports.LongPoll
# Socket params are passed from the client and can
# be used to verify and authenticate a user. After
# verification, you can put default assigns into
# the socket that will be set for all channels, ie
#
# {:ok, assign(socket, :user_id, verified_user_id)}
#
# To deny connection, return `:error`.
#
# See `Phoenix.Token` documentation for examples in
# performing token verification on connect.
def connect(_params, socket) do
{:ok, socket}
end
# Socket id's are topics that allow you to identify all sockets for a given user:
#
# def id(socket), do: "user_socket:#{socket.assigns.user_id}"
#
# Would allow you to broadcast a "disconnect" event and terminate
# all active sockets and channels for a given user:
#
# MyAppWeb.Endpoint.broadcast("user_socket:#{user.id}", "disconnect", %{})
#
# Returning `nil` makes this socket anonymous.
def id(_socket), do: nil
end

View File

@@ -0,0 +1,20 @@
defmodule MyAppWeb.BattleNetController do
use MyAppWeb, :controller
alias MyAppWeb.Response
alias MyApp.BattleNet.Auth
# https://us.battle.net/oauth/authorize?redirect_uri=https://localhost/api/battlenet/authorize&scope=wow.profile&client_id=vxqv32fddxsy6cmk6259amtymbuzmfrq&response_type=code
def authorize(conn, %{"code" => code}) when not is_nil(code) do
{output, status} = case Auth.get_token(code) do
{:ok, token} -> {token, 200}
{:error, err} -> {err, 400}
end
conn
|>put_status(status)
|>Response.json(output)
end
end

View File

@@ -0,0 +1,12 @@
defmodule MyAppWeb.UserController do
use MyAppWeb, :controller
alias MyAppWeb.Response
def index(conn, params) do
IO.inspect(conn)
IO.inspect(params)
conn
|> Response.json("Auth works!")
end
end

55
lib/myapp_web/endpoint.ex Normal file
View File

@@ -0,0 +1,55 @@
defmodule MyAppWeb.Endpoint do
use Phoenix.Endpoint, otp_app: :myapp
socket "/socket", MyAppWeb.UserSocket
# Serve at "/" the static files from "priv/static" directory.
#
# You should set gzip to true if you are running phoenix.digest
# when deploying your static files in production.
plug Plug.Static,
at: "/", from: :myapp, gzip: false,
only: ~w(css fonts images js favicon.ico robots.txt)
# Code reloading can be explicitly enabled under the
# :code_reloader configuration of your endpoint.
if code_reloading? do
plug Phoenix.CodeReloader
end
plug Plug.RequestId
plug Plug.Logger
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
pass: ["*/*"],
json_decoder: Poison
plug Plug.MethodOverride
plug Plug.Head
# The session will be stored in the cookie and signed,
# this means its contents can be read but not tampered with.
# Set :encryption_salt if you would also like to encrypt it.
plug Plug.Session,
store: :cookie,
key: "_myapp_key",
signing_salt: "WENlVlsb"
plug MyAppWeb.Router
@doc """
Callback invoked for dynamically configuring the endpoint.
It receives the endpoint configuration and checks if
configuration should be loaded from the system environment.
"""
def init(_key, config) do
if config[:load_from_system_env] do
port = System.get_env("PORT") || raise "expected the PORT environment variable to be set"
{:ok, Keyword.put(config, :http, [:inet6, port: port])}
else
{:ok, config}
end
end
end

24
lib/myapp_web/gettext.ex Normal file
View File

@@ -0,0 +1,24 @@
defmodule MyAppWeb.Gettext do
@moduledoc """
A module providing Internationalization with a gettext-based API.
By using [Gettext](https://hexdocs.pm/gettext),
your module gains a set of macros for translations, for example:
import MyAppWeb.Gettext
# Simple translation
gettext "Here is the string to translate"
# Plural translation
ngettext "Here is the string to translate",
"Here are the strings to translate",
3
# Domain-based translation
dgettext "errors", "Here is the error message to translate"
See the [Gettext Docs](https://hexdocs.pm/gettext) for detailed usage.
"""
use Gettext, otp_app: :myapp
end

22
lib/myapp_web/response.ex Normal file
View File

@@ -0,0 +1,22 @@
defmodule MyAppWeb.Response do
# error handling based on http status
def json(conn, data) do
output = cond do
conn.status && conn.status >= 400 ->
%{
error: %{
status: conn.status,
message: data,
}
}
true -> %{
data: data
}
end
Phoenix.Controller.json(conn, output)
end
end

28
lib/myapp_web/router.ex Normal file
View File

@@ -0,0 +1,28 @@
defmodule MyAppWeb.Router do
use MyAppWeb, :router
alias MyApp.Guardian.AuthPipeline
pipeline :api do
plug :accepts, ["json"]
end
pipeline :api_auth do
plug AuthPipeline.JSON
end
# Other scopes may use custom stacks.
scope "/api", MyAppWeb do
pipe_through [:api]
scope "/battlenet" do
get "/authorize", BattleNetController, :authorize
end
scope "/user" do
# authenticated routes
pipe_through [:api_auth]
get "/", UserController, :index
end
end
end

View File

@@ -0,0 +1,29 @@
defmodule MyAppWeb.ErrorHelpers do
@moduledoc """
Conveniences for translating and building error messages.
"""
@doc """
Translates an error message using gettext.
"""
def translate_error({msg, opts}) do
# Because error messages were defined within Ecto, we must
# call the Gettext module passing our Gettext backend. We
# also use the "errors" domain as translations are placed
# in the errors.po file.
# Ecto will pass the :count keyword if the error message is
# meant to be pluralized.
# On your own code and templates, depending on whether you
# need the message to be pluralized or not, this could be
# written simply as:
#
# dngettext "errors", "1 file", "%{count} files", count
# dgettext "errors", "is invalid"
#
if count = opts[:count] do
Gettext.dngettext(MyAppWeb.Gettext, "errors", msg, msg, count, opts)
else
Gettext.dgettext(MyAppWeb.Gettext, "errors", msg, opts)
end
end
end

View File

@@ -0,0 +1,17 @@
defmodule MyAppWeb.ErrorView do
use MyAppWeb, :view
def render("404.json", _assigns) do
%{errors: %{detail: "Page not found"}}
end
def render("500.json", _assigns) do
%{errors: %{detail: "Internal server error"}}
end
# In case no render clause matches or no
# template is found, let's render it as 500
def template_not_found(_template, assigns) do
render "500.json", assigns
end
end