architect

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2019 License: GPL-3.0 Imports: 5 Imported by: 0

README

Architect Go SDK

This is the Go SDK used for brokering connections to microservice dependencies via Architect's deployment platform. If you're unfamiliar with the platform or our deploy tools, please check out Architect.io and our CLI to get started.

SDK Documentation

Installation

The latest stable version of the Go SDK can be found here and installed with the command

go get github.com/architect-team/go-sdk

Connecting to dependencies

import (
    architect "github.com/architect-team/go-sdk"
)

func add() {
    additionService := architect.Service("architect/addition-service")
    params := map[string]string{}
    params["first"] = first
    params["second"] = secondString
    
    // Client used to connect to service
    response := additionService.Client().Get("/add", params)
}
REST

The Architect SDK uses Resty to broker communication to downstream REST microservices. The client that is provided is an HTTP session that is enriched with the proper service location meaning only URIs and HTTP actions need be provided:

response := additionService.Client().Get("/add", params)
response := additionService.Client().Post("/add", params)
response := additionService.Client().Put("/add", params)
response := additionService.Client().Delete("/add", params)

Connecting to data stores

Since there are so many DB clients available, we don't want to choose a default for developers. Instead, the Architect SDK provides easy mechanics to parse credentials provided by the platform:

datastore := architect.Datastore("primary")
dbName := datastore["name"].(string)
user := datastore["username"].(string)
password := datastore["password"].(string)
db, _ := sql.Open("postgres", "user=" + user + "password=" + password + "dbname=" + dbName)
db.Close()

Events and messaging

The Architect platform also supports pub/sub based communication between services. The primary use-case for this flow would be to allow services to broadcast events for other services to subscribe to without needing to know who the subscribers are.

Subscribing
// architect.json
{
  "subscriptions": {
    "<service_name>": {
      "<event_name>": {
        "uri": "/event/callback",
        "headers": {
          "x-custom-header": "example"
        }
      }
    }
  }
}

The URI used for registration must match a URI on your service:

func callbackEvent(c echo.Context) error {
    // Custom event handling here
}

echoServer := echo.New()
echoServer.GET("/event/callback", callbackEvent)
echoServer.Logger.Fatal(echoServer.Start(os.Getenv("HOST") + ":" + os.Getenv("PORT")))
Publishing

NOTE: simple publication methods coming soon. For now, you can iterate through subscribers to submit events.

// architect.json
{
  "notifications": ["<event_name>"]
}
client := *resty.New()
for _, subscription := range architect.Notification("eventName").Subscriptions() {
    client.SetHostURL(subscription["HOST"] + ":" + subscription["port"])
	client.R().Post(subscription["uri"])
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Datastore

func Datastore(datastoreName string) (map[string]interface{}, error)

Datastore lookup a datastore for the current service

Types

type ArchitectService

type ArchitectService struct {
	// contains filtered or unexported fields
}

func CurrentService

func CurrentService() (*ArchitectService, error)

CurrentService helper to get running service

func Service

func Service(serviceName string) (*ArchitectService, error)

Service lookup a service dependency

func (*ArchitectService) Client

func (service *ArchitectService) Client() *resty.Client

func (*ArchitectService) Config

func (service *ArchitectService) Config() map[string]interface{}

type Notifier

type Notifier struct {
	// contains filtered or unexported fields
}

func Notification

func Notification(eventName string) (*Notifier, error)

Notification lookup a notification for the current service

func (Notifier) Subscriptions

func (notifier Notifier) Subscriptions() ([](map[string]interface{}), error)

Subscriptions find all of the service subscribers

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL