Browse Source

Boototstraping

William Wiechorek 6 years ago
parent
commit
499ef41eed
6 changed files with 37 additions and 11 deletions
  1. BIN
      build
  2. 1 1
      core/database/database.go
  3. 17 7
      core/request/request.go
  4. 8 1
      core/response/response.go
  5. 8 0
      main.go
  6. 3 2
      src/controller/Test/Test.go

BIN
build


+ 1 - 1
core/database/database.go

@@ -28,7 +28,7 @@ func Init() error {
28 28
 	}
29 29
 
30 30
 	if err = DB.Ping(); err != nil {
31
-		log.Panic(err)s
31
+		log.Panic(err)
32 32
 	}
33 33
 
34 34
 	return err

+ 17 - 7
core/request/request.go

@@ -6,13 +6,17 @@ import (
6 6
 	"net/http"
7 7
 )
8 8
 
9
+var request *http.Request
10
+
9 11
 //Method : retorna o metodo
10
-func Method(r *http.Request) string {
11
-	return r.Method
12
+func Method() string {
13
+	return request.Method
12 14
 }
13 15
 
14 16
 //Post : retorna um parametro post
15
-func Post(r *http.Request, parameter string) string {
17
+func Post(parameter string) string {
18
+	var r = request
19
+
16 20
 	if err := r.ParseForm(); err != nil {
17 21
 		fmt.Printf("ParseForm() err: %v", err)
18 22
 		return ""
@@ -21,7 +25,9 @@ func Post(r *http.Request, parameter string) string {
21 25
 }
22 26
 
23 27
 //Get : retorna um parametro get
24
-func Get(r *http.Request, parameter string) string {
28
+func Get(parameter string) string {
29
+	var r = request
30
+
25 31
 	keys, ok := r.URL.Query()["key"]
26 32
 
27 33
 	if !ok || len(keys[0]) < 1 {
@@ -35,16 +41,20 @@ func Get(r *http.Request, parameter string) string {
35 41
 }
36 42
 
37 43
 //Parameter : retorna um parametro post ou get
38
-func Parameter(r *http.Request, parameter string) string {
39
-	var post = Post(r, parameter)
44
+func Parameter(parameter string) string {
45
+	var post = Post(parameter)
40 46
 	if post != "" {
41 47
 		return post
42 48
 	}
43 49
 
44
-	var get = Get(r, parameter)
50
+	var get = Get(parameter)
45 51
 	if get != "" {
46 52
 		return get
47 53
 	}
48 54
 
49 55
 	return ""
50 56
 }
57
+
58
+func SetRequest(r *http.Request) {
59
+	request = r
60
+}

+ 8 - 1
core/response/response.go

@@ -7,8 +7,11 @@ import (
7 7
 
8 8
 var responseCode = 200
9 9
 
10
+var responseWriter *http.ResponseWriter
11
+
10 12
 //JSON :response json
11
-func JSON(w http.ResponseWriter, data interface{}) {
13
+func JSON(data interface{}) {
14
+	w := *responseWriter
12 15
 	js, err := json.Marshal(data)
13 16
 	if err != nil {
14 17
 		http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -24,3 +27,7 @@ func JSON(w http.ResponseWriter, data interface{}) {
24 27
 func SetCode(code int) {
25 28
 	responseCode = code
26 29
 }
30
+
31
+func SetResponseWriter(w http.ResponseWriter) {
32
+	responseWriter = &w
33
+}

+ 8 - 0
main.go

@@ -5,11 +5,19 @@ import (
5 5
 	"net/http"
6 6
 	"strconv"
7 7
 
8
+	"./core/database"
9
+	req "./core/request"
10
+	res "./core/response"
8 11
 	"./src/config"
9 12
 )
10 13
 
11 14
 func main() {
15
+
16
+	database.Init()
17
+
12 18
 	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
19
+		res.SetResponseWriter(w)
20
+		req.SetRequest(r)
13 21
 		config.Routes.Match(w, r)
14 22
 	})
15 23
 

+ 3 - 2
src/controller/Test/Test.go

@@ -21,10 +21,11 @@ func Inicial(w http.ResponseWriter, r *http.Request, vars []string) {
21 21
 
22 22
 	if err := r.ParseForm(); err != nil {
23 23
 		fmt.Printf("ParseForm() err: %v", err)
24
+		return
24 25
 	}
25 26
 
26
-	fmt.Println(req.Method(r))
27
+	fmt.Println(req.Method())
27 28
 
28 29
 	res.SetCode(404)
29
-	res.JSON(w, profile)
30
+	res.JSON(profile)
30 31
 }