http2amqp

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2021 License: MIT Imports: 12 Imported by: 0

README

http2amqp

Build Status GoDoc License

HTTP interface to AMPQ.

Features

  • It publishes an AMQP request message for each HTTP request received. Then it waits for the corresponding AMQP response message in order to answer to the original HTTP request.
  • The topic of the message that it publishes comes from the method and the path of the HTTP request, replacing slashes (/) with dots (.).
    • e.g.: GET http://localhost:18080/net/testget.net.test
  • The topic of the response messages to close the connection is queries.response. These response messages must carry the same ID as the request they intend to close.
  • The default TTL for the published messages is 1000 ms. After that, the connection will be closed with a timeout if a response did not arrive.
  • The exchange used by default is 'events'.

Build

You need a Go runtime installed in your system which supports modules. A nice way to have multiple Go versions and switch easily between them is the g application.

A Makefile is available, so you only need to run:

make build

Running tests

Make sure that you have built the binaries from the previous section, because the tests will run the echoservice binary.

Load environment variables to set the BROKER_URI environment variable.

source dev/env_develop

Start a RabbitMQ service with default configuration (specified in /dev/env_develop).

make start_dependencies

Run tests. They will only work if the RabbitMQ container is up.

make test

Building docker images

The Jenkins pipeline will generate two containers, which we can also build locally. One for compiling the app (http2amqp-builder), and the other to deploy its binary (http2amqp). This avoids the need of having Go installed in the host system. Take a look at the Dockerfile for more details.

make build_images

Once built, if we want to run the tests inside the builder image, we can do the following (remember to have the environment variables loaded and the RabbitMQ container up too):

docker run --rm -it --net=host aleasoluciones/http2amqp-builder:GIT_REV integration-tests

And to run http2amqp from inside the container:

docker run --rm -it --net=host -e BROKER_URI=amqp://guest:guest@localhost:5666/ aleasoluciones/http2amqp:GIT_REV

Usage

$ ./http2amqp -help
Usage of ./http2amqp:
  -address string
    	HTTP listen IP address (default "0.0.0.0")
  -brokeruri string
    	AMQP broker connection URI (default "amqp://guest:guest@localhost/")
  -exchange string
    	AMQP broker exchange name (default "events")
  -port string
    	HTTP listen port (default "18080")
  -timeout int
    	AMQP broker queries timeout in milliseconds (default 1000)
  -verbose
    	Verbose mode, enable logging

Execution example

Make sure that the environment variables are loaded before executing each command. Also that you have a RabbitMQ container up and running with those parameters.

Start the htt2amqp server in a terminal.

./http2amqp -verbose

Start the echo service in another terminal. This will publish a response event each time a request event arrives, with the same ID and payload (ping pong).

./echoservice

Make a request with a payload. The echo service will answer back if running, otherwise it will timeout.

curl -X GET http://localhost:18080/net/test -d 'hello world'

You can specify the timeout (in milliseconds) as a query param.

curl -X GET http://localhost:18080/net/test?timeout=200 -d 'hello world'

TODO

  • Test timeout parameter for each request.
  • Implement delay parameter for echo server to allow tests timeouts.

Notes

  • We compile in the Dockerfile with CGO_ENABLED=0 because in the scratch image there are not some C libraries it needs. So we compile the application with them in order to run it properly. See https://go.dev/blog/cgo for more info.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHTTPServerFunc

func NewHTTPServerFunc(service *Service) func(w http.ResponseWriter, r *http.Request)

NewHTTPServerFunc return a function to process a http request

Types

type AmqpRequestMessage

type AmqpRequestMessage struct {
	ID            string  `json:"id"`
	Request       Request `json:"request"`
	ResponseTopic string  `json:"responseTopic"`
}

AmqpRequestMessage wrapper type for a Request including a ID and the topic to use

type AmqpResponseMessage

type AmqpResponseMessage struct {
	ID       string   `json:"id"`
	Response Response `json:"response"`
}

AmqpResponseMessage wrapper type for a Response including a ID and the response

type IdsGenerator added in v0.4.1

type IdsGenerator interface {
	Next() string
}

IdsGenerator generate a diferent ID each time Next is called

func NewUUIDIdsGenerator added in v0.4.1

func NewUUIDIdsGenerator() IdsGenerator

NewUUIDIdsGenerator return a uuid

type Request

type Request struct {
	Method string      `json:"method"`
	URL    *url.URL    `json:"url"`
	Header http.Header `json:"header"`
	Body   []byte      `json:"body"`
}

Request http request info to publish to amqp broker

type Response

type Response struct {
	Status int         `json:"status"`
	Header http.Header `json:"header"`
	Body   []byte      `json:"body"`
}

Response info to generate a http response to return to the original http client

type Service added in v0.4.1

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

Service http2amqp service

func NewService added in v0.4.1

func NewService(brokerURI, exchange string, timeout time.Duration) *Service

NewService return the http2amqp service. This service publish a amqp message for each http request and process the corresponding amqp responses to answer to the original http request

func (*Service) DispatchHTTPRequest added in v0.4.1

func (service *Service) DispatchHTTPRequest(topic string, request Request) (Response, error)

DispatchHTTPRequest process a request. Send the request to the broker using the given topic and wait for the response (or the timeout)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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