323cfb9e1ff6e8b714bd9f1ee74398df960b6d5b.svn-base 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "github.com/longjoy/micro-go-course/section19/cargo/component"
  7. "github.com/longjoy/micro-go-course/section19/cargo/endpoint"
  8. "github.com/longjoy/micro-go-course/section19/cargo/transport"
  9. "net/http"
  10. "os"
  11. "os/signal"
  12. "syscall"
  13. "time"
  14. "github.com/go-kit/kit/log"
  15. "gopkg.in/mgo.v2"
  16. "github.com/longjoy/micro-go-course/section19/cargo/dao/inmem"
  17. "github.com/longjoy/micro-go-course/section19/cargo/dao/mongo"
  18. "github.com/longjoy/micro-go-course/section19/cargo/inspection"
  19. shipping "github.com/longjoy/micro-go-course/section19/cargo/model"
  20. "github.com/longjoy/micro-go-course/section19/cargo/service/booking"
  21. "github.com/longjoy/micro-go-course/section19/cargo/service/handling"
  22. )
  23. const (
  24. defaultPort = "8080"
  25. defaultHost = "localhost"
  26. defaultMongoDBURL = "mongodb://129.211.63.96:27117"
  27. defaultDBName = "cargo"
  28. )
  29. func main() {
  30. var (
  31. port = envString("PORT", defaultPort)
  32. addr = envString("HOST", defaultHost)
  33. dburl = envString("MONGODB_URL", defaultMongoDBURL)
  34. dbname = envString("DB_NAME", defaultDBName)
  35. //consulHost = flag.String("consul.host", "114.67.98.210", "consul server ip address")
  36. consulHost = flag.String("consul.host", "106.15.233.99", "consul server ip address")
  37. consulPort = flag.String("consul.port", "8500", "consul server port")
  38. serviceHost = flag.String("service.host", addr, "service ip address")
  39. servicePort = flag.String("service.port", port, "service port")
  40. mongoDBURL = flag.String("db.url", dburl, "MongoDB URL")
  41. databaseName = flag.String("db.name", dbname, "MongoDB database name")
  42. inmemory = flag.Bool("inmem", false, "use in-memory repositories")
  43. )
  44. flag.Parse()
  45. ctx := context.Background()
  46. var logger log.Logger
  47. logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
  48. logger = log.With(logger, "ts", log.DefaultTimestampUTC)
  49. // Setup repositories
  50. var (
  51. cargos shipping.CargoRepository
  52. locations shipping.LocationRepository
  53. voyages shipping.VoyageRepository
  54. handlingEvents shipping.HandlingEventRepository
  55. )
  56. if *inmemory {
  57. cargos = inmem.NewCargoRepository()
  58. locations = inmem.NewLocationRepository()
  59. voyages = inmem.NewVoyageRepository()
  60. handlingEvents = inmem.NewHandlingEventRepository()
  61. } else {
  62. session, err := mgo.Dial(*mongoDBURL)
  63. if err != nil {
  64. panic(err)
  65. }
  66. defer session.Close()
  67. session.SetMode(mgo.Monotonic, true)
  68. cargos, _ = mongo.NewCargoRepository(*databaseName, session)
  69. locations, _ = mongo.NewLocationRepository(*databaseName, session)
  70. voyages, _ = mongo.NewVoyageRepository(*databaseName, session)
  71. handlingEvents = mongo.NewHandlingEventRepository(*databaseName, session)
  72. }
  73. // Configure some questionable dependencies.
  74. var (
  75. handlingEventFactory = shipping.HandlingEventFactory{
  76. CargoRepository: cargos,
  77. VoyageRepository: voyages,
  78. LocationRepository: locations,
  79. }
  80. handlingEventHandler = handling.NewEventHandler(
  81. inspection.NewService(cargos, handlingEvents, nil),
  82. )
  83. )
  84. // Facilitate testing by adding some cargos.
  85. storeTestData(cargos)
  86. //fieldKeys := []string{"method"}
  87. var bs booking.Service
  88. bs = booking.NewService(cargos, locations, handlingEvents)
  89. bs = booking.NewLoggingService(log.With(logger, "component", "booking"), bs)
  90. var hs handling.Service
  91. hs = handling.NewService(handlingEvents, handlingEventFactory, handlingEventHandler)
  92. hs = handling.NewLoggingService(log.With(logger, "component", "handling"), hs)
  93. endpoints := &endpoint.CargoEndpoints{
  94. endpoint.MakeBookCargoEndpoint(bs),
  95. endpoint.MakeLoadCargoEndpoint(bs),
  96. endpoint.AssignCargoToRouteEndpoint(bs),
  97. endpoint.ChangeDestinationEndpoint(bs),
  98. endpoint.CargosEndpoint(bs),
  99. endpoint.LocationsEndpoint(bs),
  100. endpoint.RegisterHandlingEventEndpoint(hs),
  101. }
  102. r := transport.MakeHttpHandler(ctx, endpoints)
  103. //创建注册对象
  104. //TODO replace with pkg consul
  105. registar := component.Register(*consulHost, *consulPort, *serviceHost, *servicePort, logger)
  106. errs := make(chan error, 2)
  107. go func() {
  108. logger.Log("transport", "http", "address", servicePort, "msg", "listening")
  109. //启动前执行注册
  110. registar.Register()
  111. errs <- http.ListenAndServe(":"+*servicePort, r)
  112. }()
  113. go func() {
  114. c := make(chan os.Signal, 1)
  115. signal.Notify(c, syscall.SIGINT)
  116. errs <- fmt.Errorf("%s", <-c)
  117. }()
  118. error := <-errs
  119. //服务退出取消注册
  120. registar.Deregister()
  121. logger.Log("terminated", error)
  122. }
  123. func envString(env, fallback string) string {
  124. e := os.Getenv(env)
  125. if e == "" {
  126. return fallback
  127. }
  128. return e
  129. }
  130. func storeTestData(r shipping.CargoRepository) {
  131. test1 := shipping.NewCargo("FTL456", shipping.RouteSpecification{
  132. Origin: shipping.AUMEL,
  133. Destination: shipping.SESTO,
  134. ArrivalDeadline: time.Now().AddDate(0, 0, 7),
  135. })
  136. if _, err := r.Store(test1); err != nil {
  137. panic(err)
  138. }
  139. test2 := shipping.NewCargo("ABC123", shipping.RouteSpecification{
  140. Origin: shipping.SESTO,
  141. Destination: shipping.CNHKG,
  142. ArrivalDeadline: time.Now().AddDate(0, 0, 14),
  143. })
  144. if _, err := r.Store(test2); err != nil {
  145. panic(err)
  146. }
  147. }