96 lines
2.0 KiB
Go
96 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"center-api/internal/conf"
|
|
log2 "center-api/internal/pkg/log"
|
|
"flag"
|
|
"fmt"
|
|
"github.com/go-kratos/kratos/v2"
|
|
"github.com/go-kratos/kratos/v2/config"
|
|
"github.com/go-kratos/kratos/v2/config/file"
|
|
"github.com/go-kratos/kratos/v2/log"
|
|
"github.com/go-kratos/kratos/v2/transport/http"
|
|
_ "go.uber.org/automaxprocs"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
// go build -ldflags "-X main.Version=x.y.z"
|
|
var (
|
|
// Name is the name of the compiled software.
|
|
Name string = "marketingApi"
|
|
// Version is the version of the compiled software.
|
|
Version string
|
|
// flagconf is the config flag.
|
|
flagconf string
|
|
|
|
id, _ = os.Hostname()
|
|
)
|
|
|
|
func newApp(
|
|
logger log.Logger,
|
|
httpServer *http.Server,
|
|
) *kratos.App {
|
|
return kratos.New(
|
|
kratos.ID(id),
|
|
kratos.Name(Name),
|
|
kratos.Version(Version),
|
|
kratos.Logger(logger),
|
|
kratos.Server(
|
|
httpServer,
|
|
),
|
|
)
|
|
}
|
|
|
|
func main() {
|
|
bc := loadConfig()
|
|
businessLogger := log2.NewBusinessLogger(bc, id, Name, Version)
|
|
accessLogger := log2.NewAccessLogger(bc, id, Name, Version)
|
|
|
|
app, cleanup, err := wireApp(bc, businessLogger, accessLogger)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer cleanup()
|
|
|
|
// start and wait for stop signal
|
|
if err := app.Run(); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func loadConfig() *conf.Bootstrap {
|
|
flag.StringVar(&flagconf, "conf", "./configs", "config path, eg: -conf config.yaml")
|
|
flag.Parse()
|
|
|
|
//支持子目录, 线上的系统配置会挂载为子目录
|
|
sources := make([]config.Source, 0)
|
|
err := filepath.Walk(flagconf, func(path string, info os.FileInfo, err error) error {
|
|
if err == nil && info.IsDir() {
|
|
sources = append(sources, file.NewSource(path))
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
fmt.Printf("filepath.Walk() returned %+v \n", err)
|
|
panic(err)
|
|
}
|
|
|
|
c := config.New(
|
|
config.WithSource(sources...),
|
|
)
|
|
defer c.Close()
|
|
|
|
if err = c.Load(); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
bc := &conf.Bootstrap{}
|
|
if err = c.Scan(bc); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
fmt.Printf("已加载的配置数据:%+v\n", bc)
|
|
return bc
|
|
}
|