Accepting HTTP connections with Neoroute

Using a HTTP transporter with Neoroute allows you to accept requests from clients over HTTP. However, unlike traditional HTTP frameworks, all of the routes are called through one central endpoint.

Note: The HTTP transporter only supports accepting requests. Events can not be sent through it since HTTP is a request-response protocol. If you want to send events as well, you could consider WebSocket.

Installation

Install the http_transporter package with go:

go get -u github.com/Liphium/neoroute/transporter/http@latest

Usage

While creating the transporter is really simple, you’re going to need a router to actually add routes (things receiving requests) to your server.

Creating the transporter

You can now create a new transporter like this:

import (
	"net/http"
	http_transporter "github.com/Liphium/neoroute/transporter/http"
)

hook, t := http_transporter.NewTransporter(router, http_transporter.Config[neoroute.NoData]{
	// In this function, you can set data that the session (an object you get in all routes),
	// will contain.
	// If you return false here, the request will not go through.
	HandshakeFunc: func(r *http.Request) (neoroute.NoData, bool) {
		return neoroute.NoData{}, true
	},
})

Now to explain what you get back:

  • hook is a HTTP handler (http.HandlerFunc) you can mount into almost any Go Web Framework to accept Neoroute requests.
  • t is the actual transporter powering the whole thing. For HTTP, this object does actually not have a lot of use, but you can for example put it into neogen for schema and code generation.

Mounting the hook

While we now have a hook, we don’t actually have a HTTP server that serves the endpoint yet. While we can’t possibly cover every framework, here is how you mount it into a few popular ones.

If your framework is not covered here, you can just look up how to mount a http.HandlerFunc in your framework of choice. We would also appreciate a contribution if you’re using a popular framework we’re not covering here.