goa

A New Approach to Microservice Development in Go

11 July 2016

A Bit About Me

Raphael Simon – Systems Architect at RightScale

A Bit On goa

goa is a novel approach to building microservices that starts with the design.

goa has three parts:

A Bit On The Talk

API Design

Designing The Wine Review App

We have a new customer!

and they want us to build a wine review app!!

The API

RESTful API with the following resources:

Looking Good!

Creating bottles:

POST /bottles

Listing bottles:

GET /bottles

Creating reviews:

POST /bottles/1/reviews

Listing reviews:

GET /bottles/1/reviews

UI Needs

We'd like to show all the reviews made by a user

simple!

GET /bottles

then for each

GET /bottles/:id/reviews

uh oh...

Satisfying UI Needs

GET /reviews

hmmm

GET /reviews?bottles=1

V2

Creating reviews:

POST /reviews

Listing all reviews:

GET /reviews

Listing reviews for a bottle:

GET /reviews?bottle=1

Creating bottles:

POST /bottles

Listing bottles:

GET /bottles

API Design Life Cycle

Designing an API is an iterative process.

Designing GOOD APIs Is Surprisingly Hard

as a reviewer

as a developer

goa's goal is to enable good design practices while helping both API developers and API reviewers.

Enters goa

Hello, goa

Implementing the Design

Live Coding

What Just Happened?

goagen generates a tool by compiling the command specific code generation algorithm together with the design package.

goagen then executes the tool:

Anatomy of a goa Service

The Stack

goa Library vs. Generated Code

Request Flow

Live Coding

Enabling the API Design Lifecycle

1. Code Separation Principle

Use standard language constructs to integrate generated code with user code.

⇨ Generated code lives in different package that is re-generated completely each time.

2. Boilerplate Only

Applied to goa:

3. Generation Must Be Idempotent

Know the effects of changing the input

Applied to goa:

Adding Documentation to TWRS

Live Coding

Enabling the API Life cycle

⇨ mount controller on service

⇨ won't compile until implemented

⇨ may also break compilation depending on changes

⇨ ditto

Changes are intuitive, compilation fails like you would expect.

Other Goodies

Stepping Back

With goa:

The design code is the Single Source Of Truth.

Advanced Topics

Connecting Services

Generated client provides a base on which client services can build specialized clients.

package acl

import "public_service/clients/acl/client"

type Client struct {
    *client.Client
}

Sharing Designs

Design can be shared across different services for example to share type definitions.

package design

import . "github.com/goadesign/goa/design"
import . "github.com/goadesign/goa/design/apidsl"
import . "cellar/design/public"

var CellarPayload = Type("BottlePayload", func() {
    Attribute("bottles", CollectionOf(BottlePayload), "Cellar bottles", func() {
        MinLength(1)
    })
})

Security

var JWT = JWTSecurity("jwt", func() {
    Header("Authorization")
})

// Resource jwt uses the JWTSecurity security scheme.
var _ = Resource("jwt", func() {
    Description("This resource uses JWT to secure its endpoints")
    DefaultMedia(SuccessMedia)

    Security(JWT)
    // ...

Creating New DSL

goa's DSL engine is built to support arbitrary DSLs.

Plugins can:

var _ = StorageGroup("MyAPIStorage", func() {
    Store("postgres", gorma.Postgres, func() {
        Model("User", func() {
            BuildsFrom(func() { Payload("user", "create") })
            RendersTo(User)
            Field("id", gorma.Integer, func() {
                PrimaryKey()
                Description("This is the User Model PK field")
            })
        })
    })
})

Thank You To All The Contributors!

Many features implemented by contributors, thank you!

Conclusion

Design First!

Next time you implement http.Handler,

ask yourself whether you shouldn't be writing the design instead.

Thank you

Use the left and right arrow keys or click the left and right edges of the page to navigate between slides.
(Press 'H' or navigate to hide this message.)