/go-starter

Go 服务框架脚手架. 整合 echo、swag、viper、nsq、logrus、fx、xorm、cobra 等第三方库

Primary LanguageGoApache License 2.0Apache-2.0

go-starter

github activity

Stargazers repo roster for @pinkhello/go-starter

Stargazers over time

Demo

Demo: 房产CRM

thirdparty

code layer

   - app        # application main
     - cmd
     - ... 
   - config       # config
   - deploy       # ci/cd
     - mysql      # mysql docker-compose
     - nsq        # nsq docker-compose
     - ...        # other     
   - docs         # swag gen swagger2.0 doc
   - internal     # core 
     - controller # http handler(controller)
     - http       # http sever startup
     - lib        # lib
     - models     # models
     - nsq        # nsq producer & nsq consumer startup
     - repository # repository 
     - service    # service
   - utils        # util
     - ... 
   - ...

Build & Publish & Deploy

swag tips

> swag init -g app/main.go

swagger url: http://{IP}:{PORT}/swagger/index.html

build

  • local
    > cd .
    > docker build . --file deploy/Dockerfile --tag {ImageTag}
  • github action
    > { secrets.ACCESS_USERNAME }: `your docker hub username`

be deploy

  1. docker network:go_starter_network
    > docker network create go_starter_network
  2. mysql & nsq
    # MYSQL start
    > cd deploy/mysql
    > docker-compose up -d
    # NSQ start
    > cd deploy/nsq
    > docker-compose up -d
  3. be server
    # go-starter start
    > cd deploy
    > docker-compose up -d
  4. Health
    http://{IP}:{PORT}/

Other

  • Dockerfile

      # build go 
      FROM golang:1.16.1-alpine3.13 as builder
      ......
      RUN CGO_ENABLED=0 GOOS=linux go build -o go_starter app/main.go
      
      # package stage
      FROM alpine
      ......
      # copy from builder
      COPY --from=builder /app/go_starter /app
      # ......
  • Uber IOC/DI: fx

    //other code
    ......
    
    var (
        httpCmd = &cobra.Command{
            Use:   "http",
            Short: "Start Http REST API",
            Run:   initHTTP,
        }
    )
    
    func initHTTP(cmd *cobra.Command, args []string) {
        fx.New(inject()).Run()
    }
    
    func inject() 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,
        )
    }