Giter VIP home page Giter VIP logo

api-go-template's Introduction

api-go-template

Lines Of Code

A boilerplate Go repo that comes with:

These libraries do a lot of heavy lifting in terms of boilerplate.

For example:

  • The fx framework manages dependency injection and application life-cycle for you. (See example_test.go).
  • sqlboiler makes DB CRUD really simple. (See foo.go). Raw queries are supported as an escape hatch.

Project structure

Directory Description
./cmd CLI for making gRPC requests
./idl Protobufs (Interface Definition Language)
./internal/app App dependency injection / initialization
./internal/idl Auto-generated protobufs
./internal/models Auto-generated ORM / models
./internal/service Service layer / Business logic
./internal/service/db Data layer
./schema SQL migration scripts

Getting started

# Step 1: Start containers in detached (background) mode
docker-compose up -d

# Step 2: Create the database schema
make migrate-up

# Step 3: Start app
go run main.go

Finally, hit the API (using HTTPie)

# Create a new Foo
http POST \
  http://localhost:8081/coop.drivers.foo.v1beta1.FooService/CreateFoo \
    name="Kevin"

# Get existing Foo
http POST \
  http://localhost:8081/coop.drivers.foo.v1beta1.FooService/GetFoo \
    id="cb4c4rnrirfucgsert7g"

Or with curl:

curl -X POST http://localhost:8081/coop.drivers.foo.v1beta1.FooService/CreateFoo \
  -H "Content-Type: application/json" \
  -d '{"name": "Kevin"}'

curl -X POST http://localhost:8081/coop.drivers.foo.v1beta1.FooService/GetFoo \
  -H "Content-Type: application/json" \
  -d '{"id": "cb4c4rnrirfucgsert7g"}'

Database

Run Migrations

make migrate-up

or you can run:

docker run -v $(pwd)/schema:/migrations \
  --network host \
  --rm \
  migrate/migrate \
  -path=/migrations/ \
  -database postgres://postgres:postgres@localhost:5432/foo\?sslmode=disable \
  up

This will run all migrations in ./schema.

Create New Migration

To create a new migration called create-new-table, run:

docker run -v $(pwd)/schema:/migrations \
  --network host \
  --rm \
  migrate/migrate \
  -path=/migrations/ \
  create \
  -dir /migrations \
  -ext sql \
  create-new-table

This will create a new up and down migration in ./schema.

Auto-generate ORM DB Models

We have a sqlboiler command that introspects the DB and generates ORM models.

make gen-models

Configuration comes from sqlboiler.toml

Telemetry

Traces

See traces in Jaeger here

Metrics

See metrics in Prometheus here.

api-go-template's People

Contributors

kevinmichaelchen avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

rwoosley91

api-go-template's Issues

Remote Generation

Remote Generation

From remote generation docs:

A common frustration when working with Protocol Buffers is that you need to generate code for each language that you're working with. Many teams implement custom tooling and scripts to solve this problem, but it can be difficult to ensure that every person who works on a given project has all of the code generation tooling set up locally. And if you have Protobuf-based APIs, the consumers of your APIs shouldn't have to deal with code generation.

The Buf Schema Registry solves this problem with remote code generation. With this feature, you can eliminate code generation from your workflows and directly install code generated from Protobuf definitions using standard package managers and build tools. This diagram illustrates how remote generation works:

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.