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

24
main.go
View File

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

View File

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