servicemanager

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2020 License: MIT Imports: 16 Imported by: 0

README

Simple and lightweight service manager for simple microservices

Simple is the key, service manager created to be used inside docker containers to "share" the endpoint of different services to a "gateway" or "parent" service.

There is no authentication or complex endpoints, the only thing the client has to do is periodically call an endpoint with the name, endpoint and (optionality) a value.

The client can use GET or POST

http://service-manager:8080/register?service=MyServiceName&endpoint=http://10.0.0.155:3000&value=OK

You can get the registered services on the endpoint /services.

That's it.

If you really wants, thats webhook support. You can provide a comma separated list of url's to be called when an service changes.

Example docker-compose.yml

version: "3.7"

services:
    service-manager:
        restart: unless-stopped
        image: crossworth/service-manager
        environment:
          - CHANGES_WEBHOOK=http://my-gateway/check-services,http://auth-api:9000/check-services

There is a simple client as well.


import (
 	"log"
	"github.com/crossworth/service-manager/client"
)

const service = "My-Service-Name"

func init() {
	if os.Getenv("SERVICE_MANAGER") != "" {
		log.Println("ServiceManager notifications enable")

		localIp, err := client.GetLocalIP()
		if err != nil {
			log.Fatal("could not determine local ip", err)
		}


		var opts client.Options
		opts.Name = service
		opts.ManagerEndpoint = os.Getenv("SERVICE_MANAGER")
		opts.Endpoint = localIp + ":8080"
		opts.Value = func() string {
			return "my-value"
		}

		client.Register(opts)
	}
}

Documentation

Index

Constants

View Source
const ServiceName = "service-manager"

The service name and signature

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

type Server struct {
	chi.Router
	// contains filtered or unexported fields
}

Server is main application struct

func New

func New(timeToLive time.Duration, webhookUrls []string) (*Server, error)

New create a new service manager with the provided time to live and webhook urls, if any webhook url is provided it start a background job to check and notify services changes to the webhook urls

func (*Server) GetServices

func (s *Server) GetServices() []ServiceInfo

GetServices returns all the registered services

func (*Server) RegisterService

func (s *Server) RegisterService(service ServiceInfo)

RegisterService register a new service or replace it with the default time to live

func (*Server) SetHttpClient

func (s *Server) SetHttpClient(client *http.Client)

SetHttpClient allows setting a custom http client used on the webhook calls

type ServiceInfo

type ServiceInfo struct {
	Name     string      `json:"name"`
	Endpoint string      `json:"endpoint"`
	Value    interface{} `json:"value,omitempty"`
}

ServiceInfo is the representation of a registered service

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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