-
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
- Notice for Windows users
-
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
-
Generate the project:
make hz-init
-
Run the server:
make up
-
Update the API:
- Update the
.proto
files in theidl
(interface definition language) folder. - Run the following command to generate the code:
make hz-update
- Update the
.
|-- 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