accf7b30e871a48a11a8412bf852139e744b74e5.svn-base 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "github.com/longjoy/micro-go-course/section25/goods/endpoint"
  7. "github.com/longjoy/micro-go-course/section25/goods/service"
  8. "github.com/longjoy/micro-go-course/section25/goods/transport"
  9. "golang.org/x/time/rate"
  10. "log"
  11. "net/http"
  12. "os"
  13. "os/signal"
  14. "strconv"
  15. "syscall"
  16. )
  17. func main() {
  18. servicePort := flag.Int("service.port", 12313, "service port")
  19. flag.Parse()
  20. errChan := make(chan error)
  21. srv := service.NewGoodsServiceImpl()
  22. go srv.InitConfig(context.Background())
  23. limiter := rate.NewLimiter(10, 10)
  24. endpoints := endpoint.GoodsEndpoints{
  25. GoodsDetailEndpoint: endpoint.MakeGoodsDetailEndpoint(srv, limiter),
  26. }
  27. handler := transport.MakeHttpHandler(context.Background(), &endpoints)
  28. go func() {
  29. errChan <- http.ListenAndServe(":"+strconv.Itoa(*servicePort), handler)
  30. }()
  31. go func() {
  32. // 监控系统信号,等待 ctrl + c 系统信号通知服务关闭
  33. c := make(chan os.Signal, 1)
  34. signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
  35. errChan <- fmt.Errorf("%s", <-c)
  36. }()
  37. err := <-errChan
  38. log.Printf("listen err : %s", err)
  39. }