missy

module
v0.4.14 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2020 License: MIT

README

MiSSy

Slack Status Build Status Coverage Status Go Report Card

MiSSy (Microservice Support System) is an SDK for creating REST services that talk to each other. It provides the following functionality:

Features
  • Routing with gorrila/mux
  • Logging
  • Configuration with environment variables
  • Monitoring with Prometheus
  • /info /health
Roadmap
  • Service discovery
  • REST client
  • Security

How to use it

Example for a simple hello world service

Create a .missy.yml config file in the root directory of your service with the following content

name: hello
# main.go

package main

import (
	"github.com/microdevs/missy/service"
	"net/http"
	"fmt"
)

func main() {
	s := service.New("hello")
	s.HandleFunc("/hello/{name}", HelloHandler).Methods("GET")
	s.HandleFunc("/json", JsonHandler).Methods("GET")
	s.Start()
}

func HelloHandler(w http.ResponseWriter, r *http.Request) {
	vars := service.Vars(r)
	w.Write([]byte(fmt.Sprintf("Hello %s", vars["name"])))
}

// marshalling example
type MyType struct {
	A int
	B string
}

func JsonHandler(w http.ResponseWriter, r *http.Request) {
	mytype := MyType{
		A: 123,
		B: "Hello world",
	}
	
	data.Marshal(w, r, mytype)
}

// response body {A:123,B:"Hello world"}

Run it:

go run main.go

Call the Endpoint:
curl "http://localhost:8088/hello/microdevs"

Get Prometheus Metrics (all metrics are exposed on 8090 as default). To change this please set METRICS_LISTEN_PORT env. var.:

curl http://localhost:8090/metrics
Get Info:
http://localhost:8090/info

Response:

Name hello
Uptime 14.504883092s
Get Health:
http://localhost:8090/health

Response:

OK
Messaging

Use messaging.Reader and messaging.Writer to subscribe and publish messages. It uses kafka underneath. Example usage:

Reader with brokers hosts, group-id and topic

reader := messaging.NewReader([]string{"localhost:9092"}, "group-id", "topic")
err := reader.Read(func(msg Message) error{
    // do something with msg
    
    // return nil or error (if commit should not happen)
})

// remember to close reader after use
defer reader.Close()

#####Dead letter queue

If you need to save messages that couldn't be processed, you have to use constructor NewReaderWithDLQ which takes name of DLQ topic as additional parameter.

#####Writer with brokers hosts and topic

writer := messaging.NewWriter([]string{"localhost:9092"}, "topic")
err := writer.Write([]byte("key"), []byte("value"))

// remember to close writer after use
defer writer.Close()

Directories

Path Synopsis
certificates module
http module
Package messaging is a generated GoMock package.
Package messaging is a generated GoMock package.
Package service contains the logic to build a HTTP/Rest Service for the MiSSy runtime environment
Package service contains the logic to build a HTTP/Rest Service for the MiSSy runtime environment
url

Jump to

Keyboard shortcuts

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