From d6a41ed7aac41394e075cecccf1a312c2639a951 Mon Sep 17 00:00:00 2001 From: Martin Gutsch Date: Wed, 15 Jul 2015 18:11:43 +0200 Subject: [PATCH] use Phoenix 0.14, use brunch and bower for asset management --- .bowerrc | 2 +- .gitignore | 10 +- README.md | 57 +- bower.json | 19 +- brunch-config.coffee | 13 + brunch-config.js | 39 - config/config.exs | 12 +- config/dev.exs | 14 +- config/prod.exs | 17 +- config/prod.secret.exs | 9 +- config/test.exs | 14 +- lib/mix/tasks/skeleton.ex | 22 +- lib/{skeleton4.ex => skeleton.ex} | 12 +- lib/skeleton/endpoint.ex | 38 + .../navigation_tree.ex | 6 +- lib/skeleton/repo.ex | 3 + lib/skeleton4/endpoint.ex | 34 - lib/skeleton4/repo.ex | 3 - mix.exs | 33 +- mix.lock | 25 +- package.json | 6 +- .../20150323224152_create-users-table.exs | 2 +- .../20150324232114_create_userrole.exs | 2 +- .../20150324234246_user_userrole_map.exs | 2 +- .../20150324235248_user_userrole_map_pkey.exs | 2 +- ...20150325001351_user_userrole_map_fkeys.exs | 2 +- ...0150325001525_user_userrole_map_fkeys2.exs | 2 +- .../20150401095318_create_admin_user.exs | 4 +- priv/static/css/app.css | 5598 +----- priv/static/css/app.css.map | 2 +- priv/static/images/favicon.ico | Bin 0 -> 1258 bytes priv/static/js/app.js | 14220 +++++++++++++--- priv/static/js/app.js.map | 2 +- priv/static/robots.txt | 5 + skeleton4.komodoproject | 4 - test/controllers/page_controller_test.exs | 8 - test/skeleton4_test.exs | 7 - test/skeleton4_user_helper.exs | 9 - test/support/conn_case.ex | 43 - test/test_helper.exs | 6 - web/controllers/auth_controller.ex | 14 +- web/controllers/page_controller.ex | 6 +- web/controllers/userrole_controller.ex | 7 +- web/models/user.ex | 26 +- web/models/user_userrole_map.ex | 8 +- web/models/userrole.ex | 6 +- web/router.ex | 8 +- web/static/assets/images/favicon.ico | Bin 0 -> 1258 bytes web/static/assets/images/phoenix.png | Bin 0 -> 13900 bytes web/static/assets/robots.txt | 5 + web/static/css/app.scss | 87 +- web/static/css/bootstrap.css | 1 + web/static/js/app.js | 9 +- web/static/vendor/phoenix.js | 1479 +- web/templates/auth/index.html.eex | 32 +- .../{application.html.eex => app.html.eex} | 17 +- web/templates/page/index.html.eex | 48 +- web/views/auth_view.ex | 4 +- web/views/error_view.ex | 8 +- web/views/layout_view.ex | 4 +- web/views/page_view.ex | 4 +- web/views/userrole_view.ex | 4 +- web/web.ex | 22 +- 63 files changed, 13621 insertions(+), 8486 deletions(-) create mode 100644 brunch-config.coffee delete mode 100644 brunch-config.js rename lib/{skeleton4.ex => skeleton.ex} (72%) create mode 100644 lib/skeleton/endpoint.ex rename lib/{skeleton4 => skeleton}/navigation_tree.ex (88%) create mode 100644 lib/skeleton/repo.ex delete mode 100644 lib/skeleton4/endpoint.ex delete mode 100644 lib/skeleton4/repo.ex create mode 100644 priv/static/images/favicon.ico create mode 100644 priv/static/robots.txt delete mode 100644 skeleton4.komodoproject delete mode 100644 test/controllers/page_controller_test.exs delete mode 100644 test/skeleton4_test.exs delete mode 100644 test/skeleton4_user_helper.exs delete mode 100644 test/support/conn_case.ex delete mode 100644 test/test_helper.exs create mode 100644 web/static/assets/images/favicon.ico create mode 100644 web/static/assets/images/phoenix.png create mode 100644 web/static/assets/robots.txt create mode 120000 web/static/css/bootstrap.css rename web/templates/layout/{application.html.eex => app.html.eex} (62%) diff --git a/.bowerrc b/.bowerrc index 228a1b1..44491d3 100644 --- a/.bowerrc +++ b/.bowerrc @@ -1,3 +1,3 @@ { - "directory": "priv/static/bower_components" + "directory": "bower_components" } diff --git a/.gitignore b/.gitignore index b85be79..2ffcbab 100644 --- a/.gitignore +++ b/.gitignore @@ -10,7 +10,10 @@ erl_crash.dump /node_modules # bower-installed stuff -/priv/static/bower_components +/bower_components + +# for real products, uncomment this: +# /config/prod.secret.exs # Since we are building js and css from web/static, # we ignore priv/static/{css,js}. You may want to @@ -18,3 +21,8 @@ erl_crash.dump /priv/static/css /priv/static/js +# used by brunch/sass +/web/static/css/.sass-cache/ + +# When I use Komodo IDE +/phoenix-skeleton.komodoproject diff --git a/README.md b/README.md index 3688d3e..66dd874 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,18 @@ # Phoenix-Sekeleton -Master branch currently supports Phoenix v0.11.0 +Master branch currently supports Phoenix v0.14.0 -# Coming soon! +# What's new in 0.14.0? -Yay! Nothing much happened here for the last few weeks. +- Support fort phoenix 0.14 +- integrate brunch to handle assets +- improved documentation: added Postgre commands for fast setup of development database +- deployment guide: systemd files, running skeleton app as unprivileged user +- return to old "Skeleton" namespace for everything. -- Next release will support phoenix 0.13.1 and integrate more nicely with brunch or webpack (still cannot decide on what to use). -- Have navigation as Elixir Agent +# Coming Soon! + +- Have navigation as Elixir Agent (current implementation is way too quick'n'dirty. ## Feature Roadmap - [x] Basic routing @@ -17,7 +22,7 @@ Yay! Nothing much happened here for the last few weeks. - [x] basic Login page - [x] Authorization - [x] Userroles Ecto defintion - - [x] Skeleton4.Helper.roles_of( user ) returns string_ + - [x] Skeleton.Helper.roles_of( user ) returns string_ - [ ] Means to check for roles in controllers/actions - [x] Memcached Session store - this is done via the plug gutschilla/plug-session-memcached @@ -32,11 +37,22 @@ Yay! Nothing much happened here for the last few weeks. - [ ] CouchDB connector - [ ] Hello2.Admin.RESTCrud controller - [ ] validate/save - - [ ] uploads - - [ ] Image resizing + - [ ] uploads () + - [ ] Image resizing (will be done via dragonfly-server [using convert/ImageMagick]) - [ ] TinySite CMS - [ ] Create mix task to boostrap components instead of copy/paste+search/replace +# Installation + +1. Install Elixir dependencies with `mix deps.get` +2. Install npm dependencies for ranch with `npm install` + - on Debuan/Ubuntu "deb.nodesource.com" works great +3. Install bower dependecies with `bower install` +4. Check system dependencies, run postgre dev script below +5. Start Phoenix endpoint with `mix phoenix.server` + +Now you can visit `localhost:4000` from your browser. + # System dependencies - Erlang/Elixir, of course ;-) @@ -49,12 +65,23 @@ Yay! Nothing much happened here for the last few weeks. - bower package manager - tested with 1.3.12 -# Installation +## Create development database -1. Install Elixir dependencies with `mix deps.get` -2. Install npm dependencies for ranch with `npm install` - - on Debuan/Ubuntu "deb.nodesource.com" works great -3. Install bower dependecies with `bower install` -2. Start Phoenix endpoint with `mix phoenix.server` +This will create a database "skeleton_dev" owned by "skeleton_dev_user" with password "skeleton_dev_pass". Tune this to yout needs. -Now you can visit `localhost:4000` from your browser. +``` +CREATE ROLE skeleton_dev_user LOGIN + ENCRYPTED PASSWORD 'md5465fc2608ab7a4ebe4eaa198e63b85a7' + NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION; +CREATE DATABASE skeleton_dev + WITH OWNER = skeleton_dev_user + ENCODING = 'UTF8' + TABLESPACE = pg_default + LC_COLLATE = 'de_DE.UTF-8' + LC_CTYPE = 'de_DE.UTF-8' + CONNECTION LIMIT = -1; +``` + +# Feedback + +Is more than welcome. I am still learing this whole Elixir and Erlang/OTP thing so be rude, be harsh but be helpful. diff --git a/bower.json b/bower.json index 05ff2ba..e7f6bfe 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,12 @@ { "name": "phoenix-skeleton4", - "version": "0.0.1", + "private": true, + "ignore": [ + "**/.*", + "node_modules", + "components" + ], + "version": "0.14.0", "authors": [ "Martin Gutsch " ], @@ -14,15 +20,8 @@ "postgre" ], "license": "MIT", - "ignore": [ - "**/.*", - "node_modules", - "bower_components", - "test", - "tests" - ], "dependencies": { - "bootstrap": "~3.3.4", - "jquery": "~2.1.3" + "jquery": "1.*", + "bootstrap": "~3.3.5" } } diff --git a/brunch-config.coffee b/brunch-config.coffee new file mode 100644 index 0000000..6e4cf0c --- /dev/null +++ b/brunch-config.coffee @@ -0,0 +1,13 @@ +module.exports = config: + files: + javascripts: joinTo: 'js/app.js' + stylesheets: joinTo: 'css/app.css' + paths: + watched: ["web/static", "test/static"] + public: "priv/static" + plugins: + babel: + ignore: [ + /^(web\/static\/vendor)/ + /^(bower_components)/ + ] diff --git a/brunch-config.js b/brunch-config.js deleted file mode 100644 index ec5da28..0000000 --- a/brunch-config.js +++ /dev/null @@ -1,39 +0,0 @@ -exports.config = { - // See http://brunch.io/#documentation for docs. - files: { - javascripts: { - joinTo: 'js/app.js' - // 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: { - ES6to5: { - // Do not use ES6 compiler in vendor code - ignore: [/^(web\/static\/vendor)/] - } - } -}; diff --git a/config/config.exs b/config/config.exs index 15c8eb5..2380f26 100644 --- a/config/config.exs +++ b/config/config.exs @@ -6,19 +6,19 @@ use Mix.Config # Configures the endpoint -config :skeleton4, Skeleton4.Endpoint, +config :skeleton, Skeleton.Endpoint, url: [host: "localhost"], - root: Path.expand("..", __DIR__), - secret_key_base: "Xr16fZ4OwibbjqGi8gy8ectpRuGuPpECjCbG9ChpCfqE0W1Y3kJrDUmRNlWSFeQ3", - debug_errors: false, - pubsub: [name: Skeleton4.PubSub, + root: Path.dirname(__DIR__), + secret_key_base: "wzTwKg+PNLR7M595wCG9aNDXgD46hTbrKrS714zbjiL/Jh6CwLai1p5cHATxVeUE", + render_errors: [default_format: "html"], + pubsub: [name: Skeleton.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" diff --git a/config/dev.exs b/config/dev.exs index 27769b3..a774e64 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -6,7 +6,7 @@ use Mix.Config # 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 :skeleton4, Skeleton4.Endpoint, +config :skeleton, Skeleton.Endpoint, http: [port: 4000], debug_errors: true, code_reloader: true, @@ -14,7 +14,7 @@ config :skeleton4, Skeleton4.Endpoint, watchers: [node: ["node_modules/brunch/bin/brunch", "watch"]] # Watch static and templates for browser reloading. -config :skeleton4, Skeleton4.Endpoint, +config :skeleton, Skeleton.Endpoint, live_reload: [ patterns: [ ~r{priv/static/.*(js|css|png|jpeg|jpg|gif)$}, @@ -27,8 +27,10 @@ config :skeleton4, Skeleton4.Endpoint, config :logger, :console, format: "[$level] $message\n" # Configure your database -config :skeleton4, Skeleton4.Repo, +config :skeleton, Skeleton.Repo, adapter: Ecto.Adapters.Postgres, - username: "skeleton4_user", - password: "skeleton4_pass", - database: "skeleton4" + username: "skeleton_dev_user", + password: "skeleton_dev_pass", + database: "skeleton_dev", + hostname: "192.168.0.240", + size: 10 # The amount of database connections in the pool diff --git a/config/prod.exs b/config/prod.exs index 5df22fa..6d99da5 100644 --- a/config/prod.exs +++ b/config/prod.exs @@ -6,17 +6,24 @@ use Mix.Config # # You should also configure the url host to something # meaningful, we use this information when generating URLs. -config :skeleton4, Skeleton4.Endpoint, +# +# 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 :skeleton, Skeleton.Endpoint, http: [port: {:system, "PORT"}], - url: [host: "example.com"] + url: [host: "example.com", port: 80], + 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: +# to the previous section, and set your `:url` port to 443 # -# config:skeleton4, Skeleton4.Endpoint, +# config :skeleton, Skeleton.Endpoint, # ... +# url: [host: "example.com", port: 443], # https: [port: 443, # keyfile: System.get_env("SOME_APP_SSL_KEY_PATH"), # certfile: System.get_env("SOME_APP_SSL_CERT_PATH")] @@ -37,7 +44,7 @@ config :logger, level: :info # Alternatively, you can configure exactly which server to # start per endpoint: # -# config :skeleton4, Skeleton4.Endpoint, server: true +# config :skeleton, Skeleton.Endpoint, server: true # # Finally import the config/prod.secret.exs diff --git a/config/prod.secret.exs b/config/prod.secret.exs index df25baa..4da9add 100644 --- a/config/prod.secret.exs +++ b/config/prod.secret.exs @@ -3,12 +3,13 @@ use Mix.Config # In this file, we keep production configuration that # you likely want to automate and keep it away from # your version control system. -config :skeleton4, Skeleton4.Endpoint, - secret_key_base: "bzaui+SHRFxYTfdej43TqcriqHel6HzJvXET5+d8Az8ihVtvQsCy5bP0GPp/mgUA" +config :skeleton, Skeleton.Endpoint, + secret_key_base: "k40PLP1kAhQHAjslbMA8c/9dp/1FptVYxH1fYVjT3YsqR/uN8HhUU+kj8WeXnZIr" # Configure your database -config :skeleton4, Skeleton4.Repo, +config :skeleton, Skeleton.Repo, adapter: Ecto.Adapters.Postgres, username: "postgres", password: "postgres", - database: "skeleton4_prod" + database: "skeleton_prod", + size: 20 # The amount of database connections in the pool diff --git a/config/test.exs b/config/test.exs index 556b99c..7fb0be1 100644 --- a/config/test.exs +++ b/config/test.exs @@ -2,7 +2,7 @@ use Mix.Config # We don't run a server during test. If one is required, # you can enable the server option below. -config :skeleton4, Skeleton4.Endpoint, +config :skeleton, Skeleton.Endpoint, http: [port: 4001], server: false @@ -10,10 +10,10 @@ config :skeleton4, Skeleton4.Endpoint, config :logger, level: :warn # Configure your database -config :skeleton4, Skeleton4.Repo, +config :skeleton, Skeleton.Repo, adapter: Ecto.Adapters.Postgres, - username: "skeleton4_user", - password: "skeleton4_pass", - database: "skeleton4", - size: 1, - max_overflow: false + username: "postgres", + password: "postgres", + database: "skeleton_test", + pool: Ecto.Adapters.SQL.Sandbox, # Use a sandbox for transactional testing + size: 1 diff --git a/lib/mix/tasks/skeleton.ex b/lib/mix/tasks/skeleton.ex index fda6fba..15529d6 100644 --- a/lib/mix/tasks/skeleton.ex +++ b/lib/mix/tasks/skeleton.ex @@ -8,35 +8,35 @@ defmodule Mix.Tasks.Skeleton do def run(_) do - Skeleton4.Repo.start_link + Skeleton.Repo.start_link users = 1..1000 - |> Enum.map fn( num ) -> + |> Enum.map fn( _num ) -> username = "U-" <> Misc.Random.string IO.puts username - user = %Skeleton4.User{ username: username } - Skeleton4.Repo.insert user + user = %Skeleton.User{ username: username } + Skeleton.Repo.insert user end roles = 1..100 - |> Enum.map fn( num ) -> + |> Enum.map fn( _num ) -> rolename = "R-" <> Misc.Random.string IO.puts rolename - role = %Skeleton4.Userrole{ name: rolename } - Skeleton4.Repo.insert role + role = %Skeleton.Userrole{ name: rolename } + Skeleton.Repo.insert role end users |> Enum.each fn( user ) -> # link 1 to 10 roles to this user 0..(:random.uniform 10) - |> Enum.each fn( num ) -> + |> Enum.each fn( _num ) -> role = roles |> Enum.at :random.uniform( length( roles ) - 1 ) try do - map = %Skeleton4.UserUserroleMap{ user_id: user.id, userrole_id: role.id } - Skeleton4.Repo.insert map + map = %Skeleton.UserUserroleMap{ user_id: user.id, userrole_id: role.id } + Skeleton.Repo.insert map rescue - e in Postgrex.Error -> nil + _e in Postgrex.Error -> nil end end end diff --git a/lib/skeleton4.ex b/lib/skeleton.ex similarity index 72% rename from lib/skeleton4.ex rename to lib/skeleton.ex index d613d94..98bdfa5 100644 --- a/lib/skeleton4.ex +++ b/lib/skeleton.ex @@ -1,4 +1,4 @@ -defmodule Skeleton4 do +defmodule Skeleton do use Application # See http://elixir-lang.org/docs/stable/elixir/Application.html @@ -8,23 +8,23 @@ defmodule Skeleton4 do children = [ # Start the endpoint when the application starts - supervisor(Skeleton4.Endpoint, []), + supervisor(Skeleton.Endpoint, []), # Start the Ecto repository - worker(Skeleton4.Repo, []), + worker(Skeleton.Repo, []), # Here you could define other workers and supervisors as children - # worker(Skeleton4.Worker, [arg1, arg2, arg3]), + # worker(Skeleton.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: Skeleton4.Supervisor] + opts = [strategy: :one_for_one, name: Skeleton.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 - Skeleton4.Endpoint.config_change(changed, removed) + Skeleton.Endpoint.config_change(changed, removed) :ok end end diff --git a/lib/skeleton/endpoint.ex b/lib/skeleton/endpoint.ex new file mode 100644 index 0000000..c07b401 --- /dev/null +++ b/lib/skeleton/endpoint.ex @@ -0,0 +1,38 @@ +defmodule Skeleton.Endpoint do + use Phoenix.Endpoint, otp_app: :skeleton + + # 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: :skeleton, 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.RequestId + plug Plug.Logger + + plug Plug.Parsers, + parsers: [:urlencoded, :multipart, :json], + pass: ["*/*"], + json_decoder: Poison + + plug Plug.MethodOverride + plug Plug.Head + + plug Plug.Session, + key: "_skeleton_key", + signing_salt: "SBRvpcSC", + # store: :cookie, + store: :memcached, + table: :memcached_sessions + + plug :router, Skeleton.Router +end diff --git a/lib/skeleton4/navigation_tree.ex b/lib/skeleton/navigation_tree.ex similarity index 88% rename from lib/skeleton4/navigation_tree.ex rename to lib/skeleton/navigation_tree.ex index f279ec9..6aba7aa 100644 --- a/lib/skeleton4/navigation_tree.ex +++ b/lib/skeleton/navigation_tree.ex @@ -1,4 +1,4 @@ -defmodule Skeleton4.NavigationTree do +defmodule Skeleton.NavigationTree do # TODO: auto-gerenerate this as macro instead of having this boilerplate code @@ -49,7 +49,7 @@ defmodule Skeleton4.NavigationTree do end def allowed_for_user_id( user_id ) do - roles = Skeleton4.User.Helper.roles_of( Skeleton4.Repo.get Skeleton4.User, user_id ) + roles = Skeleton.User.Helper.roles_of( Skeleton.Repo.get Skeleton.User, user_id ) allowed_tree roles end @@ -60,7 +60,7 @@ defmodule Skeleton4.NavigationTree do def as_html( item ) when is_map( item) do case item do - %{ ul_start: true } -> ~s(