Matheus Querido 6 years ago
parent
commit
bb8b6d28b7
2 changed files with 25 additions and 8 deletions
  1. 17 7
      core/database/database.go
  2. 8 1
      main.go

+ 17 - 7
core/database/database.go

4
 	"database/sql"
4
 	"database/sql"
5
 	"log"
5
 	"log"
6
 
6
 
7
-	"../../config"
8
 	_ "github.com/go-sql-driver/mysql"
7
 	_ "github.com/go-sql-driver/mysql"
9
 )
8
 )
10
 
9
 
10
+//DB : Conexão com db
11
 var DB *sql.DB
11
 var DB *sql.DB
12
 
12
 
13
-func Init() error {
13
+//Config : struct do banco de dados
14
+type Config struct {
15
+	Host   string
16
+	Port   string
17
+	User   string
18
+	Pass   string
19
+	Schema string
20
+}
21
+
22
+//Init : iniciação do metodo de conexão
23
+func Init(config Config) error {
14
 
24
 
15
 	//user:pass@tcp(host:port)/schema?parseTime=true
25
 	//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"
26
+	var dataSource = config.User + ":" +
27
+		config.Pass + "@tcp(" +
28
+		config.Host + ":" +
29
+		config.Port + ")/" +
30
+		config.Schema + "?parseTime=true"
21
 
31
 
22
 	var err error
32
 	var err error
23
 
33
 

+ 8 - 1
main.go

13
 )
13
 )
14
 
14
 
15
 func main() {
15
 func main() {
16
+	dbConfig := database.Config{
17
+		config.DATABASE_HOST,
18
+		config.DATABASE_PORT,
19
+		config.DATABASE_USER,
20
+		config.DATABASE_PASS,
21
+		config.DATABASE_SCHEMA,
22
+	}
16
 
23
 
17
-	database.Init()
24
+	database.Init(dbConfig)
18
 
25
 
19
 	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
26
 	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
20
 		res.SetResponseWriter(w)
27
 		res.SetResponseWriter(w)