Added Config file #10

Merged
stefan merged 7 commits from config-file into master 2021-02-11 19:58:29 +00:00
3 changed files with 23 additions and 19 deletions
Showing only changes of commit 861d1c5d5e - Show all commits

View File

@ -1,12 +1,12 @@
{
"unms": {
"active": "true",
"enabled": true,
"unmsAPIUrl": "https://unifi.freifunk-troisdorf.de/v2.1",
"APItoken": "API TOKEN",
"devicesURL": "https://git.freifunk-rhein-sieg.net/Freifunk-Troisdorf/ubnt-freifunk-map-api/raw/branch/master/"
},
"unifi": {
"active": "true",
"enabled": true,
"APIUrl": "https://unifi.freifunk-troisdorf.de:8443",
"user": "APIuser",
"password": "PASSWORD",

32
main.go
View File

@ -25,13 +25,13 @@ const (
// flags
var configPath = flag.String("configPath", "config.json", "Path to config.json")
var version = "development"
var delay time.Duration = 5 * time.Second
var delay time.Duration = 60 * time.Second
func main() {
log.Printf("starting version %s...\n", version)
// parse all flags
flag.Parse()
c := loadConfig(*configPath)
c := loadconfig(*configPath)
// check if flags are set
if *configPath == "" {
@ -44,8 +44,8 @@ func main() {
serveJSON()
}
func loadConfig(file string) Config {
var config Config
func loadconfig(file string) config {
var config config
configFile, err := os.Open(file)
if err != nil {
log.Fatalln(err)
@ -64,7 +64,7 @@ func itob(i int) bool {
}
//Unifi Controller API processing
func processUcAPIs(c Config) ([]node, []link) {
func processUcAPIs(c config) ([]node, []link) {
//get list of Unifi devices to display
var nodes []node
var links []link
@ -148,7 +148,7 @@ func processUcAPIs(c Config) ([]node, []link) {
}
//UNMS API processing (Richtfunk)
func processUNMSAPI(c Config) ([]node, []link) {
func processUNMSAPI(c config) ([]node, []link) {
// Variables for runtime
var links []link
var nodes []node
@ -238,18 +238,22 @@ func processUNMSAPI(c Config) ([]node, []link) {
return nodes, links
}
func processAPIs(c Config) {
func processAPIs(c config) {
tick := time.Tick(delay)
for range tick {
var nodes []node
var links []link
unmsNodes, unmsLinks := processUNMSAPI(c)
ucNodes, ucLinks := processUcAPIs(c)
nodes = append(nodes, unmsNodes...)
nodes = append(nodes, ucNodes...)
links = append(links, unmsLinks...)
links = append(links, ucLinks...)
if c.Unms.Enabled == true {
unmsNodes, unmsLinks := processUNMSAPI(c)
nodes = append(nodes, unmsNodes...)
links = append(links, unmsLinks...)
}
if c.Unifi.Enabled == true {
ucNodes, ucLinks := processUcAPIs(c)
nodes = append(nodes, ucNodes...)
links = append(links, ucLinks...)
}
// assemble final struct
o := output{
@ -295,7 +299,7 @@ func getDevices(url string) (devices, error) {
return d, nil
}
func callUnifiAPI(c Config, url string, i interface{}) error {
func callUnifiAPI(c config, url string, i interface{}) error {
request, err := http.NewRequest(http.MethodGet, c.Unms.UnmsAPIURL+url, nil)
if err != nil {
return errors.New(fmt.Sprint("can't set request", c.Unms.UnmsAPIURL+url))

View File

@ -10,15 +10,15 @@ import (
"time"
)
type Config struct {
type config struct {
Unms struct {
Active string `json:"active"`
Enabled bool `json:"enabled"`
UnmsAPIURL string `json:"unmsAPIUrl"`
APItoken string `json:"APItoken"`
DevicesURL string `json:"devicesURL"`
} `json:"unms"`
Unifi struct {
Active string `json:"active"`
Enabled bool `json:"enabled"`
APIURL string `json:"APIUrl"`
User string `json:"user"`
Password string `json:"password"`