Browse Source

Middleware login

William Wiechorek 6 years ago
parent
commit
f780af83c4
3 changed files with 12 additions and 4 deletions
  1. BIN
      build
  2. 1 1
      config.go
  3. 11 3
      core/router/router.go

BIN
build


+ 1 - 1
config.go

@@ -7,7 +7,7 @@ const SERVER_PORT = 8081
7 7
 const DATABASE_HOST = "208.97.141.22"
8 8
 const DATABASE_PORT = "3306"
9 9
 const DATABASE_USER = "hml"
10
-const DATABASE_PASS = "spr7772hml"
10
+const DATABASE_PASS = "spr777hml"
11 11
 const DATABASE_SCHEMA = "hml_mkt_sprinta"
12 12
 
13 13
 /* SERVICES ENDPOINTS */

+ 11 - 3
core/router/router.go

@@ -1,7 +1,6 @@
1 1
 package router
2 2
 
3 3
 import (
4
-	"fmt"
5 4
 	"net/http"
6 5
 	"regexp"
7 6
 	"strconv"
@@ -24,7 +23,7 @@ type Routes []Route
24 23
 var routes Routes
25 24
 
26 25
 //Match retorna a rota encontrada
27
-func (routes Routes) Match(w http.ResponseWriter, r *http.Request) {
26
+func (routes Routes) Match(w http.ResponseWriter, r *http.Request) bool {
28 27
 	url := r.URL.Path
29 28
 	for _, element := range routes {
30 29
 		when := element.Pattern
@@ -33,10 +32,19 @@ func (routes Routes) Match(w http.ResponseWriter, r *http.Request) {
33 32
 		items := regexSubmatch(pattern, url)
34 33
 
35 34
 		if items[0] == url {
36
-			fmt.Println(element.Middlewares)
35
+			if element.Middlewares != nil && len(element.Middlewares) > 0 {
36
+				for _, mid := range element.Middlewares {
37
+					if !mid(w, r, items) {
38
+						return false
39
+					}
40
+				}
41
+			}
37 42
 			element.Handler(w, r, items)
43
+			return true
38 44
 		}
39 45
 	}
46
+
47
+	return false
40 48
 }
41 49
 
42 50
 //PatternURL coloca barra no final de url se não tiver