Go 服务框架脚手架. 整合 echo、swag、viper、nsq、logrus、fx、xorm、cobra 等第三方库
Demo: 房产CRM
- 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
- ...
- ...
> swag init -g app/main.go
swagger url: http://{IP}:{PORT}/swagger/index.html
> cd .
> docker build . --file deploy/Dockerfile --tag {ImageTag}
> { secrets.ACCESS_USERNAME }: `your docker hub username`
docker network
:go_starter_network
> docker network create go_starter_network
mysql
& nsq
# MYSQL start
> cd deploy/mysql
> docker-compose up -d
# NSQ start
> cd deploy/nsq
> docker-compose up -d
be server
# go-starter start
> cd deploy
> docker-compose up -d
Health
http://{IP}:{PORT}/
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,
)
}