Browse Source

Merge branch 'master' of 208.97.141.22:Matheus/go-bootstrap

William Wiechorek 6 years ago
parent
commit
8c52769b96
6 changed files with 112 additions and 4 deletions
  1. 10 2
      core/database/database.go
  2. 8 0
      src/config/config.go
  3. 2 2
      src/config/routes.go
  4. 34 0
      src/lib/authentication.go
  5. 51 0
      src/lib/curl.go
  6. 7 0
      src/lib/error.go

+ 10 - 2
core/database/database.go

@@ -4,16 +4,24 @@ import (
4 4
 	"database/sql"
5 5
 	"log"
6 6
 
7
+	"../../src/config"
7 8
 	_ "github.com/go-sql-driver/mysql"
8 9
 )
9 10
 
10 11
 var DB *sql.DB
11 12
 
12
-func Init(dataSourceName string) error {
13
+func Init() error {
14
+
15
+	//user:pass@tcp(host:port)/schema?parseTime=true
16
+	var dataSource = config.DATABASE_USER + ":" +
17
+		config.DATABASE_PASS + "@tcp(" +
18
+		config.DATABASE_HOST + ":" +
19
+		config.DATABASE_PORT + ")/" +
20
+		config.DATABASE_SCHEMA + "?parseTime=true"
13 21
 
14 22
 	var err error
15 23
 
16
-	DB, err = sql.Open("mysql", dataSourceName)
24
+	DB, err = sql.Open("mysql", dataSource)
17 25
 
18 26
 	if err != nil {
19 27
 		log.Panic(err)

+ 8 - 0
src/config/config.go

@@ -1,3 +1,11 @@
1 1
 package config
2 2
 
3
+/* SERVER CONFIGURATION */
3 4
 const SERVER_PORT = 8080
5
+
6
+/* DATABASE CONFIGURATION */
7
+const DATABASE_HOST = "208.97.141.22"
8
+const DATABASE_PORT = "3306"
9
+const DATABASE_USER = "hml"
10
+const DATABASE_PASS = "spr777hml"
11
+const DATABASE_SCHEMA = "hml_mkt_sprinta"

+ 2 - 2
src/config/routes.go

@@ -8,7 +8,7 @@ import (
8 8
 //Routes rotas de acesso
9 9
 var Routes = router.Routes{
10 10
 	router.Route{
11
-		`\/(.*)`,
12
-		test.Inicial,
11
+		Pattern: `\/(.*)`,
12
+		Handler: test.Inicial,
13 13
 	},
14 14
 }

+ 34 - 0
src/lib/authentication.go

@@ -0,0 +1,34 @@
1
+package lib
2
+
3
+import (
4
+	"net/http"
5
+	"strconv"
6
+)
7
+
8
+type UserData struct {
9
+	Id_user   string
10
+	Email     string
11
+	Name      string
12
+	Username  string
13
+	Token     string
14
+	Url_token string
15
+	Avatar    string
16
+	Language  string
17
+	Errors    []apiError
18
+}
19
+
20
+func (user UserData) getId() int {
21
+	id, _ := strconv.Atoi(user.Id_user)
22
+	return id
23
+}
24
+
25
+func GetUser(r *http.Request) *UserData {
26
+
27
+	auth := r.Header.Get("Auth")
28
+	userData := curl(auth)
29
+
30
+	// debug only
31
+	//fmt.Printf("%+v\n", userData)
32
+
33
+	return userData
34
+}

+ 51 - 0
src/lib/curl.go

@@ -0,0 +1,51 @@
1
+package lib
2
+
3
+import (
4
+	"encoding/json"
5
+	"io/ioutil"
6
+	"log"
7
+	"net/http"
8
+)
9
+
10
+func curl(auth string) *UserData {
11
+
12
+	req, err := http.NewRequest("POST", "http://208.97.141.22/api/sign/user_data", nil)
13
+
14
+	if err != nil {
15
+		return nil
16
+	}
17
+
18
+	req.Header.Set("Auth", auth)
19
+
20
+	resp, err := http.DefaultClient.Do(req)
21
+
22
+	if err != nil {
23
+		log.Print(err)
24
+		return nil
25
+	}
26
+
27
+	defer resp.Body.Close()
28
+
29
+	body, _ := ioutil.ReadAll(resp.Body)
30
+
31
+	// debug only
32
+	//s := string(body[:])
33
+	//fmt.Println(s)
34
+
35
+	var userData UserData
36
+
37
+	err = json.Unmarshal(body, &userData)
38
+
39
+	//fmt.Printf("%+v\n", userData)
40
+
41
+	if err != nil {
42
+		log.Print(err)
43
+		return nil
44
+	}
45
+
46
+	if len(userData.Errors) > 0 {
47
+		return nil
48
+	}
49
+
50
+	return &userData
51
+}

+ 7 - 0
src/lib/error.go

@@ -0,0 +1,7 @@
1
+package lib
2
+
3
+type apiError struct {
4
+	ErrorType string `json:type`
5
+	Parameter string `json:parameter`
6
+	Message   string `json:message`
7
+}