Page tree
Skip to end of metadata
Go to start of metadata

Registration with REST SDK

Import the package

go get code.linksmart.eu/sc/service-catalog/catalog

Sample code:


package main

import (
   "time"

   "code.linksmart.eu/sc/service-catalog/catalog"
   "code.linksmart.eu/sc/service-catalog/client"
)

func main() {
   service := catalog.Service{
      ID:          "unique_id",
      Description: "service description",
      APIs:        map[string]string{"API 1": "http://localhost:8080"},
      Docs: []catalog.Doc{{
         Description: "doc description",
         URL:         "http://doc.linksmart.eu/DC",
         Type:        "text/html",
         APIs:        []string{"API 1"},
      }},
      Meta: map[string]interface{}{"pub_key": "qwertyuiopasdfghjklzxcvbnm"},
      TTL:  10,
   }

   stopRegistrator, _ := client.RegisterServiceAndKeepalive("http://localhost:8082", service, nil)

   time.Sleep(22 * time.Second)
   // call this on interrupt/kill signal to remove service immediately, otherwise the service is removed after expiry
   stopRegistrator()
}

RegisterServiceAndKeepalive registers the service (or updates if it already exists) and updates it continuously so that it doesn't expire. 


Registration via an MQTT Broker

package main


import (
    "log"
    "time"

    paho "github.com/eclipse/paho.mqtt.golang"
)

func main() {
    // Create MQTT client and connect
    opts := paho.NewClientOptions()
    opts.AddBroker("tcp://localhost:1883")
    client := paho.NewClient(opts)
    if token := client.Connect(); token.Wait() && token.Error() != nil {
        log.Fatal(token.Error())
    }

    APIEndpoint := "mqtt://test.mosquitto.org:1883"
    payload := `{
        "id": "unique_id",
        "description": "service description",
        "meta": {},
        "apis": {
            "Data API" : "` + APIEndpoint + `"
        },
        "docs": [
            {
                "description": "LS XYZ Documentation",
                "apis": ["Data API"],  
                "type": "text/html",
                "url": "http://docs.linksmart.eu/XYZ"
            }
        ],
        "ttl": 10
    }`

    ticker := time.NewTicker(time.Second * 5)
    go func() {
        for _ = range ticker.C {
            log.Print("Updating...")
            if token := client.Publish("LS/MOCK/1/SER/1.0/REG", 1, false, payload); token.Wait() && token.Error() != nil {
                log.Print(token.Error())
            }
        }
    }()

    time.Sleep(22 * time.Second)
    ticker.Stop()
    // a proper will message leads to immediate service removal, otherwise the service is removed after expiry
}

The service object can also be imported from the catalog package and serialized into json; See the REST SDK.

package main

import (
"time"

"code.linksmart.eu/sc/service-catalog/catalog"
"code.linksmart.eu/sc/service-catalog/client"
)

func main() {
service := catalog.Service{
ID: "unique_id",
Description: "service description",
APIs: map[string]string{"API 1": "http://localhost:8080"},
Docs: []catalog.Doc{{
Description: "doc description",
URL: "http://doc.linksmart.eu/DC",
Type: "text/html",
APIs: []string{"API 1"},
}},
Meta: map[string]interface{}{"pub_key": "qwertyuiopasdfghjklzxcvbnm"},
TTL: 10,
}

stopRegistrator, _ := client.RegisterServiceAndKeepalive("http://localhost:8082", service, nil)

time.Sleep(22 * time.Second)
// call this on interrupt/kill signal to remove service immediately, otherwise the service is removed after expiry
stopRegistrator()
}
  • No labels