composure

command module
v0.0.0-...-370828f Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2014 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Composure

Build Status GoDoc

Composure is a lightweight server-side UI composition framework for building web applications with microservices.

  • Compose distinct UIs and APIs into one service
  • Transform HTTP requests and responses
  • Define compositions in JSON
  • Load templates and components from remote services
  • Asynchronous rendering - as fast as your slowest component
  • Integrate legacy services to avoid component rewrites
Quick start

💡 You can specify a URL to load, e.g. composure http://your/spec.json

⚠ Composure is an experimental product, not suitable for production environments.

Composure web application example

This is an example of using Composure to combine web components written in multiple languages into one user interface.

Example JSON specification

This example specification implements the diagram above.

It uses the following configuration:

  • Preflight

    The Authorization header is mapped to X-Composure-Authorization.

    The request is synchronously sent to an authentication server, which validates X-Composure-Authorization and responds with X-Composure-Identity.

    X-Composure-Identity is mapped to X-Identity for downstream requests.

  • Components

    Requests to render each component are sent asynchronously.

    Rendering services use X-Identity to identify the user.

  • Template

    Renders the template synchronously using the component output.

  • Postflight

    Transforms the final response synchronously, to remove the X-Identity header and set the Server header to Composure.

{
  "/": {
    "Template": {
      "Type": "HTTP",
      "Request": [ "http://templates.foobar/index.tmpl" ]
    },
    "Components": {
      "Header": {
        "Type": "HTTP",
        "Request": [ "http://header.foobar" ]
      },
      "Navbar": {
        "Type": "HTTP",
        "Request": [ "http://navbar.foobar" ]
      },
      "Content": {
        "Type": "HTTP",
        "Request": [ "http://newsfeed.foobar" ]
      },
      "Footer": {
        "Type": "HTTP",
        "Request": [ "http://footer.foobar" ]
      },
    },
    "Preflight": {
      "Type": "HTTP",
      "Request": [{
        "URL": "http://authentication.foobar/preflight",
        "Method": "GET",
        "Headers": {
          "$COPY": {
            "Authorization": "X-Composure-Authorization"
          }
        }
      }],
      "Response": [{
        "Headers": {
          "$REMOVE": [ "X-Composure-Authorization" ],
          "$COPY": {
            "X-Composure-Identity": "X-Identity"
          }
        }
      }]
    },
    "Postflight": {
      "Type": "HTTP",
      "Response": [{
        "Headers": {
          "$SET": {
            "Server": "Composure"
          },
          "$REMOVE": [ "X-Identity" ]
        }
      }]
    }
  }
}
JSON specification

Composition names beginning with / are routes served by composure.

Routes are defined using Gorilla Pat.

Other compositions are reserved for internal use, e.g. to build reusable components.

  • Preflight is executed synchronously before rendering takes place
  • Components are rendered asynchronously after Preflight
  • Template is rendered synchronously after all Components are rendered
  • Postflight is executed synchronously after all rendering is complete

On a route composition, you can optionally specify HTTP methods:

"/": {
  "Methods": [ "GET" ]
}

If no methods are specified, GET is registered by default.

Route placeholders

Gorilla Pat routes can contain placeholders, e.g. /product/{id}.

These are captured in query string parameters prefixed with a colon.

For example, id becomes :id.

These values can be used in later requests, e.g. to proxy an entire service.

Composition

Each child object is a "composition":

"Navbar": {
  "Template": {
    "Type": "Text",
    "Value": "<p>Nav bar!</p>"
  }
}

A composition consists of a template and, optionally, some components.

Templates are essentially Go html/template templates, and components are injected into the template using html/template syntax, e.g.

{{ .Navbar }}
{{ .Content }}

This means you can use logic in your templates to control rendering of remote components as if they were local variables, e.g.

{{ if .Admin }}
  <div class="admin_nav">{{ .Admin }}</div>
{{ end }}

The Template, Preflight and Postflight parameters are all Components, and can use any of the parameters defined below.

Component

A component is a JSON object specifying a Type:

"Navbar": {
  "Type": "Composition",
  "Name": "Navbar"
}

Additional items are passed to the component:

  • HTTP - requests a HTTP resource, or modifies the existing one
  • Text - uses the raw text in Value
  • Composition - renders composition named in Name from this composure
HTTP requests and responses

The HTTP component type supports simple and complex requests:

The simple form takes a single string argument, generating a GET request which inherits the original request properties (e.g. query string and headers).

"Content": {
  "Type": "HTTP",
  "Request": [ "http://localhost:9000" ]
}

The complex form takes a JSON object, allowing custom request and response handling:

"Content": {
  "Type": "HTTP",
  "Request": [{
    "URL": "http://localhost:9000",
    "Method": "GET",
    "Headers": {
      "$INHERIT": "1",
      "$REMOVE": [
        "X-Composure-Test"
      ],
      "$COPY": {
        "X-Composure-Test": "X-Composure-Copied"
      },
      "$SET": {
        "X-Composure": "1"
      },
      "$ADD": {
        "X-Foo": "1",
        "X-Bar": "2"
      }
    }
  }]
}

You can use placeholders in the request URL, for example:

"/product/{id}": {
  "Type": "HTTP",
  "Request": [ "http://localhost:9000/products/view/{{:id}}" ]
}

Header manipulation takes place in the following order:

  • $INHERIT copies all request parameters
  • $REMOVE removes a header
  • $COPY copies a specific header (which can be renamed)
  • $SET sets a header, overwriting it if it already exists
  • $ADD adds a header, keeping existing headers with the same name
Extending Composure

Composure is designed to be used as a standalone service.

It can also be extended using Go to provide additional functionality, for example:

  • Inline pre- and post-flight transformations for faster response times
  • Validating user authentication before invoking UI services
  • Performing centralised rate limiting away from UI code
Load a composure

You can load a composure from an io.File object:

f, _ := os.Open("spec.json")
spec, err := composition.Load(f)

Or you can parse JSON bytes directly:

spec, err := composition.ParseJSON([]byte(`{...}`))
Render a composition

You can render a composition either with or without a HTTP request context. The request context is important if you need to preserve or manipulate the request headers or body.

To render without a context, simply pass in nil.

func HandleRequest(w http.ResponseWriter, req *http.Request) {
  cm := spec.Get("/")
  err := cm.RenderFor(w, req)
}

For a more detailed example, see main.go.

Contributing

Clone this repository to $GOPATH/src/github.com/ian-kent/Composure

Run make to install required dependencies and build Composure.

Run tests using make test or goconvey.

If you make any changes, run go fmt ./... before submitting a pull request. This will be done automatically if you use make.

Licence

Copyright ©‎ 2014, Ian Kent (http://www.iankent.eu).

Released under MIT license, see LICENSE for details.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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