117 lines
2.2 KiB
Go
117 lines
2.2 KiB
Go
package main
|
||
|
||
import (
|
||
"bytes"
|
||
"errors"
|
||
"fmt"
|
||
"net"
|
||
"strconv"
|
||
"strings"
|
||
|
||
"github.com/BurntSushi/toml"
|
||
go_ora "github.com/sijms/go-ora/v2"
|
||
)
|
||
|
||
type Config struct {
|
||
// Список серверов
|
||
Servers []*Server
|
||
|
||
// Кол-во prefetch строк
|
||
PrefetchRows int
|
||
|
||
// Таймаут ожидания ответа на запрос
|
||
Timeout int
|
||
}
|
||
|
||
type Server struct {
|
||
// Имя сервера
|
||
Name string
|
||
|
||
// Логин БД Oracle
|
||
Login string
|
||
|
||
// Пароль БД Oracle
|
||
Password string
|
||
|
||
// Список хостов БД Oracle
|
||
Hosts []string
|
||
|
||
// Наименование сервиса БД Oracle
|
||
Service string
|
||
|
||
// Статус работы с сервером
|
||
status string
|
||
|
||
// Ошибка работы с сервером
|
||
err error
|
||
|
||
// Флаг завершения работы
|
||
finished bool
|
||
|
||
config *Config
|
||
}
|
||
|
||
func loadConfig(filePath string) (*Config, error) {
|
||
b, err := readFileIgnoreBOM(filePath)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
config := new(Config)
|
||
|
||
_, err = toml.NewDecoder(bytes.NewReader(b)).Decode(&config)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
if config.PrefetchRows <= 0 {
|
||
config.PrefetchRows = 1000
|
||
}
|
||
if config.Timeout < 0 {
|
||
config.Timeout = 0
|
||
}
|
||
|
||
for i := range config.Servers {
|
||
config.Servers[i].config = config
|
||
}
|
||
|
||
return config, nil
|
||
}
|
||
|
||
func (s *Server) Url() (string, error) {
|
||
urlOptions := make(map[string]string)
|
||
|
||
if len(s.Hosts) == 0 {
|
||
return "", errors.New("hostname is not specified")
|
||
}
|
||
|
||
host, portStr, err := net.SplitHostPort(s.Hosts[0])
|
||
if err != nil {
|
||
host = s.Hosts[0]
|
||
portStr = "1521"
|
||
}
|
||
port, err := strconv.Atoi(portStr)
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
|
||
additionalHosts := make([]string, 0)
|
||
if len(s.Hosts) > 1 {
|
||
for _, v := range s.Hosts[1:] {
|
||
h, p, err := net.SplitHostPort(v)
|
||
if err != nil {
|
||
h = v
|
||
p = "1521"
|
||
}
|
||
additionalHosts = append(additionalHosts, fmt.Sprintf("%s:%s", h, p))
|
||
}
|
||
|
||
urlOptions["server"] = strings.Join(additionalHosts, ",") // TODO: должен добавляться порт
|
||
}
|
||
|
||
urlOptions["TIMEOUT"] = strconv.Itoa(s.config.Timeout)
|
||
urlOptions["PREFETCH_ROWS"] = strconv.Itoa(s.config.PrefetchRows)
|
||
|
||
return go_ora.BuildUrl(host, port, s.Service, s.Login, s.Password, urlOptions), nil
|
||
}
|