main.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "github.com/longjoy/micro-go-course/section08/user/dao"
  7. "github.com/longjoy/micro-go-course/section08/user/endpoint"
  8. "github.com/longjoy/micro-go-course/section08/user/redis"
  9. "github.com/longjoy/micro-go-course/section08/user/service"
  10. "github.com/longjoy/micro-go-course/section08/user/transport"
  11. "log"
  12. "net/http"
  13. "os"
  14. "os/signal"
  15. "strconv"
  16. "syscall"
  17. )
  18. func main() {
  19. var (
  20. // 服务地址和服务名
  21. servicePort = flag.Int("service.port", 10086, "service port")
  22. )
  23. flag.Parse()
  24. ctx := context.Background()
  25. errChan := make(chan error)
  26. err := dao.InitMysql("47.92.52.73", "3306", "root", "root123456", "user")
  27. if err != nil{
  28. log.Fatal(err)
  29. }
  30. err = redis.InitRedis("127.0.0.1","6379", "" )
  31. if err != nil{
  32. log.Fatal(err)
  33. }
  34. userService := service.MakeUserServiceImpl(&dao.UserDAOImpl{})
  35. userEndpoints := &endpoint.UserEndpoints{
  36. endpoint.MakeRegisterEndpoint(userService),
  37. endpoint.MakeLoginEndpoint(userService),
  38. }
  39. r := transport.MakeHttpHandler(ctx, userEndpoints)
  40. go func() {
  41. errChan <- http.ListenAndServe(":" + strconv.Itoa(*servicePort), r)
  42. }()
  43. go func() {
  44. // 监控系统信号,等待 ctrl + c 系统信号通知服务关闭
  45. c := make(chan os.Signal, 1)
  46. signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
  47. errChan <- fmt.Errorf("%s", <-c)
  48. }()
  49. error := <-errChan
  50. log.Println(error)
  51. }