main.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "github.com/go-kratos/kratos/v2"
  6. "gopkg.in/natefinch/lumberjack.v2"
  7. "os"
  8. "sikey/w303a/http/internal/conf"
  9. "sikey/w303a/http/pkg/registry"
  10. "sikey/w303a/http/pkg/zaplog"
  11. "time"
  12. "github.com/go-kratos/kratos/v2/config"
  13. "github.com/go-kratos/kratos/v2/config/file"
  14. "github.com/go-kratos/kratos/v2/transport/grpc"
  15. "github.com/go-kratos/kratos/v2/transport/http"
  16. _ "go.uber.org/automaxprocs"
  17. )
  18. // go build -ldflags "-X main.Version=x.y.z"
  19. var (
  20. // Name is the name of the compiled software.
  21. Name string
  22. // Version is the version of the compiled software.
  23. Version string
  24. // flagconf is the config flag.
  25. flagconf string
  26. flagHttpAddr string
  27. flagGrpcAddr string
  28. id, _ = os.Hostname()
  29. )
  30. func init() {
  31. var sh, _ = time.LoadLocation("Asia/Shanghai")
  32. time.Local = sh
  33. flag.StringVar(&flagconf, "conf", "../../configs", "config path, eg: -conf config.yaml")
  34. flag.StringVar(&flagHttpAddr, "http", "0.0.0.0:8000", "http listen addr eg: 0.0.0.0:8000")
  35. flag.StringVar(&flagGrpcAddr, "grpc", "0.0.0.0:9000", "grpc listen addr eg: 0.0.0.0:9000")
  36. }
  37. func newApp(c *conf.Server, gs *grpc.Server, hs *http.Server) *kratos.App {
  38. return kratos.New(
  39. kratos.ID(id),
  40. kratos.Name(Name),
  41. kratos.Version(Version),
  42. kratos.Metadata(map[string]string{}),
  43. kratos.Registrar(registry.New(c.Registry.Etcd.Endpoints)),
  44. kratos.Server(
  45. gs,
  46. hs,
  47. ),
  48. )
  49. }
  50. func main() {
  51. flag.Parse()
  52. c := config.New(
  53. config.WithSource(
  54. file.NewSource(flagconf),
  55. ),
  56. )
  57. defer c.Close()
  58. if err := c.Load(); err != nil {
  59. panic(err)
  60. }
  61. var bc conf.Bootstrap
  62. if err := c.Scan(&bc); err != nil {
  63. panic(err)
  64. }
  65. bc.Server.Http.Addr = flagHttpAddr
  66. bc.Server.Grpc.Addr = flagGrpcAddr
  67. lj := &lumberjack.Logger{
  68. Filename: fmt.Sprintf(bc.Server.Logger.Output, Name),
  69. MaxSize: int(bc.Server.Logger.MaxSize),
  70. MaxBackups: int(bc.Server.Logger.MaxBackups),
  71. MaxAge: int(bc.Server.Logger.MaxAge),
  72. Compress: bc.Server.Logger.Compress,
  73. }
  74. lv := zaplog.New(zaplog.WithConsole(), zaplog.WithLumberjack(lj)).Build()
  75. app, cleanup, err := wireApp(bc.Server, bc.Data, lv)
  76. if err != nil {
  77. panic(err)
  78. }
  79. defer cleanup()
  80. // start and wait for stop signal
  81. if err = app.Run(); err != nil {
  82. panic(err)
  83. }
  84. }