From e00e2820e7d460b464965422aab284c6d3b56c70 Mon Sep 17 00:00:00 2001
From: Niall Sheridan <nsheridan@gmail.com>
Date: Sun, 22 May 2016 20:46:09 +0100
Subject: [PATCH] Make template directory configurable

---
 Dockerfile                   | 2 ++
 README.md                    | 7 +++++++
 cmd/cashierd/main.go         | 3 ++-
 exampleconfig.json           | 3 ++-
 server/config/config.go      | 1 +
 server/config/config_test.go | 1 +
 testdata/config.go           | 3 ++-
 7 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/Dockerfile b/Dockerfile
index 944fffdd..462af4d7 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -3,4 +3,6 @@ FROM golang:1.6
 ADD . /go/src/github.com/nsheridan/cashier
 RUN go install github.com/nsheridan/cashier/cmd/cashierd
 
+ONBUILD COPY . /cashier
+WORKDIR /cashier
 ENTRYPOINT /go/bin/cashierd
diff --git a/README.md b/README.md
index 512e2987..e62df254 100644
--- a/README.md
+++ b/README.md
@@ -53,6 +53,13 @@ go get github.com/cashier/cmd/...
 2. Create a signing key with `ssh-keygen` and a [config.json](exampleconfig.json)
 3. Run the cashier server with `cashierd` and the cli with `cashier`.
 
+## Using docker
+1. Create a signing key with `ssh-keygen` and a [config.json](exampleconfig.json)
+2. Run
+```
+docker run -it --rm -p 10000:10000 --name cashier -v $(pwd):/cashier nsheridan/cashier
+```
+
 # Configuration
 Configuration is divided into three sections: `server`, `auth`, and `ssh`.
 
diff --git a/cmd/cashierd/main.go b/cmd/cashierd/main.go
index bc460da5..e482ddec 100644
--- a/cmd/cashierd/main.go
+++ b/cmd/cashierd/main.go
@@ -13,6 +13,7 @@ import (
 	"log"
 	"net/http"
 	"os"
+	"path"
 	"strings"
 	"time"
 
@@ -221,7 +222,7 @@ func main() {
 	ctx := &appContext{
 		cookiestore:  sessions.NewCookieStore([]byte(config.Server.CookieSecret)),
 		authprovider: authprovider,
-		views:        template.Must(template.ParseGlob("templates/*")),
+		views:        template.Must(template.ParseGlob(path.Join(config.Server.TemplateDir, "*"))),
 		sshKeySigner: signer,
 	}
 	ctx.cookiestore.Options = &sessions.Options{
diff --git a/exampleconfig.json b/exampleconfig.json
index 683304e1..eeb1453a 100644
--- a/exampleconfig.json
+++ b/exampleconfig.json
@@ -4,7 +4,8 @@
     "tls_key": "server.key",
     "tls_cert": "server.crt",
     "port": 443,
-    "cookie_secret": "supersecret"
+    "cookie_secret": "supersecret",
+    "template_dir": "/go/src/github.com/nsheridan/cashier/templates"
   },
   "auth": {
     "provider": "google",
diff --git a/server/config/config.go b/server/config/config.go
index 7598f0a9..8922ff63 100644
--- a/server/config/config.go
+++ b/server/config/config.go
@@ -20,6 +20,7 @@ type Server struct {
 	TLSCert      string `mapstructure:"tls_cert"`
 	Port         int    `mapstructure:"port"`
 	CookieSecret string `mapstructure:"cookie_secret"`
+	TemplateDir  string `mapstructure:"template_dir"`
 }
 
 // Auth holds the configuration specific to the OAuth provider.
diff --git a/server/config/config_test.go b/server/config/config_test.go
index 6baf76d2..e528e7de 100644
--- a/server/config/config_test.go
+++ b/server/config/config_test.go
@@ -22,6 +22,7 @@ func TestServerConfig(t *testing.T) {
 	a.Equal(server.TLSCert, "server.crt")
 	a.Equal(server.Port, 443)
 	a.Equal(server.CookieSecret, "supersecret")
+	a.Equal(server.TemplateDir, "templates")
 }
 
 func TestAuthConfig(t *testing.T) {
diff --git a/testdata/config.go b/testdata/config.go
index bb2f5111..a64ebd55 100644
--- a/testdata/config.go
+++ b/testdata/config.go
@@ -6,7 +6,8 @@ var ServerConfig = []byte(`{
 		"tls_key": "server.key",
 		"tls_cert": "server.crt",
 		"port": 443,
-		"cookie_secret": "supersecret"
+		"cookie_secret": "supersecret",
+		"template_dir": "templates"
 	}
 }`)
 
-- 
GitLab