Loaded credentials from input json file
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
.vscode
|
.vscode
|
||||||
|
input.json
|
||||||
@@ -2,7 +2,9 @@ package services
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
@@ -51,6 +53,15 @@ type XMPPClient struct {
|
|||||||
Client *xmpp.Client
|
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
|
// Instance - get instance of xmpp service
|
||||||
func Instance() *XMPPService {
|
func Instance() *XMPPService {
|
||||||
return &xmppService
|
return &xmppService
|
||||||
@@ -66,6 +77,15 @@ func Init() {
|
|||||||
}
|
}
|
||||||
xmppService.Router.HandleFunc("message", handleMessage)
|
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)
|
xmppService.XMPPClients = make([]XMPPClient, 0)
|
||||||
xmppClient := XMPPClient{
|
xmppClient := XMPPClient{
|
||||||
Config: xmpp.Config{
|
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) {
|
func handleMessage(s xmpp.Sender, p stanza.Packet) {
|
||||||
log.Printf("Handle message")
|
log.Printf("Handle message")
|
||||||
msg, ok := p.(stanza.Message)
|
msg, ok := p.(stanza.Message)
|
||||||
|
|||||||
Reference in New Issue
Block a user