package main import ( "flag" "fmt" "github.com/go-kratos/kratos/v2" "gopkg.in/natefinch/lumberjack.v2" "os" "sikey/w303a/http/internal/conf" "sikey/w303a/http/pkg/registry" "sikey/w303a/http/pkg/zaplog" "time" "github.com/go-kratos/kratos/v2/config" "github.com/go-kratos/kratos/v2/config/file" "github.com/go-kratos/kratos/v2/transport/grpc" "github.com/go-kratos/kratos/v2/transport/http" _ "go.uber.org/automaxprocs" ) // 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 flagHttpAddr string flagGrpcAddr string id, _ = os.Hostname() ) func init() { var sh, _ = time.LoadLocation("Asia/Shanghai") time.Local = sh flag.StringVar(&flagconf, "conf", "../../configs", "config path, eg: -conf config.yaml") flag.StringVar(&flagHttpAddr, "http", "0.0.0.0:8000", "http listen addr eg: 0.0.0.0:8000") flag.StringVar(&flagGrpcAddr, "grpc", "0.0.0.0:9000", "grpc listen addr eg: 0.0.0.0:9000") } func newApp(c *conf.Server, gs *grpc.Server, hs *http.Server) *kratos.App { return kratos.New( kratos.ID(id), kratos.Name(Name), kratos.Version(Version), kratos.Metadata(map[string]string{}), kratos.Registrar(registry.New(c.Registry.Etcd.Endpoints)), kratos.Server( gs, hs, ), ) } func main() { flag.Parse() c := config.New( config.WithSource( file.NewSource(flagconf), ), ) defer c.Close() if err := c.Load(); err != nil { panic(err) } var bc conf.Bootstrap if err := c.Scan(&bc); err != nil { panic(err) } bc.Server.Http.Addr = flagHttpAddr bc.Server.Grpc.Addr = flagGrpcAddr lj := &lumberjack.Logger{ Filename: fmt.Sprintf(bc.Server.Logger.Output, Name), MaxSize: int(bc.Server.Logger.MaxSize), MaxBackups: int(bc.Server.Logger.MaxBackups), MaxAge: int(bc.Server.Logger.MaxAge), Compress: bc.Server.Logger.Compress, } lv := zaplog.New(zaplog.WithConsole(), zaplog.WithLumberjack(lj)).Build() app, cleanup, err := wireApp(bc.Server, bc.Data, lv) if err != nil { panic(err) } defer cleanup() // start and wait for stop signal if err = app.Run(); err != nil { panic(err) } }