Skip to content

Instantly share code, notes, and snippets.

@knubie
Last active August 29, 2015 14:07
Show Gist options
  • Save knubie/4a27a400728793e45978 to your computer and use it in GitHub Desktop.
Save knubie/4a27a400728793e45978 to your computer and use it in GitHub Desktop.
Implementing Oauth with Lapis (Lua OpenResty, Nginx, Postgres) and Docker

Implementing OAuth with Lapis (Lua, OpenResty, Nginx, Postgres) and Docker

Part 1 – Setting up docker with fig

Install docker

https://docs.docker.com/installation/

On OS X you can use Boot2Docker, or use Vagrant to spin up an empty Ubuntu machine (or whatever OS your production environment will run on).

# Vagrantfile
Vagrant.configure("2") do |config|
	config.vm.box = "ubuntu/trusty64"
	config.vm.provision "docker"
	config.vm.network :forwarded_port, host: 8080, guest: 8080
end

Install Fig

http://www.fig.sh/install.html

Fig allows you to write a configuration file to manage the building/running/linking of your docker images/containers. We'll be using this fig.yml configuration for our project.

# fig.yml
web:
  build: .
  command: lapis server
  ports:
   - "8080:8080"
  volumes:
   - .:/code
  links:
   - postgres
redis:
  image: postgres

Create a new Lapis project

Run lapis new to create the basic configuration/applications files.

$ lapis new
wrote   nginx.conf
wrote   mime.types
wrote   app.moon

Set up Lapis' config.moon

We'll need to tell Lapis where/how to connect to the database. Since we're using the default port (5432), and user (postgres), we don't need to explicitly declare them in config.moon, but we will need to declare the database (postgres) which is the default database created by postgres.

-- config.moon
config = require "lapis.config"

config {"development", "production"}, ->
  postgres ->
    backend "pgmoon"
    host "postgres"
    database "postgres"

When linking one container to another, Docker automatically creates an entry in the hosts file of the main container pointing to the IP of the linked container. This allows us to use "postgres" as the database host in the config.moon file instead of its IP. The problem is that nginx's resolver does not use the /etc/hosts file. So we'll need to figure out some other way to get the database IP, which I haven't figured out yet, stay tuned.

Compile moonscript files

Run moonc -w . to watch the directory for changes and compile .moon files to .lua

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment