Loaded credentials from input json file

This commit is contained in:
2021-09-15 20:11:55 +02:00
parent 48754c96bb
commit 9095638867
2 changed files with 48 additions and 1 deletions

3
.gitignore vendored
View File

@@ -1 +1,2 @@
.vscode
.vscode
input.json

View File

@@ -2,7 +2,9 @@ package services
import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"time"
@@ -51,6 +53,15 @@ type XMPPClient struct {
Client *xmpp.Client
}
// ClientCredentials - credentials info for client configuration
type ClientCredentials struct {
Jid string `json:"username"`
Credential string `json:"password"`
Host string `json:"host"`
Port int `json:"port"`
Ping int `json:"ping"`
}
// Instance - get instance of xmpp service
func Instance() *XMPPService {
return &xmppService
@@ -66,6 +77,15 @@ func Init() {
}
xmppService.Router.HandleFunc("message", handleMessage)
//Get Client credentials from file
clientCredentials, err := getClientConfigsFromFile(appConfig.AppConfig.Credentials.CredentialsFileLocation)
if err != nil {
log.Fatalf("Unable to load credentials from file path %v, ERROR : %v TERMINATING", appConfig.AppConfig.Credentials.CredentialsFileLocation, err)
}
log.Printf("Client credentials length %v", len(clientCredentials))
xmppService.XMPPClients = make([]XMPPClient, 0)
xmppClient := XMPPClient{
Config: xmpp.Config{
@@ -115,6 +135,32 @@ func SendOnlinePresenceStanza(client *xmpp.Client, jid string) error {
}
}
func getClientConfigsFromFile(filePath string) ([]ClientCredentials, error) {
var clientCredentials []ClientCredentials
f, err := os.Open(filePath)
if err != nil {
return clientCredentials, err
}
defer f.Close()
byteValue, err := ioutil.ReadAll(f)
if err != nil {
return clientCredentials, err
}
err = json.Unmarshal(byteValue, &clientCredentials)
if err != nil {
return clientCredentials, err
}
return clientCredentials, nil
}
func handleMessage(s xmpp.Sender, p stanza.Packet) {
log.Printf("Handle message")
msg, ok := p.(stanza.Message)