Bläddra i källkod

Documenting build and running in production mode

theenglishway (time) 4 år sedan
förälder
incheckning
03d8281d84
2 ändrade filer med 30 tillägg och 3 borttagningar
  1. 20 2
      README.md
  2. 10 1
      config/runtime.exs

+ 20 - 2
README.md

@@ -5,7 +5,7 @@ of mine in the aspects of build, integration and deployment.
 
 ## Goals
 
-* [] Document build
+* [x] Document build
 * [] Implement and document cross-build
 * [] Install the project as a systemd service
 * [] Handle the current version via a git tag
@@ -13,6 +13,14 @@ of mine in the aspects of build, integration and deployment.
 
 ## Preconditions
 
+### Update Elixir's configuration to the best practices.
+
+For some reason the default configuration of newly-generated Phoenix projects is
+completely outdated, e.g. using `Mix.Config` when the standard is now `Config`.
+
+It is advised above all to update the configuration, and try to match the
+[master branch](https://github.com/phoenixframework/phoenix/tree/master/installer/templates/phx_single/config) 
+
 ### PostgreSQL role
 
 In `dev` as well as in `prod` modes, the role used in `config/#{mode}.exs` must 
@@ -38,4 +46,14 @@ To start the server in dev mode :
 * Start the server : `mix phx.server` or `iex -S mix phx.server` to run within a shell
 
 The server can now be accessed on [`localhost:4000`](http://localhost:4000).
- 
+
+### In production mode
+
+In production mode some configuration is extracted from the environment.
+
+* Generate a secret key using `mix phx.gen.secret`
+* Install dependencies with `mix deps.get --only prod`
+* Compile the application `MIX_ENV=prod mix compile`
+* Compile static assets into `priv/static` : `npm run deploy --prefix ./assets`
+* Compress and digest static assets `mix phx.digest`
+* Start the server : `MIX_ENV=prod DATABASE_URL=db-url SECRET_KEY_BASE=secret_key PORT=port mix phx.server`

+ 10 - 1
config/runtime.exs

@@ -21,14 +21,23 @@ if config_env() == :prod do
       For example: ecto://USER:PASS@HOST/DATABASE
       """
 
+  port =
+    System.get_env("PORT") ||
+      raise "environment variable PORT is missing."
+
+  host =
+    System.get_env("HOST") ||
+      raise "environment variable HOST is missing."
+
   config :toy, ToyWeb.Endpoint,
+    url: [host: host, port: port],
     http: [
       # Enable IPv6 and bind on all interfaces.
       # Set it to  {0, 0, 0, 0, 0, 0, 0, 1} for local network only access.
       # See the documentation on https://hexdocs.pm/plug_cowboy/Plug.Cowboy.html
       # for details about using IPv6 vs IPv4 and loopback vs public addresses.
       ip: {0, 0, 0, 0, 0, 0, 0, 0},
-      port: String.to_integer(System.get_env("PORT") || "4000")
+      port: port
     ],
     secret_key_base: secret_key_base