encr.dev

module
v0.16.3 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2021 License: MPL-2.0

README

Encore - Go framework for building distributed systems

encore.dev / Slack / Email / Twitter

Encore is a Go backend framework for rapidly creating APIs and distributed systems.

It uses static analysis and code generation to reduce the boilerplate you have to write, resulting in an extremely productive developer experience.

The key features of Encore are:

  • No boilerplate: Encore drastically reduces the boilerplate needed to set up a production ready backend application. Define backend services, API endpoints, and call APIs with a single line of Go code.

  • Distributed Tracing: Encore uses a combination of static analysis and code generation to automatically instrument your application for excellent observability. Automatically captures information about API calls, goroutines, HTTP requests, database queries, and more. Automatically works for local development as well as in production.

  • Infrastructure Provisioning: Encore understands how your application works, and uses that understanding to provision and manage your cloud infrastructure. Automatically works with all the major cloud providers (use your own account with AWS/Azure/GCP), as well as for local development.

  • Simple Secrets: Encore makes it easy to store and securely use secrets and API keys. Never worry about how to store and get access to secret values again.

  • API Documentation: Encore parses your source code to understand the request/response schemas for all your APIs. Encore can automatically generate high-quality, interactive API Documentation for you. It can also automatically generate type-safe, documented clients for your frontends.

Read the complete documentation at encore.dev/docs.

Quick Start

Install
# macOS
brew install encoredev/tap/encore
# Linux
curl -L https://encore.dev/install.sh | bash
# Windows
iwr https://encore.dev/install.ps1 | iex
Create your app
encore app create my-app
cd my-app
encore run
Deploy
git push encore
Setup Demo

Setup demo

Superpowers

Encore comes with tons of superpowers that radically simplify backend development compared to traditional frameworks:

  • A state of the art developer experience with unmatched productivity
  • Define services, APIs, and make API calls with a single line of Go code
  • Autocomplete and get compile-time checks for API calls
  • Generates beautiful API docs and API clients automatically
  • Instruments your app with Distributed Tracing, logs, and metrics – automatically
  • Runs serverlessly on Encore's cloud, or deploys to your own favorite cloud
  • Sets up dedicated Preview Environments for your pull requests
  • Supports flexible authentication
  • Manages your databases and migrates them automatically
  • Provides an extremely simple yet secure secrets management
  • And lots more...

Using Encore

Encore makes it super easy to create backend services and APIs.

Creating a service with an API

In Encore, a backend service is just a regular Go package with one or more APIs defined. The Go package name becomes the service name (which must be unique within your app).

package greet

import (
    "context"
    "fmt"
)

type Params struct {
    Name string
}

type Response struct {
    Message string
}

//encore:api public
func Person(ctx context.Context, params *Params) (*Response, error) {
    msg := fmt.Sprintf("Hello, %s!", params.Name)
    return &Response{Message: msg}, nil
}

This creates a backend service named greet, with a single API endpoint named Person.

Calling it is easy:

$ encore run  # run the app in a separate terminal
$ curl http://localhost:4060/greet.Person -d '{"Name": "Jane"}'
# Outputs: {"Message": "Hello, Jane!"}

Learn more in the Encore docs.

Calling an API endpoint

Calling an API endpoint from another endpoint is easy.

Just import the service (with a regular Go import), and then call the function as if it were a regular Go function:

import "my.app/greet"

func MyAPI(ctx context.Context) error {
    resp, err := greet.Person(ctx, &greet.Params{Name: "John"})
    if err != nil {
        return err
    }
    fmt.Println("The greeting message is:", resp.Message)
    return nil
}

Encore uses its static analysis and code generation to turn this into a proper API call.

Learn more in the Encore docs.

SQL Databases

Encore automatically provisions, connects to, and performs schema migrations of SQL databases for you.

All you have to do is define the SQL migrations:

-- greet/migrations/1_create_table.up.sql
CREATE TABLE person (
    name TEXT PRIMARY KEY,
    count INT NOT NULL
);

Then import encore.dev/storage/sqldb and just start querying:

// genGreeting generates a personalized greeting for the given name.
func genGreeting(ctx context.Context, name string) (string, error) {
    var count int
    // Insert the row, and increment count if the row is already in the db.
    err := sqldb.QueryRow(ctx, `
        INSERT INTO "person" (name, count)
        VALUES ($1, 1)
        ON CONFLICT (name) DO UPDATE
        SET count = person.count + 1
        RETURNING count
    `, name).Scan(&count)
    if err != nil {
        return "", err
    }

    switch count {
    case 1:
        return fmt.Sprintf("Nice to meet you, %s!", name), nil
    case 2:
        return fmt.Sprintf("Hi again, %s!", name), nil
    default:
        return fmt.Sprintf("Good to see you, %s! We've met %d times before.", name, count-1), nil
    }
}
Database Demo

Setting up a database

Learn more in the Encore docs.

API Documentation

Encore automatically generates API documentation for your app.

You can access it by viewing the local development dashboard by opening the API URL in your browser when your app is running (normally localhost:4060).

