Integrate commonly used libraries in Golang, reduce the threshold of dependency management, and improve the efficiency of project construction and docking of various libraries.
Based on the principle that convention is greater than configuration, reduce configuration management work. Combined with anonymous import to complete the registration and initialization of the library, focus more on business.
Taking the GIN Web project as an example, the sequence in which a project is created based on boot is as follows
- Create a directory
mkdir boot-demo && cd boot-demo
, initializego mod init github.com/jsmzr/boot-demo
- Pull dependencies,
go get -u github.com/jsmzr/boot
- Import the plug-ins you need to use in
main.go
- update dependencies
go mod tidy
- Complete the interface logic writing, add swagger doc
- Write routing rules
- register router by
func init
inrouter.go
- run
swag init
generate doc, and importdocs
inmain.go
- use
bootGin.Run()
start
// main.go
package main
import (
"fmt"
bootGin "github.com/jsmzr/boot/gin"
// swagger plugin
_ "github.com/jsmzr/boot/gin/swagger"
// logrus plugin
_ "github.com/jsmzr/boot/log/logrus"
// use swag init generate
_ "github.com/jsmzr/boot-demo/docs"
// gin router register
_ "github.com/jsmzr/boot-demo/router"
)
func main() {
if err := bootGin.Run(); err != nil {
fmt.Println(err)
}
}
// router/router.go
package router
import bootGin "github.com/jsmzr/boot/gin"
func init() {
bootGin.RegisterRouter(InitDemo)
}
// router/demo.go
package router
import (
"github.com/gin-gonic/gin"
"github.com/jsmzr/boot-demo/controller"
)
func InitDemo(e *gin.Engine) {
e.GET("/hello", controller.Hello)
}
// controller/demo.go
package controller
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
// @Tags demo
// @Summary hello world
// @Router /hello [get]
func Hello(c *gin.Context) {
logrus.Info("request /hello")
c.String(http.StatusOK, "hello world!")
}
- log
- logrus
- zap
- config
- apollo
- api document
- swagger(gin)
- trace
- skywalking(gin)
- mertrics
- prometheus
- database
- mysql
- oracle
- cache
- redis