Repository: nuveo/prest
Pull request #117 (2017-02-26 17:49:38)

enable add custom middleware
This PR enable add custom middleware on pREST middlewares stack 

Signed-off-by: Felipe Oliveira <felipeweb.programador@gmail.com>


codecov[bot]: # [Codecov](https://codecov.io/gh/nuveo/prest/pull/117?src=pr&el=h1) Report
> Merging [#117](https://codecov.io/gh/nuveo/prest/pull/117?src=pr&el=desc) into [master](https://codecov.io/gh/nuveo/prest/commit/0879cbef401b9d4b8b9457c7adfe088acc3d5ea9?src=pr&el=desc) will **increase** coverage by `0.07%`.
> The diff coverage is `100%`.


```diff
@@            Coverage Diff             @@
##           master     #117      +/-   ##
==========================================
+ Coverage   89.64%   89.71%   +0.07%     
==========================================
  Files           9       10       +1     
  Lines         811      817       +6     
==========================================
+ Hits          727      733       +6     
  Misses         54       54              
  Partials       30       30
```


| [Impacted Files](https://codecov.io/gh/nuveo/prest/pull/117?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [config/middlewares.go](https://codecov.io/gh/nuveo/prest/compare/0879cbef401b9d4b8b9457c7adfe088acc3d5ea9...b2f1a7b14c8a10998a94eb57b5dd3124bcda705c?src=pr&el=tree#diff-Y29uZmlnL21pZGRsZXdhcmVzLmdv) | `100% <100%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/nuveo/prest/pull/117?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/nuveo/prest/pull/117?src=pr&el=footer). Last update [0879cbe...b2f1a7b](https://codecov.io/gh/nuveo/prest/compare/0879cbef401b9d4b8b9457c7adfe088acc3d5ea9...b2f1a7b14c8a10998a94eb57b5dd3124bcda705c?el=footer&src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

Patch:
diff --git a/.travis.yml b/.travis.yml
index c0c689d..3bc941d 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -6,9 +6,14 @@ os:
     - linux
 
 go:
-    - 1.6.x
-    - 1.7.x
-    - 1.8rc1
+  - 1.6
+  - 1.7
+  - 1.8
+  - tip
+
+matrix:
+  allow_failures:
+  - go: tip
 
 services:
     - postgresql
diff --git a/CREATING_MODULES.md b/CREATING_MODULES.md
index 158fd57..3213ead 100644
--- a/CREATING_MODULES.md
+++ b/CREATING_MODULES.md
@@ -12,12 +12,19 @@ package main
 
 import (
 	"net/http"
+	"log"
 
 	"github.com/nuveo/prest/cmd"
 	"github.com/nuveo/prest/config"
 )
 
 func main() {
+	// Get pREST app
+	n := config.GetApp()
+
+	// Register custom middleware
+	n.Use(negroni.HandlerFunc(CustomMiddleware))
+
 	// Get pPREST router
 	r := config.GetRouter()
 
@@ -32,4 +39,9 @@ func Pong(w http.ResponseWriter, r *http.Request) {
 	w.Write([]byte("Pong!"))
 	return
 }
+
+func CustomMiddleware(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
+	log.Println("Calling custom middleware")
+	next(w, r)
+}
 ```
\ No newline at end of file
diff --git a/cmd/root.go b/cmd/root.go
index 9ef2605..c148b7b 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -50,7 +50,7 @@ func app() {
 		log.Fatalf("Error parsing conf: %s", err)
 	}
 
-	n := negroni.Classic()
+	n := config.GetApp()
 	n.Use(negroni.HandlerFunc(middlewares.HandlerSet))
 	if cfg.JWTKey != "" {
 		n.Use(middlewares.JwtMiddleware(cfg.JWTKey))
diff --git a/config/middlewares.go b/config/middlewares.go
new file mode 100644
index 0000000..d3c7fd3
--- /dev/null
+++ b/config/middlewares.go
@@ -0,0 +1,17 @@
+package config
+
+import "github.com/urfave/negroni"
+
+var app *negroni.Negroni
+
+func initApp() {
+	app = negroni.Classic()
+}
+
+// GetApp get negroni
+func GetApp() *negroni.Negroni {
+	if app == nil {
+		initApp()
+	}
+	return app
+}


Test patch:
diff --git a/config/middlewares_test.go b/config/middlewares_test.go
new file mode 100644
index 0000000..eb0646e
--- /dev/null
+++ b/config/middlewares_test.go
@@ -0,0 +1,18 @@
+package config
+
+import "testing"
+
+func TestInitApp(t *testing.T) {
+	initApp()
+	if app == nil {
+		t.Errorf("App should not be nil.")
+	}
+}
+
+func TestGetApp(t *testing.T) {
+	app = nil
+	n := GetApp()
+	if n == nil {
+		t.Errorf("Should return an app.")
+	}
+}