Repository: nuveo/prest
Pull request #116 (2017-02-26 16:53:14)

fix some warnings
Just fix some lint warnings




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


```diff
@@           Coverage Diff           @@
##           master     #116   +/-   ##
=======================================
  Coverage   89.64%   89.64%           
=======================================
  Files           9        9           
  Lines         811      811           
=======================================
  Hits          727      727           
  Misses         54       54           
  Partials       30       30
```


| [Impacted Files](https://codecov.io/gh/nuveo/prest/pull/116?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [config/routers.go](https://codecov.io/gh/nuveo/prest/compare/0879cbef401b9d4b8b9457c7adfe088acc3d5ea9...f155fe5122132e3dc2f5358e301c1a1aba786623?src=pr&el=tree#diff-Y29uZmlnL3JvdXRlcnMuZ28=) | `100% <ø> (ø)` | :white_check_mark: |
| [adapters/postgres/queries.go](https://codecov.io/gh/nuveo/prest/compare/0879cbef401b9d4b8b9457c7adfe088acc3d5ea9...f155fe5122132e3dc2f5358e301c1a1aba786623?src=pr&el=tree#diff-YWRhcHRlcnMvcG9zdGdyZXMvcXVlcmllcy5nbw==) | `91.66% <100%> (ø)` | :white_check_mark: |
| [adapters/postgres/postgres.go](https://codecov.io/gh/nuveo/prest/compare/0879cbef401b9d4b8b9457c7adfe088acc3d5ea9...f155fe5122132e3dc2f5358e301c1a1aba786623?src=pr&el=tree#diff-YWRhcHRlcnMvcG9zdGdyZXMvcG9zdGdyZXMuZ28=) | `85.36% <100%> (ø)` | :white_check_mark: |
| [config/config.go](https://codecov.io/gh/nuveo/prest/compare/0879cbef401b9d4b8b9457c7adfe088acc3d5ea9...f155fe5122132e3dc2f5358e301c1a1aba786623?src=pr&el=tree#diff-Y29uZmlnL2NvbmZpZy5nbw==) | `81.13% <100%> (ø)` | :white_check_mark: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/nuveo/prest/pull/116?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/116?src=pr&el=footer). Last update [0879cbe...f155fe5](https://codecov.io/gh/nuveo/prest/compare/0879cbef401b9d4b8b9457c7adfe088acc3d5ea9...f155fe5122132e3dc2f5358e301c1a1aba786623?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/adapters/postgres/postgres.go b/adapters/postgres/postgres.go
index 2205047..365901f 100644
--- a/adapters/postgres/postgres.go
+++ b/adapters/postgres/postgres.go
@@ -607,12 +607,12 @@ func GetQueryOperator(op string) (string, error) {
 
 // TablePermissions get tables permissions based in prest configuration
 func TablePermissions(table string, op string) bool {
-	restrict := config.PREST_CONF.AccessConf.Restrict
+	restrict := config.PrestConf.AccessConf.Restrict
 	if !restrict {
 		return true
 	}
 
-	tables := config.PREST_CONF.AccessConf.Tables
+	tables := config.PrestConf.AccessConf.Tables
 	for _, t := range tables {
 		if t.Name == table {
 			for _, p := range t.Permissions {
@@ -627,13 +627,13 @@ func TablePermissions(table string, op string) bool {
 
 // FieldsPermissions get fields permissions based in prest configuration
 func FieldsPermissions(table string, cols []string, op string) []string {
-	restrict := config.PREST_CONF.AccessConf.Restrict
+	restrict := config.PrestConf.AccessConf.Restrict
 	if !restrict {
 		return cols
 	}
 
 	var permittedCols []string
-	tables := config.PREST_CONF.AccessConf.Tables
+	tables := config.PrestConf.AccessConf.Tables
 	for _, t := range tables {
 		if t.Name == table {
 			for _, f := range t.Fields {
diff --git a/adapters/postgres/queries.go b/adapters/postgres/queries.go
index 1718695..9abc5fe 100644
--- a/adapters/postgres/queries.go
+++ b/adapters/postgres/queries.go
@@ -30,7 +30,7 @@ func GetScript(verb, folder, scriptName string) (script string, err error) {
 		return
 	}
 
-	script = filepath.Join(config.PREST_CONF.QueriesPath, folder, fmt.Sprint(scriptName, sufix))
+	script = filepath.Join(config.PrestConf.QueriesPath, folder, fmt.Sprint(scriptName, sufix))
 
 	if _, err = os.Stat(script); os.IsNotExist(err) {
 		err = fmt.Errorf("could not load %s", script)
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/config.go b/config/config.go
index 32bf5f6..e6083b8 100644
--- a/config/config.go
+++ b/config/config.go
@@ -12,12 +12,14 @@ import (
 	"github.com/spf13/viper"
 )
 
+// TablesConf informations
 type TablesConf struct {
 	Name        string   `mapstructure:"name"`
 	Permissions []string `mapstructure:"permissions"`
 	Fields      []string `mapstructure:"fields"`
 }
 
+// AccessConf informations
 type AccessConf struct {
 	Restrict bool
 	Tables   []TablesConf
@@ -41,7 +43,8 @@ type Prest struct {
 	CORSAllowOrigin []string
 }
 
-var PREST_CONF *Prest
+// PrestConf config variable
+var PrestConf *Prest
 
 func init() {
 	viperCfg()
@@ -103,17 +106,18 @@ func Parse(cfg *Prest) (err error) {
 	return
 }
 
+// InitConf initialize config
 func InitConf() {
 	viperCfg()
 	prestConfig := Prest{}
 	Parse(&prestConfig)
-	PREST_CONF = &prestConfig
+	PrestConf = &prestConfig
 
 	if !prestConfig.AccessConf.Restrict {
 		fmt.Println("You are running pREST in public mode.")
 	}
 
-	if _, err := os.Stat(PREST_CONF.QueriesPath); os.IsNotExist(err) {
-		os.MkdirAll(PREST_CONF.QueriesPath, 0777)
+	if _, err := os.Stat(PrestConf.QueriesPath); os.IsNotExist(err) {
+		os.MkdirAll(PrestConf.QueriesPath, 0777)
 	}
 }
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
+}
diff --git a/config/routers.go b/config/routers.go
index 09bb3d4..e278075 100644
--- a/config/routers.go
+++ b/config/routers.go
@@ -8,6 +8,7 @@ func initRouter() {
 	router = mux.NewRouter()
 }
 
+// GetRouter for pREST
 func GetRouter() *mux.Router {
 	if router == nil {
 		initRouter()


Test patch:
diff --git a/adapters/postgres/postgres_test.go b/adapters/postgres/postgres_test.go
index 7e1ed1b..847deab 100644
--- a/adapters/postgres/postgres_test.go
+++ b/adapters/postgres/postgres_test.go
@@ -708,7 +708,7 @@ func TestFieldsPermissions(t *testing.T) {
 }
 
 func TestRestrictFalse(t *testing.T) {
-	config.PREST_CONF.AccessConf.Restrict = false
+	config.PrestConf.AccessConf.Restrict = false
 
 	t.Log("Read unrestrict")
 	fields := FieldsPermissions("test_list_only_id", []string{"*"}, "read")
diff --git a/adapters/postgres/queries_test.go b/adapters/postgres/queries_test.go
index 73be9ae..9716c87 100644
--- a/adapters/postgres/queries_test.go
+++ b/adapters/postgres/queries_test.go
@@ -13,12 +13,12 @@ import (
 
 func TestMain(m *testing.M) {
 	config.InitConf()
-	createMockScripts(config.PREST_CONF.QueriesPath)
-	writeMockScripts(config.PREST_CONF.QueriesPath)
+	createMockScripts(config.PrestConf.QueriesPath)
+	writeMockScripts(config.PrestConf.QueriesPath)
 
 	code := m.Run()
 
-	removeMockScripts(config.PREST_CONF.QueriesPath)
+	removeMockScripts(config.PrestConf.QueriesPath)
 	os.Exit(code)
 }
 
diff --git a/config/config_test.go b/config/config_test.go
index 6222c26..3e0f023 100644
--- a/config/config_test.go
+++ b/config/config_test.go
@@ -10,12 +10,12 @@ func TestInitConf(t *testing.T) {
 	os.Setenv("PREST_CONF", "../testdata/prest.toml")
 
 	InitConf()
-	if len(PREST_CONF.AccessConf.Tables) < 2 {
-		t.Errorf("expected > 2, got: %d", len(PREST_CONF.AccessConf.Tables))
+	if len(PrestConf.AccessConf.Tables) < 2 {
+		t.Errorf("expected > 2, got: %d", len(PrestConf.AccessConf.Tables))
 	}
 
 	InitConf()
-	if !PREST_CONF.AccessConf.Restrict {
+	if !PrestConf.AccessConf.Restrict {
 		t.Error("expected true, but got false")
 	}
 }
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.")
+	}
+}
diff --git a/controllers/sql_test.go b/controllers/sql_test.go
index 6dbc253..5bbee18 100644
--- a/controllers/sql_test.go
+++ b/controllers/sql_test.go
@@ -13,12 +13,12 @@ import (
 
 func TestMain(m *testing.M) {
 	config.InitConf()
-	createMockScripts(config.PREST_CONF.QueriesPath)
-	writeMockScripts(config.PREST_CONF.QueriesPath)
+	createMockScripts(config.PrestConf.QueriesPath)
+	writeMockScripts(config.PrestConf.QueriesPath)
 
 	code := m.Run()
 
-	removeMockScripts(config.PREST_CONF.QueriesPath)
+	removeMockScripts(config.PrestConf.QueriesPath)
 	os.Exit(code)
 }