Github project address: Go-Starter

Go-starter quickly develops scaffolding for small projects

In order to quickly develop and validate the minimum MVP, I integrated our common GO third-party library integration, such as Echo, SWAg, Viper, NSQ, Logrus, FX, Xorm, Cobra, etc., in order to quickly enter business development, template development.


Integrated third-party libraries and technologies

  • Github Actions
  • Custom Loggerlogrus
  • CLI commandcobra
  • Configure readerviper
  • Web frameworkecho
  • Dependency injectionfx
  • ORM xorm
  • Swagger generator swag echo-swagger
  • Messaging NSQ
  • golangci-lint golangci-lint
  • Migrate migrate
  • .

The project code structure is layered

-app # main program entry -cmd -... Mysql # docker-comemagy-nsq # NSQ docker-comemagy-nsq # NSQ docker-comemagy-nsq # Other deployment related - docs # Swag generated Swagger2.0 document directory - Internal # Core business Logic - Controller # HTTP handler (Controller layer) - HTTP # HTTP Server - lib # Core base - Models # model - NSQ # producer and NSQ Consumer - Repository # Repository layer - service Service layer - utils # utility general (utility methods, constants, common error definitions) -... -...Copy the code

Build publish dependencies

Swag generates the latest Swagger document

> swag init -g app/main.go
Copy the code

The resulting Swagger address is http://{IP}:{PORT}/swagger/index.html

Build and publish images

  • local
    > cd .
    > docker build . --file deploy/Dockerfile --tag {ImageTag}
    Copy the code
  • github action

    Automatic build and publish image has been done inside CI (the value of {secrets.access_username} configured under github’s current project secrets is docker Hub username)

BE initializes and starts the deployment

  1. createdockerCustom networkgo_starter_network
    > docker network create go_starter_network
    Copy the code
  2. mysqlTo start withnsqStart the
    #MYSQL start
    > cd deploy/mysql
    > docker-compose up -d
    #NSQ start
    > cd deploy/nsq
    > docker-compose up -d
    Copy the code
  3. The BE back-end service is started
    #Go - the starter start
    > cd deploy
    > docker-compose up -d
    Copy the code
  4. Access the health persistence interface
    http://{IP}:{PORT}/
    Copy the code

Description of other documents

  • Dockerfile — Docker image is divided into two stages of construction, Builder stage and packaging stage.

      The # Builder phase builds the binary executable
      FROM golang:1.16.1-alpine3.13 as builder
      ......
      RUN CGO_ENABLED=0 GOOS=linux go build -o go_starter app/main.go
      
      # Packaging phase
      FROM alpine
      ......
      COPY the generated executable from the build phase
      COPY --from=builder /app/go_starter /app
      #...
    Copy the code
  • Uber relies on the framework Fx. fx development pack details

    // The code above is not written.var (
        httpCmd = &cobra.Command{
            Use:   "http",
            Short: "Start Http REST API",
            Run:   initHTTP,
        }
    )
    
    func initHTTP(cmd *cobra.Command, args []string) {
        fx.New(inject()).Run()
    }
    
    // Start sequentially.
    func inject(a) fx.Option {
        return fx.Options(
            fx.Provide(
                config.NewConfig,
                utils.NewTimeoutContext,
            ),
            libs.XormModule,
            repository.Module,
            service.Module,
            controller.Module,
            nsq.ProducerModule,
            nsq.ConsumerModule,
            http.Module,
        )
    }
    
    Copy the code
  • Swag indicates that the doc directory is automatically generated and does not need to be changed