-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 29e2eb3
Showing
45 changed files
with
2,275 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Mix artifacts | ||
/_build | ||
/deps | ||
/*.ez | ||
|
||
# Generate on crash by the VM | ||
erl_crash.dump | ||
|
||
# Static artifacts | ||
/node_modules | ||
|
||
# Since we are building js and css from web/static, | ||
# we ignore priv/static/{css,js}. You may want to | ||
# comment this depending on your deployment strategy. | ||
/priv/static/css | ||
/priv/static/js | ||
|
||
# The config/prod.secret.exs file by default contains sensitive | ||
# data and you should not commit it into version control. | ||
# | ||
# Alternatively, you may comment the line below and commit the | ||
# secrets file as long as you replace its contents by environment | ||
# variables. | ||
/config/prod.secret.exs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Phoenix Rest JSON API Example with JWT | ||
|
||
Requires Erlang, Elixir and postgres | ||
|
||
To test this out: | ||
|
||
1. clone this repo | ||
2. in the new folder, go to restapp/config/dev.exs and enter your postgres database, username, and password. Also enter your preferred login username and password in config: :login. | ||
3. in the new folder, go to restapp/config/test.exs and re-enter your postgres database, username, and password | ||
4. cd to the folder, or just start a terminal in it | ||
5. run mix deps.get && mix deps.compile to retrieve jwt libraries and compile. | ||
6. run mix ecto.migrate --all | ||
7. run mix compile (this compiles a formatter module which is outside of the /web directory and does not compile automagically) | ||
8. run mix test (should have six passing tests) | ||
9. run mix phoenix.server | ||
|
||
#get regular user token | ||
1. post your username and password to localhost:4000/login default is {"data":{"username":"user","password":"password"}} | ||
2. copy token string | ||
3. in request header for localhost:4000/api/invoices, add Token: (your user token) | ||
|
||
#updating/adding to database with json | ||
1. post json to the /api/invoices route in the format {"data":{"contact_id":"1"},"includes":[{"sell_price":"7.48"},{"sell_price":"5.32"}]} dont forget to add the regular user token to request header! | ||
You should recieve a 201 created status, and reloading get /invoices should show both the invoice and related invoice items. If you forgot the token, or if you used the admin token instead, you will get a 401 error with 'incorrect audience' error. | ||
2. post json in the same format to /api/invoices/ (+ the id of the invoice just created). | ||
You should recieve a 200 OK status, with changes applied to database | ||
3. send request with method DELETE to /api/invoices/ (+ the id of the invoice just created) to delete record. | ||
|
||
TODO: | ||
test update, getone, and delete routes | ||
integrate with simple Durandal SPA to demostrate how token can be used | ||
|
||
|
||
Credits: | ||
1. Thoughtbot rest api example | ||
2. Phoenix and Elixir creators Chris Mccord and Jose Valim | ||
3. Ecto creator, joken |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
exports.config = { | ||
// See http://brunch.io/#documentation for docs. | ||
files: { | ||
javascripts: { | ||
joinTo: 'js/app.js' | ||
// To use a separate vendor.js bundle, specify two files path | ||
// https://github.com/brunch/brunch/blob/stable/docs/config.md#files | ||
// joinTo: { | ||
// 'js/app.js': /^(web\/static\/js)/, | ||
// 'js/vendor.js': /^(web\/static\/vendor)/ | ||
// } | ||
// | ||
// To change the order of concatenation of files, explictly mention here | ||
// https://github.com/brunch/brunch/tree/stable/docs#concatenation | ||
// order: { | ||
// before: [ | ||
// 'web/static/vendor/js/jquery-2.1.1.js', | ||
// 'web/static/vendor/js/bootstrap.min.js' | ||
// ] | ||
// } | ||
}, | ||
stylesheets: { | ||
joinTo: 'css/app.css' | ||
}, | ||
templates: { | ||
joinTo: 'js/app.js' | ||
} | ||
}, | ||
|
||
// Phoenix paths configuration | ||
paths: { | ||
// Which directories to watch | ||
watched: ["web/static", "test/static"], | ||
|
||
// Where to compile files to | ||
public: "priv/static" | ||
}, | ||
|
||
// Configure your plugins | ||
plugins: { | ||
babel: { | ||
// Do not use ES6 compiler in vendor code | ||
ignore: [/^(web\/static\/vendor)/] | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# This file is responsible for configuring your application | ||
# and its dependencies with the aid of the Mix.Config module. | ||
# | ||
# This configuration file is loaded before any dependency and | ||
# is restricted to this project. | ||
use Mix.Config | ||
|
||
# Configures the endpoint | ||
config :restapp, Restapp.Endpoint, | ||
url: [host: "localhost"], | ||
root: Path.dirname(__DIR__), | ||
secret_key_base: "am96wNTvwdNkDCzZ5TJh3lxhr8PuybmcLjGnXoVQe9jPFDbiDku6Znb3+DpCSK5x", | ||
debug_errors: false, | ||
pubsub: [name: Restapp.PubSub, | ||
adapter: Phoenix.PubSub.PG2] | ||
|
||
# Configures Elixir's Logger | ||
config :logger, :console, | ||
format: "$time $metadata[$level] $message\n", | ||
metadata: [:request_id] | ||
|
||
# Import environment specific config. This must remain at the bottom | ||
# of this file so it overrides the configuration defined above. | ||
import_config "#{Mix.env}.exs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use Mix.Config | ||
|
||
# For development, we disable any cache and enable | ||
# debugging and code reloading. | ||
# | ||
# The watchers configuration can be used to run external | ||
# watchers to your application. For example, we use it | ||
# with brunch.io to recompile .js and .css sources. | ||
config :restapp, Restapp.Endpoint, | ||
http: [port: 4000], | ||
debug_errors: true, | ||
code_reloader: true, | ||
cache_static_lookup: false, | ||
watchers: [node: ["node_modules/brunch/bin/brunch", "watch"]] | ||
|
||
# Watch static and templates for browser reloading. | ||
config :restapp, Restapp.Endpoint, | ||
live_reload: [ | ||
patterns: [ | ||
~r{priv/static/.*(js|css|png|jpeg|jpg|gif)$}, | ||
~r{web/views/.*(ex)$}, | ||
~r{web/templates/.*(eex)$} | ||
] | ||
] | ||
|
||
# Do not include metadata nor timestamps in development logs | ||
config :logger, :console, format: "[$level] $message\n" | ||
|
||
# Configure your database | ||
config :restapp, Restapp.Repo, | ||
adapter: Ecto.Adapters.Postgres, | ||
username: "postgres", | ||
password: "postgres", | ||
database: "rest", | ||
size: 10 # The amount of database connections in the pool | ||
|
||
config :joken, | ||
secret_key: "test", | ||
algorithm: :HS256, | ||
json_module: JWT.Module | ||
|
||
config :login, | ||
username: "user", | ||
password: "password" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use Mix.Config | ||
|
||
# For production, we configure the host to read the PORT | ||
# from the system environment. Therefore, you will need | ||
# to set PORT=80 before running your server. | ||
# | ||
# You should also configure the url host to something | ||
# meaningful, we use this information when generating URLs. | ||
# | ||
# Finally, we also include the path to a manifest | ||
# containing the digested version of static files. This | ||
# manifest is generated by the mix phoenix.digest task | ||
# which you typically run after static files are built. | ||
config :restapp, Restapp.Endpoint, | ||
http: [port: {:system, "PORT"}], | ||
url: [host: "example.com"], | ||
cache_static_manifest: "priv/static/manifest.json" | ||
|
||
# ## SSL Support | ||
# | ||
# To get SSL working, you will need to add the `https` key | ||
# to the previous section: | ||
# | ||
# config :restapp, Restapp.Endpoint, | ||
# ... | ||
# https: [port: 443, | ||
# keyfile: System.get_env("SOME_APP_SSL_KEY_PATH"), | ||
# certfile: System.get_env("SOME_APP_SSL_CERT_PATH")] | ||
# | ||
# Where those two env variables point to a file on | ||
# disk for the key and cert. | ||
|
||
# Do not print debug messages in production | ||
config :logger, level: :info | ||
|
||
# ## Using releases | ||
# | ||
# If you are doing OTP releases, you need to instruct Phoenix | ||
# to start the server for all endpoints: | ||
# | ||
# config :phoenix, :serve_endpoints, true | ||
# | ||
# Alternatively, you can configure exactly which server to | ||
# start per endpoint: | ||
# | ||
# config :restapp, Restapp.Endpoint, server: true | ||
# | ||
|
||
# Finally import the config/prod.secret.exs | ||
# which should be versioned separately. | ||
import_config "prod.secret.exs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use Mix.Config | ||
|
||
# We don't run a server during test. If one is required, | ||
# you can enable the server option below. | ||
config :restapp, Restapp.Endpoint, | ||
http: [port: 4001], | ||
server: false | ||
|
||
# Print only warnings and errors during test | ||
config :logger, level: :warn | ||
|
||
# Configure your database | ||
config :restapp, Restapp.Repo, | ||
adapter: Ecto.Adapters.Postgres, | ||
username: "postgres", | ||
password: "postgres", | ||
database: "rest", | ||
size: 1 # Use a single connection for transactional tests | ||
|
||
config :joken, | ||
secret_key: "test", | ||
algorithm: :HS256, | ||
json_module: JWT.Module | ||
|
||
config :login, | ||
username: "user", | ||
password: "password" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
defmodule Restapp do | ||
use Application | ||
|
||
# See http://elixir-lang.org/docs/stable/elixir/Application.html | ||
# for more information on OTP Applications | ||
def start(_type, _args) do | ||
import Supervisor.Spec, warn: false | ||
|
||
children = [ | ||
# Start the endpoint when the application starts | ||
supervisor(Restapp.Endpoint, []), | ||
# Start the Ecto repository | ||
worker(Restapp.Repo, []), | ||
# Here you could define other workers and supervisors as children | ||
# worker(Restapp.Worker, [arg1, arg2, arg3]), | ||
] | ||
|
||
# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html | ||
# for other strategies and supported options | ||
opts = [strategy: :one_for_one, name: Restapp.Supervisor] | ||
Supervisor.start_link(children, opts) | ||
end | ||
|
||
# Tell Phoenix to update the endpoint configuration | ||
# whenever the application is updated. | ||
def config_change(changed, _new, removed) do | ||
Restapp.Endpoint.config_change(changed, removed) | ||
:ok | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
defmodule Restapp.Endpoint do | ||
use Phoenix.Endpoint, otp_app: :restapp | ||
|
||
# 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: :restapp, gzip: false, | ||
only: ~w(css 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.LiveReloader | ||
plug Phoenix.CodeReloader | ||
end | ||
|
||
plug Plug.Logger | ||
|
||
plug Plug.Parsers, | ||
parsers: [:urlencoded, :multipart, :json], | ||
pass: ["*/*"], | ||
json_decoder: Poison | ||
|
||
plug Plug.MethodOverride | ||
plug Plug.Head | ||
|
||
plug Plug.Session, | ||
store: :cookie, | ||
key: "_restapp_key", | ||
signing_salt: "GV7R88So" | ||
|
||
plug :router, Restapp.Router | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defmodule Restapp.Formatter do | ||
|
||
def date(date) do | ||
:io_lib.format("~4..0B-~2..0B-~2..0B", Tuple.to_list(date)) | ||
|> List.flatten | ||
|> to_string | ||
end | ||
|
||
def numeric(number) do | ||
(number.sign*number.coef)/:math.pow(10, -(number.exp)) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
defmodule Restapp.Repo do | ||
use Ecto.Repo, otp_app: :restapp | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
defmodule Restapp.Mixfile do | ||
use Mix.Project | ||
|
||
def project do | ||
[app: :restapp, | ||
version: "0.0.1", | ||
elixir: "~> 1.0", | ||
elixirc_paths: elixirc_paths(Mix.env), | ||
compilers: [:phoenix] ++ Mix.compilers, | ||
build_embedded: Mix.env == :prod, | ||
start_permanent: Mix.env == :prod, | ||
deps: deps] | ||
end | ||
|
||
# Configuration for the OTP application | ||
# | ||
# Type `mix help compile.app` for more information | ||
def application do | ||
[mod: {Restapp, []}, | ||
applications: [:phoenix, :phoenix_html, :cowboy, :logger, | ||
:phoenix_ecto, :postgrex]] | ||
end | ||
|
||
# Specifies which paths to compile per environment | ||
defp elixirc_paths(:test), do: ["lib", "web", "test/support"] | ||
defp elixirc_paths(_), do: ["lib", "web"] | ||
|
||
# Specifies your project dependencies | ||
# | ||
# Type `mix help deps` for examples and options | ||
defp deps do | ||
[{:phoenix, "~> 0.13.1"}, | ||
{:phoenix_ecto, "~> 0.4"}, | ||
{:postgrex, ">= 0.0.0"}, | ||
{:phoenix_html, "~> 1.0"}, | ||
{:phoenix_live_reload, "~> 0.4", only: :dev}, | ||
{:cowboy, "~> 1.0"}, | ||
{:plug_jwt, git: "https://github.com/Dania02525/plug_jwt.git"}] | ||
end | ||
end |
Oops, something went wrong.