API Documentation

Distributed Tracing

Encore automatically instruments your app with Distributed Tracing.

For local development you can access it by viewing the local development dashboard by opening the API URL in your browser when your app is running (normally localhost:4060).

Any API calls to your app automatically produces traces.

Automatic Tracing

Contributing to Encore and building from source

See CONTRIBUTING.md.

Frequently Asked Questions (FAQ)

Who's behind Encore?

We're long-time Staff Engineers from Spotify, who grew frustrated with all the boilerplate and boring stuff you have to do to build modern cloud applications.

Why is the framework integrated with cloud hosting?

We've found that to meaningfully improve developer productivity you have to operate across the full stack. Unless you understand how an application is deployed, there are lots of things in the development process that you can't simplify. You can still use your own account with any of the major cloud providers (AWS/Azure/GCP), or you can use Encore's cloud for free, for Hobby projects, with pretty generous "fair use" limits.

Can I use an existing Kubernetes cluster with Encore?

Not right now. We definitely want to support deploying to an existing k8s cluster, and enable more flexible deployment topologies in general. It's a bit tricky since we set up the cluster in a certain way, and it's hard to know how the existing cluster is configured and we don't want to break any existing application that might be running there.

Can you have it provision in Kubernetes rather than a cloud infrastructure?

Right now we only support deploying Encore apps to Kubernetes. Either where we host it for you (using AWS under the hood), or you can tell Encore to deploy to your own cloud account. In that case we currently set up a new Kubernetes cluster.

Does Encore support using websockets?

Encore supports dropping down to plain HTTP requests which lets you use Websockets.

Get Involved

We rely on your contributions and feedback to improve Encore. We love hearing about your experiences using Encore, and about what may be unclear and we can do a better job explaining.

Directories

Path Synopsis
cli
cmd/git-remote-encore
Command git-remote-encore provides a gitremote helper for interacting with Encore's git hosting without SSH keys, by piggybacking on Encore's auth tokens.
Command git-remote-encore provides a gitremote helper for interacting with Encore's git hosting without SSH keys, by piggybacking on Encore's auth tokens.
daemon
Package daemon implements the Encore daemon gRPC server.
Package daemon implements the Encore daemon gRPC server.
daemon/dash
Package dash serves the Encore Developer Dashboard.
Package dash serves the Encore Developer Dashboard.
daemon/internal/appfile
Package appfile reads and writes encore.app files.
Package appfile reads and writes encore.app files.
daemon/internal/manifest
Package manifest reads and writes Encore app manifests.
Package manifest reads and writes Encore app manifests.
daemon/internal/sym
Package sym parses symbol tables from Go binaries.
Package sym parses symbol tables from Go binaries.
daemon/run
Package run starts and tracks running Encore applications.
Package run starts and tracks running Encore applications.
daemon/secret
Package secret fetches and caches development secrets for Encore apps.
Package secret fetches and caches development secrets for Encore apps.
daemon/sqldb
Package sqldb runs and manages connections for Encore applications.
Package sqldb runs and manages connections for Encore applications.
internal/browser
Package browser provides utilities for interacting with users' browsers.
Package browser provides utilities for interacting with users' browsers.
internal/codegen
Package codegen generates code for use with Encore apps.
Package codegen generates code for use with Encore apps.
internal/conf
Package conf writes and reads the Encore configuration file for the user.
Package conf writes and reads the Encore configuration file for the user.
internal/env
Package env answers where Encore tools and resources are located.
Package env answers where Encore tools and resources are located.
internal/gosym
Package gosym implements access to the Go symbol and line number tables embedded in Go binaries generated by the gc compilers.
Package gosym implements access to the Go symbol and line number tables embedded in Go binaries generated by the gc compilers.
internal/jsonrpc2
Package jsonrpc2 is a minimal implementation of the JSON RPC 2 spec.
Package jsonrpc2 is a minimal implementation of the JSON RPC 2 spec.
internal/jsonrpc2/servertest
Package servertest provides utilities for running tests against a remote LSP server.
Package servertest provides utilities for running tests against a remote LSP server.
internal/login
Package login handles login and authentication with Encore's platform.
Package login handles login and authentication with Encore's platform.
internal/wgtunnel
Package wgtunnel sets up and configures Encore's WireGuard tunnel for authenticating against private environments.
Package wgtunnel sets up and configures Encore's WireGuard tunnel for authenticating against private environments.
internal/xos
Package xos provides cross-platform helper functions.
Package xos provides cross-platform helper functions.
Package parser parses Encore applications into an Encore Syntax Tree (EST).
Package parser parses Encore applications into an Encore Syntax Tree (EST).
est
Package est provides the Encore Syntax Tree (EST).
Package est provides the Encore Syntax Tree (EST).
paths
Package paths parses API paths.
Package paths parses API paths.
pkg
pgproxy/internal/pgio
Package pgio is a low-level toolkit building messages in the PostgreSQL wire protocol.
Package pgio is a low-level toolkit building messages in the PostgreSQL wire protocol.

Jump to

Keyboard shortcuts

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