|
| 1 | +# next-postgres |
| 2 | + |
| 3 | +This is a minimal example of a forum web application with posts and comments. |
| 4 | + |
| 5 | +Uses: |
| 6 | + |
| 7 | +- [NextJS + Custom Express](https://github.com/zeit/next.js/) |
| 8 | +- [Postgres](https://www.postgresql.org/) |
| 9 | +- [Styled-JSX](https://github.com/zeit/styled-jsx) |
| 10 | +- [Passport with local authentication](http://passportjs.org/) |
| 11 | +- [Heroku](https://www.heroku.com) |
| 12 | +- [Redux](http://redux.js.org/) + [Higher-Order Components](https://facebook.github.io/react/docs/higher-order-components.html) |
| 13 | +- [Babel + Stage-1 + Stage-2 + Stage-3](https://babeljs.io/) |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +Getting setup should only take 5 minutes! |
| 18 | + |
| 19 | +Useful for: |
| 20 | + |
| 21 | +- Building 100% JavaScript applications with example code. |
| 22 | +- Testing out concepts before bringing them over to real work. |
| 23 | +- Getting [server side rendering](https://zeit.co/blog/next2) right. |
| 24 | +- Looking like you have cheat codes at a Hackathon. |
| 25 | + |
| 26 | +You can [view the demo on heroku](https://guarded-coast-67601.herokuapp.com/). Below are steps to deploy your own. |
| 27 | + |
| 28 | +Coming soon: a [React Native](https://facebook.github.io/react-native/) application built with [Expo](https://expo.io) that communicates to an API served from here. |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +## Prerequisites |
| 33 | + |
| 34 | +- I use [Homebrew](https://brew.sh/). Using it makes it easier to follow these steps. |
| 35 | +- Install Postgres: `brew install postgres`. |
| 36 | +- Install [Node 6+](https://nodejs.org/en/): `brew install node`. |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +## Quick newbies guide to Postgres |
| 41 | + |
| 42 | +- On OSX, to run Postgres: |
| 43 | + |
| 44 | +```sh |
| 45 | +pg_ctl -D /usr/local/var/postgres start |
| 46 | +``` |
| 47 | + |
| 48 | +- There is a file named `./config.js`. |
| 49 | +- That file points to a local database named `testdb`. With the username and password as `test`. |
| 50 | + |
| 51 | +### First time Postgres instructions. |
| 52 | + |
| 53 | +```sh |
| 54 | +# Enter Postgres console |
| 55 | +psql postgres |
| 56 | + |
| 57 | +# Create a new user for yourself |
| 58 | +CREATE ROLE yourname WITH LOGIN PASSWORD 'yourname'; |
| 59 | + |
| 60 | +# Allow yourself to create databases |
| 61 | +ALTER ROLE yourname CREATEDB; |
| 62 | + |
| 63 | +# Exit Postgres console |
| 64 | +\q |
| 65 | + |
| 66 | +# Log in as your new user. |
| 67 | +psql postgres -U yourname |
| 68 | + |
| 69 | +# Create a database named: testdb. |
| 70 | +# If you change this, update config.js |
| 71 | +CREATE DATABASE testdb; |
| 72 | + |
| 73 | +# Give your self privileges |
| 74 | +GRANT ALL PRIVILEGES ON DATABASE testdb TO yourname; |
| 75 | + |
| 76 | +# List all of your databases |
| 77 | +\list |
| 78 | + |
| 79 | +# Connect to your newly created DB as a test |
| 80 | +\connect testdb |
| 81 | + |
| 82 | +# Exit Postgres console |
| 83 | +\q |
| 84 | +``` |
| 85 | + |
| 86 | +You wont return here unless you drop tables. You can also use a GUI if you like. |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | +## Running the app locally |
| 91 | + |
| 92 | +While in the root directory of the repo: |
| 93 | + |
| 94 | +```sh |
| 95 | +npm install |
| 96 | +npm install -g sequelize-cli |
| 97 | +sequelize db:migrate |
| 98 | +npm run dev |
| 99 | +``` |
| 100 | + |
| 101 | +You can now visit `localhost:8000` in the browser of your choice. |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | +## Setup Heroku |
| 106 | + |
| 107 | +Install Heroku. |
| 108 | + |
| 109 | +```sh |
| 110 | +npm install -g heroku-cli |
| 111 | +heroku login |
| 112 | +heroku create |
| 113 | +``` |
| 114 | + |
| 115 | +Heroku will give you a unique address, like ours: `guarded-coast-67601.herokuapp.com`. |
| 116 | + |
| 117 | +Already have a heroku app to deploy to? |
| 118 | + |
| 119 | +``` |
| 120 | +heroku git:remote -a name-of-your-heroku-app |
| 121 | +``` |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | +## Setup Postgres and config vars |
| 126 | + |
| 127 | +Go to https://data.heroku.com, add a datastore, pick Postgres. |
| 128 | + |
| 129 | +You will receive `database`, `host`, `password`, `port`, and `username` values. Here is how you set them: |
| 130 | + |
| 131 | +```sh |
| 132 | +# Set variables |
| 133 | +heroku config:set PRODUCTION_DATABASE=NAME_PROVIDED_FROM_HEROKU |
| 134 | +heroku config:set PRODUCTION_HOST=HOST_PROVIDED_FROM_HEROKU |
| 135 | +heroku config:set PRODUCTION_PASSWORD=PASSWORD_PROVIDED_FROM_HEROKU |
| 136 | +heroku config:set PRODUCTION_PORT=PORT_PROVIDED_FROM_HEROKU |
| 137 | +heroku config:set PRODUCTION_USERNAME=USERNAME_PROVIDED_FROM_HEROKU |
| 138 | + |
| 139 | +# See all of your variables |
| 140 | +heroku config |
| 141 | +``` |
| 142 | + |
| 143 | +Set a secret for [cookie-session](https://github.com/expressjs/cookie-session): |
| 144 | + |
| 145 | +```sh |
| 146 | +heroku config:set PRODUCTION_SECRET=PICK_A_SECRET |
| 147 | +``` |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | +## Deploy to Heroku |
| 152 | + |
| 153 | +```sh |
| 154 | +git push heroku master |
| 155 | +``` |
| 156 | + |
| 157 | +append `--force` if necessary. |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | +## Questions? |
| 162 | + |
| 163 | +Feel free to slang any feels to [@meanjim](https://twitter.com/meanjim). |
0 commit comments