Files
contact/vendor/local/system/sysconf/const.go
2019-07-03 11:48:52 -06:00

167 lines
3.6 KiB
Go
Executable File

package sysconf
import (
"encoding/json"
"fmt"
"io/ioutil"
"local/logger"
"os"
"path"
"strconv"
"strings"
"time"
)
type Conf struct {
Name string
Pub string
Port string
IP string
IP2 string
TimeOut time.Duration
Timeout string
Log string
DB string
Measure bool
IntervalUnit time.Duration
Intervalunit string
Overwrite bool
ConfFile string
}
var configs map[string]Conf
func (c Conf) String() string {
kv := make(map[string]interface{})
kv["name"] = c.Name
kv["port"] = c.Port
kv["IP"] = c.IP
kv["IP2"] = c.IP2
out := ""
for k, v := range kv {
out += fmt.Sprintf("%v:%v ", k, v)
}
return out
}
func Get(name string) Conf {
if configs == nil {
open()
}
if _, ok := configs[name]; !ok {
panic("Unknown service " + name)
}
logger.Log("config: got", name, "config: ", configs[name])
return configs[name]
}
func open() {
f, err := ioutil.ReadFile(path.Join(os.Getenv("GOPATH"), "src", "local1", "system", "sysconf", "conf.json"))
if err != nil {
panic(err)
}
var jsonConfs []Conf
err = json.Unmarshal([]byte(string(f)), &jsonConfs)
if err != nil {
panic(err)
}
configs = make(map[string]Conf)
ports := make(map[string]bool)
initPort := 28685
thisPort := 0
for i := range jsonConfs {
portstr := jsonConfs[i].Port
portlist := strings.Split(portstr, ",")
for j := range portlist {
ports[portlist[j]] = true
}
}
for i := range jsonConfs {
if jsonConfs[i].Timeout != "" {
jsonConfs[i].TimeOut, err = time.ParseDuration(jsonConfs[i].Timeout)
if err != nil {
panic(err)
}
}
if jsonConfs[i].Intervalunit != "" {
jsonConfs[i].IntervalUnit, err = time.ParseDuration(jsonConfs[i].Intervalunit)
if err != nil {
panic(err)
}
}
if jsonConfs[i].Port == "" {
thisPort += 1
port := strconv.Itoa(initPort + thisPort)
for _, ok := ports[port]; ok; _, ok = ports[port] {
port = strconv.Itoa(initPort + thisPort)
thisPort++
}
jsonConfs[i].Port = port
ports[jsonConfs[i].Port] = true
}
if jsonConfs[i].Log != "" && jsonConfs[i].Log[0] == '.' {
jsonConfs[i].Log = os.Getenv("MNT") + jsonConfs[i].Log
}
if jsonConfs[i].DB != "" && jsonConfs[i].DB[0] == '.' {
jsonConfs[i].DB = os.Getenv("MNT") + jsonConfs[i].DB
}
if jsonConfs[i].ConfFile != "" && jsonConfs[i].ConfFile[0] == '.' {
jsonConfs[i].ConfFile = os.Getenv("MNT") + jsonConfs[i].ConfFile
}
configs[jsonConfs[i].Name] = jsonConfs[i]
}
}
/*
var (
Broadcaster = Conf{
Name: "broadcaster",
Port: "28688",
Timeout: time.Second * 5,
IP: "localhost",
}
JBTServe = Conf{
Name: "jbt-serve",
Port: "28080",
Timeout: time.Second * 5,
IP: "localhost",
}
JBTClient = Conf{
Name: "jbt-client",
Port: "28081",
Timeout: time.Second * 5,
IP: "localhost",
}
NATServe = Conf{
Name: "nat-serve",
Port: "28447",
Timeout: time.Second * 5,
IP: "localhost",
}
RSSMon = Conf{
Name: "rss-monitor",
Port: "28686",
Timeout: time.Second * 5,
IP: "localhost",
Log: "./my.log",
DB: "./my.db",
Measure: false,
IntervalUnit: time.Second,
}
SevAlerts = Conf{
Name: "sev-alerts",
IP: "https://qualtrics.atlassian.net/sr/jira.issueviews:searchrequest-rss/44938/SearchRequest-44938.xml?tempMax=1000",
Port: "YnJlZWxAcXVhbHRyaWNzLmNvbTpeKllycGl6Y3cmNTRPd3Yw",
Timeout: time.Minute * 60,
IntervalUnit: time.Minute * 15,
}
Email = Conf{
Name: "bbarl64@gmail.com",
IP: "smtp.gmail.com:465",
Port: ">_w2Xeq\"tvJkv5y\\",
DB: "3852010864@txt.att.net",
}
)
*/