Browse Source

Boototstraping

William Wiechorek 6 years ago
parent
commit
691619311e
3 changed files with 69 additions and 80 deletions
  1. BIN
      build
  2. 63 78
      core/router/main.go
  3. 6 2
      main.go

BIN
build


+ 63 - 78
core/router/main.go

@@ -1,94 +1,79 @@
1 1
 package router
2 2
 
3 3
 import (
4
-	"fmt"
5 4
 	"net/http"
5
+	"regexp"
6
+	"strconv"
7
+	"strings"
6 8
 )
7 9
 
10
+type closure func(http.ResponseWriter, *http.Request)
11
+
8 12
 type route struct {
9 13
 	Pattern string
10
-	Handler http.Handler
14
+	Handler closure
11 15
 }
12 16
 
13 17
 //Routes array of route
14 18
 type Routes []route
15 19
 
20
+var routes Routes
21
+
16 22
 //Add adicionar rota em routes
17
-func (Routes) Add(pattern string, test string) {
18
-	fmt.Print(pattern)
19
-	// append(routes, route{
20
-	// 	pattern,
21
-	// 	handler,
22
-	// })
23
+func Add(pattern string, handler closure) {
24
+	routes = append(routes, route{
25
+		pattern,
26
+		handler,
27
+	})
28
+}
29
+
30
+//Match retorna a rota encontrada
31
+func Match(w http.ResponseWriter, r *http.Request) {
32
+	url := r.URL.Path
33
+	for _, element := range routes {
34
+		when := element.Pattern
35
+		pattern := toRegex(when)
36
+
37
+		items := regexSubmatch(pattern, url)
38
+
39
+		if items[0] == url {
40
+			element.Handler(w, r)
41
+		}
42
+	}
43
+}
44
+
45
+//PatternURL coloca barra no final de url se não tiver
46
+func patternURL(url string) string {
47
+	lastChar := url[len(url)-1:]
48
+	if lastChar != "/" {
49
+		url = url + "/"
50
+	}
51
+
52
+	return url
23 53
 }
24 54
 
25
-// func findURLMatch(data urls, url string) string {
26
-// 	for _, element := range data {
27
-// 		when := element.When
28
-// 		pattern := toRegex(when)
29
-
30
-// 		items := regexSubmatch(pattern, url)
31
-
32
-// 		if items[0] == url {
33
-// 			urlProxy := replaceStringsURL(items, element.To)
34
-// 			return urlProxy
35
-// 		}
36
-// 	}
37
-
38
-// 	return ""
39
-// }
40
-
41
-// func readFile(filePath string) urls {
42
-// 	file, err1 := ioutil.ReadFile(filePath)
43
-// 	if err1 != nil {
44
-// 		fmt.Printf("Error while reading file %s\n", filePath)
45
-// 		fmt.Printf("File error: %v\n", err1)
46
-// 		os.Exit(1)
47
-// 	}
48
-
49
-// 	var urls []urlObject
50
-
51
-// 	err2 := json.Unmarshal(file, &urls)
52
-// 	if err2 != nil {
53
-// 		fmt.Println("error:", err2)
54
-// 		os.Exit(1)
55
-// 	}
56
-
57
-// 	return urls
58
-// }
59
-
60
-// //PatternURL coloca barra no final de url se não tiver
61
-// func patternURL(url string) string {
62
-// 	lastChar := url[len(url)-1:]
63
-// 	if lastChar != "/" {
64
-// 		url = url + "/"
65
-// 	}
66
-
67
-// 	return url
68
-// }
69
-
70
-// //ToRegex converte uma expressao imputada em expressão do go
71
-// func toRegex(regex string) *regexp.Regexp {
72
-// 	return regexp.MustCompile(`(?m)` + regex)
73
-// }
74
-
75
-// //RegexSubmatch cria os matches aplicando a padrão de regex inputado
76
-// func regexSubmatch(pattern *regexp.Regexp, str string) []string {
77
-// 	data := []string{""}
78
-
79
-// 	items := pattern.FindAllStringSubmatch(str, -1)
80
-// 	if len(items) > 0 {
81
-// 		data = items[0]
82
-// 	}
83
-
84
-// 	return data
85
-// }
86
-
87
-// //ReplaceStringsURL alterar a url imputada colocando as variaveis da url inserida
88
-// func replaceStringsURL(items []string, urlPattern string) string {
89
-// 	for index, element := range items {
90
-// 		urlPattern = strings.Replace(urlPattern, "$"+strconv.Itoa(index), element, -1)
91
-// 	}
92
-
93
-// 	return urlPattern
94
-// }
55
+//ToRegex converte uma expressao imputada em expressão do go
56
+func toRegex(regex string) *regexp.Regexp {
57
+	return regexp.MustCompile(`(?m)` + regex)
58
+}
59
+
60
+//RegexSubmatch cria os matches aplicando a padrão de regex inputado
61
+func regexSubmatch(pattern *regexp.Regexp, str string) []string {
62
+	data := []string{""}
63
+
64
+	items := pattern.FindAllStringSubmatch(str, -1)
65
+	if len(items) > 0 {
66
+		data = items[0]
67
+	}
68
+
69
+	return data
70
+}
71
+
72
+//ReplaceStringsURL alterar a url imputada colocando as variaveis da url inserida
73
+func replaceStringsURL(items []string, urlPattern string) string {
74
+	for index, element := range items {
75
+		urlPattern = strings.Replace(urlPattern, "$"+strconv.Itoa(index), element, -1)
76
+	}
77
+
78
+	return urlPattern
79
+}

+ 6 - 2
main.go

@@ -12,8 +12,12 @@ import (
12 12
 
13 13
 func main() {
14 14
 	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
15
-		router.Routes.Add("Url", "test")
16
-		fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
15
+
16
+		router.Add(`\/(.*)`, func(w http.ResponseWriter, r *http.Request) {
17
+			fmt.Fprintf(w, "Hello Route, %q", html.EscapeString(r.URL.Path))
18
+		})
19
+
20
+		router.Match(w, r)
17 21
 	})
18 22
 
19 23
 	log.Fatal(http.ListenAndServe(":"+strconv.Itoa(ServerPort), nil))