Skip to content

grimerssy/go-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Project setup made easy


Introduction

This is a template repository, which can be used to create backend projects in Go programming language.
It uses proto programming language to create an API definition, which is used to generate stubs for gRPC and REST endpoints as well as openAPI.v2 JSON specification.
Think of end product as gRPC service with REST proxy (which is created with minimal effort using grpc-gateway).
Also buf is used to manage proto dependencies without manually downloading files and placing them in your project.
This template has some pre-imported proto libraries (i.e. googleapis/googleapis, envoyproxy/protoc-gen-validate and grpc-ecosystem/grpc-gateway)

Examples

  • go-example - simple to understand project that uses this template, which will introduce you to the technologies I use

Prerequisites

  • Go binary installed (link).
  • Buf binary installed (link).

Nice to have

  • Buf extension in your IDE of choice.
    This will include real-time .proto file linting and provide additional intellisense.

Project setup

  • Click Use this template button and create your repository.
  • Clone your repository to desired directory on the local machine.
    (using git clone or git tools in your IDE).
  • Run make init command in your project root.
    This will install required protoc binaries to your $GOPATH/bin directory.
  • Change awesomeProject in go.mod file to the name of your go module.
    Recommended prefixing it with github.com/{your_username}/ and naming it the same as your repository.
    Example: module github.com/grimerssy/go-template.
  • Go to buf.gen.yaml file and change awesomeProject to your module name in the go_package_prefix.default field.

Get coding

  • Write your first .proto file in /api/proto/{proto_package} directory.
    It may look like /api/proto/v1/helloworld.proto
    or /api/proto/helloworld/v1/helloworld.proto.
  • Run make gen command.
    This will generate API specification (api/spec/helloworld.swagger.json file).
    And .pb.go files in /internal/api/helloworld/v1 or /internal/api/v1 directory
    corresponding to your .proto package name.
  • Write your handlers, use cases and repositories, set up gRPC and/or HTTP servers.
    Note: for more help, you can find a README.md file in each directory of this repository.

Tools

  • To test or run your project run make test or make run commands respectfully.
  • Buf makes it easier to import third party .proto packages without manually copying them to your /third_party directory.
    To manage your dependencies go to /api/proto/buf.yaml file and make changes in deps field.
    Note: try to keep deps in /api/proto/buf.yaml and go_package_prefix.except in buf.gen.yaml in sync to avoid incorrect Go import paths.
    After all the above, run make update command.
    If you have buf extension installed, updated dependencies will be available in the intellisense.
  • To learn more about buf tool, visit introduction to buf.