Skip to content

Latest commit

 

History

History
114 lines (83 loc) · 2.83 KB

README.md

File metadata and controls

114 lines (83 loc) · 2.83 KB

(WIP) Go API Server Template

  • An opinionated template for building Go RESTful APIs using Hertz and fx.

  • Goals:

    • Provide a quick starting point for building HTTP RESTful APIs in Go that require integration with many third-party services.
    • No need to worry about the boilerplate code.
    • Tries not to be another enterprise layer cake.
    • Tries to use the best practices for building APIs in Go.
  • Not meant to be a one-size-fits-all solution, e.g., serverless functions, large monorepos, specific domain requirements, etc.

Table of Contents

Usage

  1. Install necessary tools:

    • Code generator:
    go install github.com/cloudwego/hertz/cmd/hz@latest
    • Protobuf compiler:

      • For MacOS:
      brew install protobuf
      • For Ubuntu:
      sudo apt install protobuf-compiler
      • For Windows:
      choco install protoc
    • Go plugin for Protobuf compiler:

    go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
  2. Generate the project:

    make hz-init
  3. Run the server:

    make up
  4. Update the API:

    • Update the .proto files in the idl (interface definition language) folder.
    • Run the following command to generate the code:
    make hz-update

Generated Folder Layout

.
|-- cmd                     # Main applications of the project 
|   |-- api.go                # Command for the API server
|   |-- root.go               # Root command of the project
|   `-- version.go            # Command for the version
|-- core                    # Core packages of the project
|   |-- handler               # Handlers for the API server
|   |   `-- hello
|   |       `-- hello_service.go
|   |-- model                 # Models for the API server
|   |   |-- api                # Generated API models
|   |   |   `-- api.pb.go
|   |   `-- hello
|   |       `-- hello.pb.go
|   `-- router                # Router for the API server
|       |-- hello
|       |   |-- hello.go
|       |   `-- middleware.go
|       `-- register.go        # Register the routes
|-- go.mod
|-- go.sum
|-- idl                    # Interface definition language
|   `-- protobuf
|       `-- v1
|           |-- api.proto
|           `-- hello.proto
|-- main.go                # Main entry point of the project

Customization