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
 	}
28
 	}
29
 
29
 
30
 	if err = DB.Ping(); err != nil {
30
 	if err = DB.Ping(); err != nil {
31
-		log.Panic(err)s
31
+		log.Panic(err)
32
 	}
32
 	}
33
 
33
 
34
 	return err
34
 	return err

+ 17 - 7
core/request/request.go

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

+ 8 - 1
core/response/response.go

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

+ 8 - 0
main.go

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

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

21
 
21
 
22
 	if err := r.ParseForm(); err != nil {
22
 	if err := r.ParseForm(); err != nil {
23
 		fmt.Printf("ParseForm() err: %v", err)
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
 	res.SetCode(404)
29
 	res.SetCode(404)
29
-	res.JSON(w, profile)
30
+	res.JSON(profile)
30
 }
31
 }