Golang back-end service template. Using this template, you can get started with back-end projects quickly.
Web Framework | ORM | Database Driver | Configuration Manager | Log Manager | API Documentation |
---|---|---|---|---|---|
labstack/echo/v4 | gorm.io/gorm | gorm.io/driver/mysql | spf13/viper | sirupsen/logrus | swaggo/echo-swagger |
- Create database and user in MySQL, and grant privileges to user.
CREATE DATABASE buz;
CREATE USER 'foo'@'localhost' IDENTIFIED BY 'bar';
GRANT ALL PRIVILEGES ON buz.* TO 'foo'@'localhost';
- Create a file named
conf.yaml
in the project's root directory.
sql:
username: foo
password: bar
db_name: buz
- To generate API doc, you need to execute the following commands:
go install github.com/swaggo/swag/cmd/[email protected] # download swag, a doc generator
swag init # docs will be written into `docs/` directory
- Compile and run this project. You will find API doc in
http://localhost:1323/api/doc/index.html
on default. - Change the go module name of this project to whatever you like.
- To create tables, add structs in
model/
.model/foo.go
is an example of such struct. Then, add the struct toDB.AutoMigrate
inmodel/init.go
. - Add business logics in
app/controller/
, then add router inapp/routers.go
. - Write API doc alongside with the controller function.
app/controller/foo.go
is an example.