beta da beta da beta 0.0.01 '-'
This commit is contained in:
75
back_end/config_data/config_data.go
Normal file
75
back_end/config_data/config_data.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package config_data
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/gocql/gocql"
|
||||
)
|
||||
|
||||
const (
|
||||
file_path = "./"
|
||||
)
|
||||
|
||||
func FileAuthGET() (map[string]interface{}, error) {
|
||||
file, err := os.Open(file_path + "zartigo.conf")
|
||||
if err != nil {
|
||||
datafile := []byte("# nada por agora")
|
||||
os.MkdirAll(file_path, 0755)
|
||||
os.WriteFile(file_path+"zartigo.conf", datafile, 0644)
|
||||
log.Fatal("o arquivo de config não existia, mas foi criado no caminho " + file_path + " o configure e rode novamente.")
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
MapDATA := make(map[string]interface{})
|
||||
ScanData := bufio.NewScanner(file)
|
||||
for ScanData.Scan() {
|
||||
linhas := strings.TrimSpace(ScanData.Text())
|
||||
partes := strings.SplitN(linhas, "=", 2)
|
||||
if linhas == "" || strings.HasPrefix(linhas, "#") || len(partes) != 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
key := strings.TrimSpace(partes[0])
|
||||
value := strings.TrimSpace(partes[1])
|
||||
|
||||
if commit := strings.Index(value, "#"); commit != -1 {
|
||||
value = strings.TrimSpace(value[:commit])
|
||||
}
|
||||
|
||||
switch strings.Contains((value), ",") {
|
||||
case true:
|
||||
valueList := strings.Split(value, ",")
|
||||
for i := range valueList {
|
||||
valueList[i] = strings.TrimSpace(valueList[i])
|
||||
}
|
||||
MapDATA[key] = valueList
|
||||
break
|
||||
case false:
|
||||
MapDATA[key] = value
|
||||
break
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return MapDATA, nil
|
||||
}
|
||||
|
||||
func UseDB(ServerIP string, DataBaseNAME string) (*gocql.Session, error) {
|
||||
cred, err := FileAuthGET()
|
||||
con := gocql.NewCluster(ServerIP)
|
||||
con.Keyspace = DataBaseNAME
|
||||
con.Authenticator = gocql.PasswordAuthenticator{
|
||||
Username: cred["user"].(string),
|
||||
Password: cred["password"].(string),
|
||||
}
|
||||
session, err := con.CreateSession()
|
||||
if err != nil {
|
||||
return nil, errors.New("error ao conectar no banco de dados")
|
||||
}
|
||||
log.Println("conexão efetuada com sucesso")
|
||||
return session, nil
|
||||
}
|
||||
Reference in New Issue
Block a user