0aa669c0d2849969fc434e7ca773facc9984068e.svn-base 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "github.com/longjoy/micro-go-course/section31/config"
  7. "github.com/longjoy/micro-go-course/section31/endpoint"
  8. "github.com/longjoy/micro-go-course/section31/model"
  9. "github.com/longjoy/micro-go-course/section31/service"
  10. "github.com/longjoy/micro-go-course/section31/transport"
  11. "net/http"
  12. "os"
  13. "os/signal"
  14. "strconv"
  15. "syscall"
  16. )
  17. func main() {
  18. var (
  19. servicePort = flag.Int("service.port", 10098, "service port")
  20. )
  21. flag.Parse()
  22. ctx := context.Background()
  23. errChan := make(chan error)
  24. var tokenService service.TokenService
  25. var tokenGranter service.TokenGranter
  26. var tokenEnhancer service.TokenEnhancer
  27. var tokenStore service.TokenStore
  28. var userDetailsService service.UserDetailsService
  29. var clientDetailsService service.ClientDetailsService
  30. var srv service.Service
  31. tokenEnhancer = service.NewJwtTokenEnhancer("secret")
  32. tokenStore = service.NewJwtTokenStore(tokenEnhancer.(*service.JwtTokenEnhancer))
  33. tokenService = service.NewTokenService(tokenStore, tokenEnhancer)
  34. userDetailsService = service.NewInMemoryUserDetailsService([] *model.UserDetails{{
  35. Username: "aoho",
  36. Password: "123456",
  37. UserId: 1,
  38. Authorities: []string{"Simple"},
  39. },
  40. {
  41. Username: "admin",
  42. Password: "123456",
  43. UserId: 1,
  44. Authorities: []string{"Admin"},
  45. }})
  46. clientDetailsService = service.NewInMemoryClientDetailService([] *model.ClientDetails{{
  47. "clientId",
  48. "clientSecret",
  49. 1800,
  50. 18000,
  51. "http://127.0.0.1",
  52. [] string{"password", "refresh_token"},
  53. }})
  54. tokenGranter = service.NewComposeTokenGranter(map[string]service.TokenGranter{
  55. "password": service.NewUsernamePasswordTokenGranter("password", userDetailsService, tokenService),
  56. "refresh_token": service.NewRefreshGranter("refresh_token", userDetailsService, tokenService),
  57. })
  58. tokenEndpoint := endpoint.MakeTokenEndpoint(tokenGranter, clientDetailsService)
  59. tokenEndpoint = endpoint.MakeClientAuthorizationMiddleware(config.KitLogger)(tokenEndpoint)
  60. checkTokenEndpoint := endpoint.MakeCheckTokenEndpoint(tokenService)
  61. checkTokenEndpoint = endpoint.MakeClientAuthorizationMiddleware(config.KitLogger)(checkTokenEndpoint)
  62. srv = service.NewCommonService()
  63. //创建健康检查的Endpoint
  64. healthEndpoint := endpoint.MakeHealthCheckEndpoint(srv)
  65. endpts := endpoint.OAuth2Endpoints{
  66. TokenEndpoint:tokenEndpoint,
  67. CheckTokenEndpoint:checkTokenEndpoint,
  68. HealthCheckEndpoint: healthEndpoint,
  69. }
  70. //创建http.Handler
  71. r := transport.MakeHttpHandler(ctx, endpts, tokenService, clientDetailsService, config.KitLogger)
  72. go func() {
  73. config.Logger.Println("Http Server start at port:" + strconv.Itoa(*servicePort))
  74. handler := r
  75. errChan <- http.ListenAndServe(":" + strconv.Itoa(*servicePort), handler)
  76. }()
  77. go func() {
  78. c := make(chan os.Signal, 1)
  79. signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
  80. errChan <- fmt.Errorf("%s", <-c)
  81. }()
  82. error := <-errChan
  83. config.Logger.Println(error)
  84. }