main.go 1012 B

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