main.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "github.com/longjoy/micro-go-course/section11/user/dao"
  7. "github.com/longjoy/micro-go-course/section11/user/endpoint"
  8. "github.com/longjoy/micro-go-course/section11/user/redis"
  9. "github.com/longjoy/micro-go-course/section11/user/service"
  10. "github.com/longjoy/micro-go-course/section11/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. //waitTime = flag.Int("wait.time", 10, "wait time")
  23. mysqlAddr = flag.String("mysql.addr", "127.0.0.1", "mysql addr")
  24. mysqlPort = flag.String("mysql.port", "3306", "mysql port")
  25. redisAddr = flag.String("redis.addr", "127.0.0.1", "redis addr")
  26. redisPort = flag.String("redis.port", "6379", "redis port")
  27. )
  28. flag.Parse()
  29. ctx := context.Background()
  30. errChan := make(chan error)
  31. err := dao.InitMysql(*mysqlAddr, *mysqlPort, "root", "123456", "user")
  32. if err != nil{
  33. log.Fatal(err)
  34. }
  35. err = redis.InitRedis(*redisAddr, *redisPort, "" )
  36. if err != nil{
  37. log.Fatal(err)
  38. }
  39. userService := service.MakeUserServiceImpl(&dao.UserDAOImpl{})
  40. userEndpoints := &endpoint.UserEndpoints{
  41. endpoint.MakeRegisterEndpoint(userService),
  42. endpoint.MakeLoginEndpoint(userService),
  43. }
  44. r := transport.MakeHttpHandler(ctx, userEndpoints)
  45. go func() {
  46. errChan <- http.ListenAndServe(":" + strconv.Itoa(*servicePort), r)
  47. }()
  48. go func() {
  49. // 监控系统信号,等待 ctrl + c 系统信号通知服务关闭
  50. c := make(chan os.Signal, 1)
  51. signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
  52. errChan <- fmt.Errorf("%s", <-c)
  53. }()
  54. error := <-errChan
  55. log.Println(error)
  56. }