Reason for being:
This prototype came out of my personal need to play with PouchDB and CouchDB where I wanted to explore what it would take to allow each and every single user who registered with the applicaiton to acquire their own database (some bucket to store and syncronize their data across devices). A place where each user (and only that user) has permission to their data in the database (except of course the server-admin user).
This model fits nicely with many types of applications where we want data to be syncronized across devices for a user and the PouchDB/CouchDB combination seemed like it'd be a good fit. So I spent some time organizing this prototype to prove it out and see where I could take it.
I think this concept and infrastructure can be used in may other types of apps and I hope you find it useful as a reference. The current end goal of this prototype is to take my learnings from it and use in a side project or other app ideas I have cooking.
This example is of-course very simplified (or at least I tried), but it shows a real-world client/server configuration with some interesting technologies.
If you discover any potential security holes or other points of interest that you think would make this tutorial/sample easier to learn/work with, please open a github issue or even better a pull request.
What they don't tell you about CouchDB...
- Quick Start
- Some ToDos
- The Tech
- Get The Codes
- The Server
- The Front-End Client
You'll need to have some 3rd party applicaitons installed to play with this sample.
git clone https://github.com/staxmanade/sample-pouch-couch-databaseperuser.git cd sample-pouch-couch-databaseperuser/server/app npm install cd ../
postinstallscript that also uses
jspm installin the
./servers/app/client/folder to install client app dependencies.
Then edit the
./server/.env file with necessary configuration - such as
COUCHDB_PASSWORD. You can learn more about passing environment variables into docker on my blog.
Also update the
./server/couchdb/local.ini and look for
TODO: comments - update accordingly.
Once it's configured, then you can:
Read the rest of this Readme to get a better idea of all the components in here...
While the prototype is currently working, there are some interesting TODO's that I'd like to accomplish (including here instead of in the github issues for visibility).
- Show how to deploy this to Digital Ocean
This project was pieced together with an assortment of the following tech.
Get the Codes
- Clone the repo
git clone https://github.com/staxmanade/sample-pouch-couch-databaseperuser.git
- CD into
cd ./servers/app && npm install
postinstallscript that also uses
jspm installin the
./servers/app/client/folder so to also install client app dependencies.
cd ../(into the
./servers/folder) where the
- In the web browser hit you're docker instance on port
http://localhost:3000should show you a basic test page that has the register/login form elements. Note use
What it Does
In this case the
server is several docker instances.
- The node web app running that has Superlogin auth routes and renders the React U.I. built in the
- CouchDB is run another docker instance
- Redis is also run in a docker instance which handles superlogin auth session state.
What's Different with the CouchDB Configuration
This may be specific to "my" use-case, but I made certain changes to the default couchdb configuration
server/config/couchdb/local.ini which help to enable this scenario work (and be secure).
- Security: set
require_valid_user = truewhich doesn't allow any access to the couchdb database without a valid auth token. Auth tokens are granted through the
superloginportion of the node webapp.
To allow the site to work we need to enable the proper CORS. The
server/config/couchdb/local.inifile has already been updated. This configuration was changed by pouchdb/add-cors-to-couchdb so you can review how this tool work to see what changes it makes to the default config to enable CORS support.
The server is composed of 3 docker containers.
The nodejs web app. This is is serving two purposes.
1) To host the static client reactjs front-end client which uses the 2) Superlogin auth api's to complete user registration/login/etc
The second is the CouchDB server itself. Once a user has registered and logged in with SuperLogin they can then use the auth token to access their couchdb database...
The third is a Redis database that the SuperLogin uses inside the node/web app to maintain user sessions.
If you need to snoop around inside the container
docker-compose ps to list the images running and then you can use
docker exec -i -t <docker container name> /bin/bash to get into a container and snoop around.
The client is a simple static site. To get it running (but there are a few steps):
- Get the latest version of JSPM
npm install -g jspm@beta
Now setup the client project.
- Once you have the Server Setup
- Update you're hosts file so that
couchdbwill resolve to the IP address of the docker machine hosting couchdb. If you're using
docker-machine ip default
Then edit you're hosts file (on linux/mac edit the
/etc/hosts file to include the following
But the sample is slow to load...
For better local dev performance, if you want JSPM to bundle the JS and load faster try running
cd ./server/app/client && jspm bundle app -wid(and keep it running)
This will watch for changes to the client js, rebuild the
build.jsfile whenever you make changes to the project. When you reload the page it's much faster...
I ran into an issue where the redis database could not write to the
./server/data/redis/ folder which wouldn't allow sessions to be stored across server reboots.
From the root of the project try running these commands to allow the container to write to the mapped docker volume defined in docker-compose.yaml.
mkdir -p ./server/data/redis chmod a+x ./server/data/redis