main.go 1.4 KB
Newer Older
kzkzzzz's avatar
kzkzzzz committed
1 2 3 4 5
package main

import (
	"flag"
	"github.com/go-kratos/kratos-layout/internal/conf"
kzkzzzz's avatar
kzkzzzz committed
6
	"github.com/go-kratos/kratos/v2"
kzkzzzz's avatar
kzkzzzz committed
7 8 9 10
	"github.com/go-kratos/kratos/v2/log"
	"github.com/go-kratos/kratos/v2/middleware/tracing"
	"github.com/go-kratos/kratos/v2/transport/grpc"
	"github.com/go-kratos/kratos/v2/transport/http"
kzkzzzz's avatar
kzkzzzz committed
11
	"os"
kzkzzzz's avatar
kzkzzzz committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
)

// go build -ldflags "-X main.Version=x.y.z"
var (
	// Name is the name of the compiled software.
	Name string
	// Version is the version of the compiled software.
	Version string
	// flagconf is the config flag.
	flagconf string

	id, _ = os.Hostname()
)

func init() {
kzkzzzz's avatar
kzkzzzz committed
27
	flag.StringVar(&flagconf, "conf", "", "config file, eg: -conf config.yaml")
kzkzzzz's avatar
kzkzzzz committed
28 29
}

kzkzzzz's avatar
kzkzzzz committed
30
func newApp(hs *http.Server, gs *grpc.Server) *kratos.App {
kzkzzzz's avatar
kzkzzzz committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44
	return kratos.New(
		kratos.ID(id),
		kratos.Name(Name),
		kratos.Version(Version),
		kratos.Metadata(map[string]string{}),
		kratos.Server(
			hs,
			gs,
		),
	)
}

func main() {
	flag.Parse()
kzkzzzz's avatar
kzkzzzz committed
45 46 47
	var c conf.Config
	conf.LoadFromYaml(flagconf, &c)

kzkzzzz's avatar
kzkzzzz committed
48 49 50 51 52 53 54 55 56
	logger := log.With(log.NewStdLogger(os.Stdout),
		"ts", log.DefaultTimestamp,
		"caller", log.DefaultCaller,
		"service.id", id,
		"service.name", Name,
		"service.version", Version,
		"trace.id", tracing.TraceID(),
		"span.id", tracing.SpanID(),
	)
kzkzzzz's avatar
kzkzzzz committed
57 58
	log.SetLogger(logger)

kzkzzzz's avatar
kzkzzzz committed
59
	app, cleanup, err := wireApp(c.Server, c.Data)
kzkzzzz's avatar
kzkzzzz committed
60 61 62 63 64 65 66 67 68 69
	if err != nil {
		panic(err)
	}
	defer cleanup()

	// start and wait for stop signal
	if err := app.Run(); err != nil {
		panic(err)
	}
}