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.

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.